[英]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.