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

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

9b1d97449924f8daf791269f0075d217.png

原件看起来像这样:

$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);

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值