Php 方形缩略前进
我有一个php缩略图函数。它的工作原理您可以在下面查看:Php 方形缩略前进,php,image,graphics,thumbnails,Php,Image,Graphics,Thumbnails,我有一个php缩略图函数。它的工作原理您可以在下面查看: public static function makeThumb($source, $destination, $thumb_width){ $size = getimagesize($source); $width = $size[0]; $height = $size[1]; $x = 0; $y = 0; $
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)
使用错误的拇指宽度(没有拇指高度!)会产生这种方形结果
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
根据PHP手册(),imagecopyresampled复制原始图像的一个调整大小的区域。但是你想要整本原作。你可以
imagecopyresampled($new_image,$image,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
您需要先计算$thumbheight-height*thumbwidth/width,或者您可以将原始宽度和高度除以一个设定值
正如下面的注释所示,此函数中使用的参数相当混乱。所以我添加了一个解释,希望你能创造你喜欢的
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
但是,由于我不确定您希望缩略图的大小,我不得不让您找到正确的值。正确的解决方案是:
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;
}
使用php进行多种选择。我知道,但需要GD解决方案。谢谢。如果它需要是正方形,并且看起来像你的photoshop示例,那么不,我也没有解决方案:)我想说-你的photoshop是矩形的,但是你说你想要一个正方形的图像。所以我没办法解决。但我已经扩展了我的答案,以更清楚地解释参数是如何工作的。希望这能给你解决问题的方法。