如何設置此代碼以1:1(方形)返回圖像?在PHP中創建正方形1:1縮略圖
目的是創建一個方形(非拉伸)的縮略圖。
我試過在'if部分'中進行更改。我得到一個方形圖像,但伸展。我希望它被裁剪。
define('THUMBNAIL_IMAGE_MAX_WIDTH', 150);
define('THUMBNAIL_IMAGE_MAX_HEIGHT', 150);
$source_image_path = {here the source filename};
$thumbnail_image_path = {here de thumb filename};
list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path);
switch ($source_image_type) {
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif($source_image_path);
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg($source_image_path);
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng($source_image_path);
break;
}
$source_aspect_ratio = $source_image_width/$source_image_height;
$thumbnail_aspect_ratio = THUMBNAIL_IMAGE_MAX_WIDTH/THUMBNAIL_IMAGE_MAX_HEIGHT;
if ($source_image_width <= THUMBNAIL_IMAGE_MAX_WIDTH && $source_image_height <= THUMBNAIL_IMAGE_MAX_HEIGHT) {
$thumbnail_image_width = $source_image_width;
$thumbnail_image_height = $source_image_height;
} elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
$thumbnail_image_width = (int) (THUMBNAIL_IMAGE_MAX_HEIGHT * $source_aspect_ratio);
$thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
} else {
$thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
$thumbnail_image_height = (int) (THUMBNAIL_IMAGE_MAX_WIDTH/$source_aspect_ratio);
}
$thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);
imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);
imagejpeg($thumbnail_gd_image, $thumbnail_image_path, 90);
PS。這不是重複的,我已經閱讀了這個主題的幾個問題,但是我無法將它與我的代碼集成。
可能重複http://stackoverflow.com/questions/ 10107306/php-image-cropping-to-square) – 2014-11-23 22:04:25
你想將圖片縮放並居中放到一個正方形中嗎?還是要將一個正方形從照片中間裁剪出來? – bumperbox 2014-11-23 22:04:41
我希望它是正確的pped。 (這不是重複的,我已經閱讀了這個主題的幾個問題,但是我無法將它整合到我的代碼中)。 – matiq 2014-11-24 12:49:22