2013-03-14 33 views
0

我試圖用jcrop,以允許用戶創建自己的圖像的縮略圖,這將是190x190 pixelsPHP - imagecopyresampled

jcrop似乎是工作,並給我正確的座標。然而,imagecopyresampled似乎行事非常不可預測,並沒有給我我所期望的。這裏是我的代碼:

$destWidth = $destHeight = 190; 
$jpeg_quality = 90; 
$path = Yii::app()->basePath."/../images/user/"; 
$src = $path.$model->image; 

$img_src = imagecreatefromjpeg($src); 
$img_dest = ImageCreateTrueColor($destWidth, $destHeight); 

imagecopyresampled(
$img_dest, //destination image 
$img_src, //source image 
0, //top left coordinate of the destination image in x direction 
0, //top left coordinate of the destination image in y direction 
$_POST['x'], //top left coordinate in x direction of source image that I want copying to start at 
$_POST['y'], //top left coordinate in y direction of source image that I want copying to start at 
$destWidth, //190, thumbnail width 
$destHeight, //190, thumbnail height 
$_POST['w'], //how wide the rectangle from the source image we want thumbnailed is 
$_POST['h'] //how high the rectangle from the source image we want thumbnailed is 
); 

imagejpeg($img_dest,$path."thumbs/test.jpg",$jpeg_quality); 

我是認真在虧損,我已經檢查了四個$_POST變量都進來正常,但由於某些原因,我不能得到適當的縮略圖。我可以肯定的是,縮略圖通常放大得太多,而我希望從最開始的左上角沒有被使用。

+0

Jcrop是否鎖定到1.0寬高比,如您的目的地190x190圖像? – jimp 2013-03-14 02:30:23

+0

Jcrop鎖定到1:1的正方形選擇區域,但據我imagecopyresampled的理解,不應該有所作爲呢。正確? – Fittersman 2013-03-14 03:17:35

+0

是的,你是對的。我認爲不正確的寬高比可能會導致縮放效果,但它確實會導致一個維度上的拉伸或縮小。是否有現場示例或由此產生的圖像?我開始認爲問題可能在其他地方。你的代碼看起來不錯。 – jimp 2013-03-14 03:51:50

回答

4

這是我最後的源代碼。我發現我的CSS與我的代碼相沖突,因爲它允許最大高度爲550px,最大寬度爲700px。這導致圖像變大,寬度和/或高度不正確。所以在這些情況下,我不得不根據圖像的高寬比添加乘數,以及CSS如何調整大小。

 $destWidth = $destHeight = 190; 
     $jpeg_quality = 90; 

     $path = Yii::app()->basePath."/../images/user/"; 
     $src = $path.$model->image; 
     $img_src = imagecreatefromjpeg($src); 
     $img_dest = ImageCreateTrueColor($destWidth, $destHeight); 

     // 
     // IMPORTANT!! 
     // If you change the max-width or max-height in the css, you MUST change them here too!! 
     // 
     $maxWidth = 700; 
     $maxHeight = 550; 

     $srcWidth = imagesx($img_src); 
     $srcHeight = imagesy($img_src); 
     $srcRatio = $srcWidth/$srcHeight; 
     $mult = 1; 

     //if the image is wider than the max allowed width AND has a wider aspect ratio than a max height + max width image 
     if ($srcWidth > $maxWidth && $srcRatio > $maxWidth/$maxHeight) { 
      $mult = $srcWidth/$maxWidth; 
     //else if the image is taller than the max height 
     } else if ($srcHeight > $maxHeight) { 
      $mult = $srcHeight/$maxHeight; 
     } 

     imagecopyresampled(
      $img_dest, //destination image 
      $img_src, //source image 
      0, //top left coordinate of the destination image in x direction 
      0, //top left coordinate of the destination image in y direction 
      $_POST['x']*$mult, //top left coordinate in x direction of source image that I want copying to start at 
      $_POST['y']*$mult, //top left coordinate in y direction of source image that I want copying to start at 
      $destWidth, //190, thumbnail width 
      $destHeight, //190, thumbnail height 
      $_POST['w']*$mult, //how wide the rectangle from the source image we want thumbnailed is 
      $_POST['h']*$mult //how high the rectangle from the source image we want thumbnailed is 
     ); 

     imagejpeg($img_dest,$path."thumbs/$model->image",$jpeg_quality);