繁体   English   中英

PHP - 使用GD旋转图像会产生黑色边框

[英]PHP - Rotate image with GD gives black borders

我正在尝试旋转并保存图像。 轮换基于EXIF数据。 我尝试了以下内容,它们都围绕着它呈现黑色边框:

在此输入图像描述

原件看起来像这样:

$orientation = array_values([0, 0, 0, 180, 0, 0, -90, 0, 90])[@exif_read_data($imagePath)['Orientation'] ?: 0];

$source = imagecreatefromjpeg($imagePath);
$resource = imagerotate($source, $orientation, 0);
imagejpeg($resource, $image, 100);

我也试过添加imagealphablending($resource, true); imagesavealpha($resource, true); 使用PHP旋转图像时黑色背景中提出,但无济于事; 边界仍然存在。

然后我尝试使用imagecreatetruecolor()创建图像:

$imageSizes = getimagesize($image);
$oldWidth  = $imageSizes[0];
$oldHeight = $imageSizes[1];

$orientation = array_values([0, 0, 0, 180, 0, 0, -90, 0, 90])[@exif_read_data($image)['Orientation'] ?: 0];

$source = imagecreatefromjpeg($imagePath);
$resource = imagerotate($source, $orientation, 0);

$newWidth  = $oldWidth;
$newHeight = $oldHeight;

if ($orientation !== 180 && $orientation !== 0) {
    $newWidth  = $oldHeight;
    $newHeight = $oldWidth;
}

$imageResized = imagecreatetruecolor($newWidth, $newHeight); 

imagecopyresampled ($imageResized, $resource, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight); 
imagejpeg($imageResized, $image, 100);

但我似乎无法让它发挥作用。 有人能帮我这个吗?

我今天在PHP for Windows中发现了这个问题。 当你进行0度或360度旋转时,边框似乎只会被添加。 我没有180度旋转的边框。 因此,只需检查方向是否为非零,并且仅在必要时旋转。

... if ($orientation !== 0) $resource = imagerotate($source, $orientation, 0); else $resource = $source; end ...

这不是一个答案..它可能对你有帮助,也可能没有

我测试了你的代码,并为我工作正常 在此输入图像描述

另外,使用你的形象它不会给我你的问题。

正如我所看到的,您的结果图像,黑色边框,与原始图像有所不同..看看左边框,顶部的狗是弯曲的,这个区别是黑色边框

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM