John Davidson

php - Image Rotates 90 degrees left when resized

0 comments
Message:


I have a resize script and it makes every image uploaded 300x300. It works fine when something from computer or mobile is uploaded. The problem is when user decides to take a picture from camera: After the resize script image rotates 90 degrees to the left and it is a problem because it should be displayed correctly without rotating. I don't know the reason here is the script:


    function resize_image($path, $width, $height, $update = false) {
$size = getimagesize($path);// [width, height, type index]
$types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
if ( array_key_exists($size['2'], $types) ) {
$load = 'imagecreatefrom' . $types[$size['2']];
$save = 'image' . $types[$size['2']];
$image = $load($path);
$resized = imagecreatetruecolor($width, $height);
$transparent = imagecolorallocatealpha($resized, 0, 0, 0, 127);
imagesavealpha($resized, true);
imagefill($resized, 0, 0, $transparent);
imagecopyresampled($resized, $image, 0, 0, 0, 0, $width, $height, $size['0'], $size['1']);
imagedestroy($image);
return $save($resized, $update ? $path : null);
}
}
if(!resize_image($_FILES["fileToUpload"]["tmp_name"], 300, 300, true))
{
exit();
}


My Answer:
> The issue you are facing is likely due to the EXIF data stored in the image file. When a picture is taken from a camera, the orientation information is stored in the EXIF data. This information is used by some image viewers to display the image correctly.

To fix the rotation issue, you can use the PHP `exif_read_data` function to read the EXIF data of the image and then rotate the image accordingly. Here is an updated version of your `resize_image` function that takes care of the rotation issue:

php
function resize_image($path, $width, $height, $update = false) {
$size = getimagesize($path);
$types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
$type = $types[$size[2]];

$image = null;
switch ($type) {
case 'gif':
$image = imagecreatefromgif($path);
break;
case 'jpeg':
$image = imagecreatefromjpeg($path);
break;
case 'png':
$image = imagecreatefrompng($path);
break;
}

$exif = exif_read_data($path);
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($image, 180, 0);
break;
case 6:
$image = imagerotate($image, -90, 0);
break;
case 8:
$image = imagerotate($image, 90, 0);
break;
}
}

$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);

switch ($type) {
case 'gif':
imagegif($new_image, $path);
break;
case 'jpeg':
imagejpeg($new_image, $path);
break;
case 'png':
imagepng($new_image, $path);
break;
}

imagedestroy($image);
imagedestroy($new_image);
}


This updated function reads the EXIF data of the image and rotates it accordingly before resizing it. This should fix the rotation issue you are facing when uploading images taken from a camera.

Rate this post

4 of 5 based on 4504 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us