天天看点

php gd 图像翻转,PHP – 使用GD旋转图像会产生黑色边框

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

php gd 图像翻转,PHP – 使用GD旋转图像会产生黑色边框

原件看起来像这样:

$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);按照Black background when rotating image with 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);

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