2012-11-17 91 views
2

我有如下PHP代碼:作物扭曲/縮放圖像

$w = 300; // Width of new image 
$h = 300; // Height of new image 
$oh = 540; // Original file height 
$ow = 720; // Original file width 
$x = 196; 
$y = 50; 

$image = imagecreatefromjpeg('fileToCrop.jpg'); 
$cropped_image = imagecreatetruecolor($w, $h); 
imagecopyresampled($cropped_image, $image, 0, 0, $x, $y, $ow, $oh, $w, $h); 

imagejpeg($cropped_image, 'fileToCrop.jpg', 100); 

而且要裁剪的圖像,但我的圖像扭曲/比原來的,例如更高:

原文:

enter image description here

裁剪( 「N」 爲 「未」 被示出):

enter image description here

我看不出有什麼錯我的代碼,什麼都發生在圖像變爲更大..

+0

原始尺寸爲720x540。使用以下函數來獲取高度/寬度。 http://php.net/manual/en/function.getimagesize.php – twodayslate

+0

@twodayslate對不起,這是一個錯字。 –

回答

1

你倒的imagecopyresampled

最後4個參數
bool imagecopyresampled (resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h) 

更改它

imagecopyresampled($cropped_image, $image, 0, 0, $x, $y, $w, $h, $ow, $oh); 

但事實上,你確定你不是在尋找裁剪區域的直接副本嗎?

imagecopy($cropped_image, $image, 0, 0, $x, $y, $ow, $oh); 
+0

這不符合預期,因爲圖像被重新採樣。我需要1:1圖像,但剪裁部分用戶選擇。 –

+0

我只需要一張原始圖像,但比例相同。例如,來自400x400像素的300x300像素的正方形。 –

+1

這就是'imagecopy'會做什麼 – emartel

0

因爲你省略imagejpeg(質量),默認爲75% http://php.net/manual/en/function.imagejpeg.php

你能嘗試在第三個參數上加95嗎?

imagejpeg($cropped_image, 'fileToCrop.jpg', 95); 

P.S. 如果你能使用的3rdParty PHP腳本,我會建議你只使用timthumb http://www.binarymoon.co.uk/projects/timthumb/

,如果你需要裁剪位置的變化,

http://www.binarymoon.co.uk/2010/08/timthumb-part-4-moving-crop-location/

+0

我已經定義了質量,但不能按預期工作。 –