I am trying to rotate and save an image. The rotation is based on the EXIF data. I have tried the following, which all give a black border around it:
Where the original looks like this:
$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);
I have also tried adding imagealphablending($resource, true);
and imagesavealpha($resource, true);
as proposed in Black background when rotating image with PHP, but to no avail; the border remains.
Then I tried creating the image with 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);
But I just can't seem to get it work. Is anyone able to help me with this?