我有一个php缩略图功能。它是如何工作的,你可以查看下面:
public static function makeThumb($source, $destination, $thumb_width){
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];
$x = 0;
$y = 0;
$status = false;
if ($width > $height) {
$x = ceil(($width - $height) / 2);
$width = $height;
} else if ($height > $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
}
$new_image = imagecreatetruecolor($thumb_width,$thumb_width) or die ('Cannot Initialize new GD image stream');
$extension = self::get_file_extension($source);
if ($extension == 'jpg' || $extension == 'jpeg')
$image = imagecreatefromjpeg($source);
if ($extension == 'gif')
$image = imagecreatefromgif($source);
if ($extension == 'png')
$image = imagecreatefrompng($source);
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
if ($extension == 'jpg' || $extension == 'jpeg')
$status = @imagejpeg($new_image, $destination);
if ($extension == 'gif')
$status = @imagegif($new_image, $destination);
if ($extension == 'png')
$status = @imagepng($new_image, $destination);
imagedestroy($image);
return $status;
}
请检查下面的图片(工作原理):
问题:如何获得此图像(此拇指来自photoshop)?
答案 0 :(得分:1)
使用错误的拇指宽度(没有拇指高度!)就是这个方方面面的结果。
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
根据PHP手册(http://php.net/manual/en/function.imagecopyresampled.php),imagecopyresampled复制原始图像的已调整大小的区域。但是你想要整个原创。你可以做到
imagecopyresampled($new_image,$image,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
您需要首先计算$ thumbheight - 高度*拇指宽度/宽度,或者您可以将原始宽度和高度除以设定的数量。
如下面的评论所示,此功能中使用的参数相当混乱。所以我添加了一个解释,希望你能创造你喜欢的东西。
imagecopyresampled
将图像的矩形部分复制并调整为新图像的矩形部分。因此,对于原始图像和新图像,所有参数都需要两次。绘图可能会有所帮助:
$image $new_image
+----------------+ +----------------+
| source img | | destination img|
| | | |
| | | +--------+ |
| +------+| | | part | |
| | part || | | copy | |
| | || | +--------+ |
| +------+| | |
+----------------+ +----------------+
参数是
$dst_image the new image (dst = destination)
$src_image the old image (src = source)
$dst_x , $dst_y top left of destination area
$src_x , $src_y top left of source area
$dst_w , $dst_h width and height of destination area
$src_w , $src_h width and height of source area
如果您尝试将整个源图像复制到新的尺寸,在新的目标图像中,它们将类似于
$dst_image the new image (dst = destination)
$src_image the old image (src = source)
0 , 0 top left of destination area
0 , 0 top left of source area
$dst_w , $dst_h new width and height of thumb
$width , $height width and height of whole source image
但由于我不确定您想要缩略图的大小,我必须让您找到正确的值。
答案 1 :(得分:1)
正确的解决方案在这里:
public static function makeThumb($source, $destination, $square_size=167, $quality=90) {
$status = false;
list($width, $height, $type, $attr) = getimagesize($source);
if($width> $height) {
$width_t = $square_size;
$height_t = round($height/$width*$square_size);
$off_y = ceil(($width_t-$height_t)/2);
$off_x = 0;
} elseif($height> $width) {
$height_t = $square_size;
$width_t = round($width/$height*$square_size);
$off_x = ceil(($height_t-$width_t)/2);
$off_y = 0;
} else {
$width_t = $height_t = $square_size;
$off_x = $off_y = 0;
}
$thumb_p = imagecreatetruecolor($square_size, $square_size);
$extension = self::get_file_extension($source);
if($extension == "gif" or $extension == "png"){
imagecolortransparent($thumb_p, imagecolorallocatealpha($thumb_p, 0, 0, 0, 127));
imagealphablending($thumb_p, false);
imagesavealpha($thumb_p, true);
}
if ($extension == 'jpg' || $extension == 'jpeg')
$thumb = imagecreatefromjpeg($source);
if ($extension == 'gif')
$thumb = imagecreatefromgif($source);
if ($extension == 'png')
$thumb = imagecreatefrompng($source);
$bg = imagecolorallocate ( $thumb_p, 255, 255, 255 );
imagefill ($thumb_p, 0, 0, $bg);
imagecopyresampled($thumb_p, $thumb, $off_x, $off_y, 0, 0, $width_t, $height_t, $width, $height);
if ($extension == 'jpg' || $extension == 'jpeg')
$status = @imagejpeg($thumb_p,$destination,$quality);
if ($extension == 'gif')
$status = @imagegif($thumb_p,$destination,$quality);
if ($extension == 'png')
$status = @imagepng($thumb_p,$destination,9);
imagedestroy($thumb);
imagedestroy($thumb_p);
return $status;
}