【问题标题】:PHP: Black strip on image while using GD libraryPHP:使用 GD 库时图像上出现黑条
【发布时间】:2014-12-14 17:04:24
【问题描述】:

我对 PHP 中的 GD 库有一点问题 - 我调整了图像大小,然后我想将其裁剪为 320 像素(宽度)/240 像素(高度)。让我说调整大小的图像是 320px/300px。当我裁剪它时,图像底部会出现一个 1 像素的黑色条带 - 我不知道为什么。 我正在使用imagecropimagecreatefromjpegimagecopyresampled

示例如下:

感谢您的宝贵时间。

代码

$filename = '../store/projects/project-123.jpg';
$mime = mime_content_type($filename);
list($w, $h) = getimagesize($filename);

$prop = $w / $h;
$new_w = 0;
$new_h = 0;

if ($prop <= 4/3) {
    $new_w = 320;
    $new_h = (int)floor($h*($new_w/$w));
} else {
    $new_h = 240;
    $new_w = (int)floor($w*($new_h/$h));
}

$thumb = imagecreatetruecolor($new_w, $new_h);

if (strcmp($mime,'image/png') == 0) {
    header('Content-Type: image/png');
    $source = imagecreatefrompng($filename);
} else {
    header('Content-Type: image/jpeg');
    $source = imagecreatefromjpeg($filename);
}

imagecopyresampled($thumb, $source, 0, 0, 0, 0, $new_w, $new_h, $w, $h);

$filename = '../store/projects-thumbs/project-123.jpg';

$crop_data = array('x' => 0 , 'y' => 0, 'width' => 320, 'height'=> 240);
$thumb = imagecrop($thumb, $crop_data);

imagejpeg($thumb, $filename, 100);  


imagedestroy($thumb);
imagedestroy($source);

【问题讨论】:

  • 请出示您的代码
  • 我编辑了我的帖子——代码在上面:)

标签: php image-processing gd


【解决方案1】:

imagecrop() 有一个 known bug 会导致添加黑色底边框。

您可以使用imagecopyresized() 解决此问题。将my answer 与另一个SO question 联系,寻求imagecrop() 的替代方案。

【讨论】:

    猜你喜欢
    • 2011-02-11
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 2016-09-02
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多