用PHP裁剪照片会被拉伸
我正在尝试调整图片的大小和裁剪 图片可以是横向的,也可以是纵向的,但我希望缩略图是250x250,不要拉伸(裁剪侧面或顶部/底部,但保持大部分图片在新的尺寸下完好无损,裁剪成方形-有点像wordpress的缩略图) 这是我的功能,它的大小是正确的,但它拉伸图片 我做错了什么用PHP裁剪照片会被拉伸,php,image,image-processing,thumbnails,Php,Image,Image Processing,Thumbnails,我正在尝试调整图片的大小和裁剪 图片可以是横向的,也可以是纵向的,但我希望缩略图是250x250,不要拉伸(裁剪侧面或顶部/底部,但保持大部分图片在新的尺寸下完好无损,裁剪成方形-有点像wordpress的缩略图) 这是我的功能,它的大小是正确的,但它拉伸图片 我做错了什么 function make_thumb($src, $dest, $desired_width, $ext) { if ($ext == 'jpg' || $ext == 'jpeg') {
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);
}
}
谢谢 想一想你在说什么 如果您更改图像的宽度,但更改高度的比例与更改宽度的比例不完全相同,则图像总是会失真 虽然我没有实际测试calc是否正确,但是您有代码可以正确地执行此操作,并且不会失真,然后您可以使用250的任意高度覆盖该计算。因此你得到了一个扭曲 注释掉这行,它可能会停止失真
$desired_height = 250;
因此,如果您知道始终希望图像的高度为250,请更改计算以计算成比例的新宽度,而不是将其作为参数传入
function make_thumb($src, $dest, $desired_height = 250, $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);
/* calc the new width */
$desired_width = floor($width * ($desired_height / $height));
/* 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不,如果你在调整图像大小复制中扭曲图像,就不可能解决任何问题。@riggsfully你是对的-我假设调整图像大小会达到正确的比例。由于imagecopyresampled
也可用于裁剪,因此如果不想保留剩余图像,而只保留裁剪后的缩略图,则使用imagecopy
是一个多余的步骤。然而,我们不知道。有没有一种方法可以将两者都设置为250乘250,而不在一个上妥协?只有当原始图片的宽度和高度相同时,例如一个正方形1000 x 1000,但是照片通常不是正方形的。您可以将其从实际大小裁剪为正方形,然后将其缩小为250 x 250,但由于PHP没有眼睛,因此您无法确定是否得到了图片中的好部分。