我想旋转并保存图像。旋转是基于EXIF数据。我曾尝试以下,而这一切给黑色边框周围: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);
但我似乎无法得到它的工作。有人能帮助我吗?
谢谢您的测试。这只能意味着我正在工作的应用程序会改变其他地方的图像。我会试着找出在哪里,但至少我知道这个代码正在工作:-) –