3

我有一个 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)?

我需要这个拇指

4

2 回答 2

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 - 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

但是由于我不确定您想要缩略图的大小,因此我必须让您找到正确的值。

于 2013-05-21T08:03:08.497 回答
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;
    }
于 2013-05-22T10:27:00.587 回答