2012-04-17 187 views
1

我想要做的是調整大小和裁剪上傳圖像的中心。到目前爲止,我已經把它放在了調整圖像大小的地方,就這些。 我知道imagecopy功能是我想要的,我只是無法讓它與我的功能一起工作。試圖調整大小和裁剪圖像的縮略圖

這是我到目前爲止。

/* read the source image */ 
$source_image = imagecreatefromjpeg($src); 
$width = imagesx($source_image); 
$height = imagesy($source_image); 

/* find the "desired height" of this thumbnail, relative to the desired width */ 
$desired_height = floor($height*($desired_width/$width)); 

/* create a new, "virtual" image */ 
$virtual_image = imagecreatetruecolor($desired_width,$desired_height); 

/* copy source image at a resized size */ 

imagecopyresampled($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height); 

/* create the physical thumbnail image to its destination */ 
imagejpeg($virtual_image,$dest); 

我只需要知道在哪裏以及如何合併圖像複製功能。

謝謝。

回答

0

看看this class。它包含GD2,就像您提供的示例一樣,它還具有縮略圖生成的其他調整大小算法。

+0

感謝您的答覆,但不是更復雜比它是什麼? 我應該不能在imagejpeg之前使用imagecopy來創建物理圖像? 我這樣做的問題是imagecopy的參數1(圖像的src)需要成爲一個資源。我將如何使imagecopyresampled資源在函數中使用? – 2012-04-17 21:05:26

+0

看看代碼,它確實如此,我想方法resizeCrop()。如果您不需要它們,您可以忽略位置零件($ left和$ top開關),但爲了正確居中它們,仍然會推薦它們(對裁剪非常有用)。我實際上推薦resizeFitCrop()函數,它也調整了上傳圖片的大小,因爲裁剪不夠好。這個班級不是「更復雜」,事實上,它是如此簡單和靈活。它用於基於設計要求的動態縮略圖生成框架。非常簡單直接。 – kingmaple 2012-04-18 07:34:21