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

以下の画像を確認してください(仕組み):

元の画像 サムネイル画像の結果

質問: 結果としてこの画像を取得するにはどうすればよいですか (この親指はフォトショップからのものです)。

この親指が必要です

4

2 に答える 2

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 に答える
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画像の長方形の部分を新しいものの長方形の部分にコピーしてサイズ変更します。したがって、元の画像と新しい画像に対して、すべてのパラメーターが 2 回必要になります。図面が役立つ場合があります。

      $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 に答える