【发布时间】:2014-03-22 04:08:19
【问题描述】:
我正在尝试调整图片大小和裁剪图片。
图片可以是横向或纵向的,但我希望缩略图为 250x250 且不拉伸(裁剪侧面或顶部/底部,但将大部分图像保持在新尺寸不变,裁剪为方形 - 有点像 wordpress 的缩略图)。
这是我的功能,它的大小合适,但它会拉伸图片...
我做错了什么?
function make_thumb($src, $dest, $desired_width, $ext) {
if ($ext == 'jpg' || $ext == 'jpeg') {
$source_image = imagecreatefromjpeg($src);
}
if ($ext == 'png') {
$source_image = imagecreatefrompng($src);
}
if ($ext == 'gif') {
$source_image = imagecreatefromgif($src);
}
/* read the source image */
$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));
$desired_height = 250;
/* 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 */
if ($ext == 'jpg' || $ext == 'jpeg') {
imagejpeg($virtual_image, $dest);
}
if ($ext == 'png') {
imagepng($virtual_image, $dest);
}
if ($ext == 'gif') {
imagegif($virtual_image, $dest);
}
}
谢谢!
【问题讨论】:
-
调整图像大小后,是否可以使用
imagecopy进行裁剪? -
@Tom 不,如果您在调整大小的复制中扭曲了图像,则不太可能修复任何问题。
-
@RiggsFolly 你是对的 - 我假设调整大小会按照正确的比例进行。由于
imagecopyresampled也可用于裁剪,因此如果您不想保留驻留图像而只想保留裁剪的缩略图,则使用imagecopy是一个多余的步骤。然而,我们不知道。
标签: php image image-processing thumbnails