[英]PHP Image Resize, with Thumbnailer Issue
Having an issue with a image resize function. 图像调整大小功能有问题。
// Image Resizer
function MakeThumbnail($inputFile, $filepath, $ext, $maxWidth, $maxHeight){
/* Get some details about the image */
$srcDetails = getimagesize($inputFile);
switch ($srcDetails[2]) {
case 1: //GIF
$source_image = imagecreatefromgif($inputFile);
break;
case 2: //JPEG
$source_image = imagecreatefromjpeg($inputFile);
break;
case 3: //PNG
$source_image = imagecreatefrompng($inputFile);
break;
case 6: //WBMP
$source_image = imagecreatefromwbmp($inputFile);
break;
default:
break;
}
/* Original Dimensions */
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width, and vice-versa */
if(($maxWidth <= $width) && $maxHeight <= $height){
if($target_ratio == $original_ratio){
$desired_height = $maxHeight;
$desired_width = $maxWidth;
}elseif($target_ratio > $original_ratio){
$desired_height = $maxHeight;
$desired_width = $width * ($maxHeight / $height);
}elseif($target_ratio < $original_ratio){
$desired_height = $height * ($maxWidth / $width);
$desired_width = $maxWidth;
}
}else{
$desired_height = $maxHeight;
$desired_width = $maxWidth;
}
imagecopyresized($virtual_image,$source_image,0,0,0,0,$maxWidth,$desired_height,$width,$height);
/* create the physical thumbnail image to its destination */
switch ($srcDetails[2]) {
case 1: //GIF
imagegif($virtual_image, $filepath);
imagedestroy($virtual_image);
break;
case 2: //JPEG
imagejpeg($virtual_image, $filepath, 100);
imagedestroy($virtual_image);
break;
case 3: //PNG
imagepng($virtual_image, $filepath, 6);
imagedestroy($virtual_image);
break;
case 6: //WBMP
imagewbmp($virtual_image, $filepath);
imagedestroy($virtual_image);
break;
default:
imagedestroy($virtual_image);
break;
}
}
MakeThumbnail($pic2['tmp_name'], $pic2Path, $extension, 800, 600); //<- resize original
MakeThumbnail($pic2['tmp_name'], $pic2ThumbPath, $extension, 150, 100); //<- make a thumbnail
Image resizing with the ratio now works with the updated function above. 现在按比例调整图像大小可以使用上面的更新功能。 Issue I am finding now, is that the image(s) are resized up (made bigger), if the original width is smaller that what I am trying to specify.
我现在发现的问题是,如果原始宽度小于我要指定的宽度,则图像将被调整大小(变大)。
What I am after is a resized image that will never be wider than $maxWidth
, never be taller than $maxHeight
, yet keep the width to height ratio of the original image. 我需要的是经过调整大小的图像,该图像将永远不会比
$maxWidth
宽,也不会比$maxHeight
高,但仍保持原始图像的宽高比。
You want this I think: 我想您要这个:
$width = imagesx($source_image);
$height = imagesy($source_image);
// new code to check if image is too small
if ($width <= $maxWidth || height <= $maxHeight) { //because we need to check either, and not both
// image is too small; don't resize
$doResize = false;
} else {
$doResize = true;
}
Then around your imagecopyresized put: 然后围绕您的imagecopyresize放:
if ($doResize) {
imagecopyresized($virtual_image,$source_image,0,0,0,0,$maxWidth,$desired_height,$width,$height);
}
Got it. 得到它了。
/* Get some details about the image */
$srcDetails = getimagesize($inputFile);
switch ($srcDetails[2]) {
case 1: //GIF
$source_image = imagecreatefromgif($inputFile);
break;
case 2: //JPEG
$source_image = imagecreatefromjpeg($inputFile);
break;
case 3: //PNG
$source_image = imagecreatefrompng($inputFile);
break;
case 6: //WBMP
$source_image = imagecreatefromwbmp($inputFile);
break;
default:
break;
}
/* Original Dimensions */
$width = imagesx($source_image);
$height = imagesy($source_image);
$target_ratio = $maxWidth / $maxHeight;
$original_ratio = $width / $height;
/* find the "desired height" of this thumbnail, relative to the desired width, and vice-versa */
if(($maxWidth <= $width) || $maxHeight <= $height){ // <-- needed to change this to orelse
if($target_ratio == $original_ratio){
$desired_height = $maxHeight;
$desired_width = $maxWidth;
}elseif($target_ratio > $original_ratio){
$desired_height = $maxHeight;
$desired_width = $width * ($maxHeight / $height);
}elseif($target_ratio < $original_ratio){
$desired_height = $height * ($maxWidth / $width);
$desired_width = $maxWidth;
}
}else{
$desired_height = $maxHeight;
$desired_width = $maxWidth;
}
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
imagecopyresized($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
switch ($srcDetails[2]) {
case 1: //GIF
imagegif($virtual_image, $filepath);
imagedestroy($virtual_image);
break;
case 2: //JPEG
imagejpeg($virtual_image, $filepath, 100);
imagedestroy($virtual_image);
break;
case 3: //PNG
imagepng($virtual_image, $filepath, 6);
imagedestroy($virtual_image);
break;
case 6: //WBMP
imagewbmp($virtual_image, $filepath);
imagedestroy($virtual_image);
break;
default:
imagedestroy($virtual_image);
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.