【问题标题】:Cropping a photo with PHP gets stretched用 PHP 裁剪照片会被拉伸
【发布时间】: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


【解决方案1】:

想一想你在说什么。

如果您更改图像的宽度,但没有按照与宽度完全相同的比例更改高度,则图像总是会失真。

虽然我没有实际测试过计算是否正确,但您有正确执行此操作的代码,然后您使用任意高度 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); }   
}

当然,宽度会根据原始图像的实际大小而有所不同。但是,如果您想停止失真,就必须让这种情况发生。

【讨论】:

  • 有没有办法将其设置为 250 x 250 并且不妥协?
  • 仅当原始图片尺寸的宽度和高度相同时,例如正方形 1000 x 1000,但照片通常不是正方形。您可以将其从实际大小裁剪为正方形,然后将其缩小到 250 x 250,但由于 PHP 没有眼睛,您无法确定您是否得到了图片的好位置。
猜你喜欢
  • 2016-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 2017-05-08
相关资源
最近更新 更多