2013-05-21 56 views
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; 
    } 

請檢查下面的圖像(它是如何工作):

Original image Thumbnail image result

問:我怎樣才能得到這個圖像的結果(這拇指來自photoshop)?

I need this thumb

+0

使用PHP [imagick庫](http://php.net/manual/en/class.imagick.php),用於多個選項。 – Rikesh

+0

我知道,但需要GD解決方案。謝謝。 – user889349

回答

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

使用錯誤thumbwidth的(沒有thumbheight!)是什麼讓你這個廣場的結果。

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第一 - 高* thumbwidth /寬度會做,或者你可以通過劃分一組量的原始寬度和高度。

如下面的註釋所示,此功能中使用的參數相當混亂。所以我添加一個解釋,希望你能創建你喜歡的東西。

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 

但是,因爲我不確定你想要什麼大小的縮略圖,我必須離開你去找到正確的值。

+0

我需要它被平方。 – user889349

+0

而我找不到解決問題的必要解決方案... – user889349

+0

如果它需要是方形的,看起來像你的photoshop示例,那麼不,我沒有解決方案:) – boisvert