>  기사  >  백엔드 개발  >  生成缩略图保存数据库_PHP教程

生成缩略图保存数据库_PHP教程

WBOY
WBOY원래의
2016-07-20 11:05:36961검색

# Constants
define("IMAGE_BASE", './');
define("MAX_WIDTH", 150);
define("MAX_HEIGHT", 150);

# Get image locationstr_replace('..', '', $_SERVER['QUERY_STRING']);
$image_file = 't.jpg';
$image_path = IMAGE_BASE . "$image_file";

# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
     $img = imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
     $img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
     $img = @imagecreatefrompng($image_path);
}

# If an image was successfully loaded, test the image for size
if ($img) {

     # Get image size and scale ratio
     $width = imagesx($img);
     $height = imagesy($img);
     $scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);

     # If the image is larger than the max shrink it
     if ($scale          $new_width =150; //floor($scale*$width);
         $new_height =150;// floor($scale*$height);

         # Create a new temporary image
         $tmp_img = imagecreatetruecolor($new_width, $new_height);

         # Copy and resize old image into new image
         imagecopyresized($tmp_img, $img, 0, 0, 0, 0,$new_width, $new_height, $width, $height);
         imagedestroy($img);
         $img = $tmp_img;
     }
}

# Create error image if necessary
if (!$img) {
     $img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
     imagecolorallocate($img,0,0,0);
     $c = imagecolorallocate($img,70,70,70 );
     imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
     imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}

# Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
imagedestroy($img);
?>

 


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445089.htmlTechArticle?php # Constants define(IMAGE_BASE, './'); define(MAX_WIDTH, 150); define(MAX_HEIGHT, 150); # Get image locationstr_replace('..', '', $_SERVER['QUERY_STRING']); $image_file = 't.jp...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.