php resizing image on upload rotates the image when i don't want it to
This is my code, resize image and don't rotates images with exif inside
PHP must be enabled: extension=php_mbstring.dll extension=php_exif.dll if error, See more: PHP:exif_read_data() not defined
function CreateThumbnail($pic,$thumb,$thumbwidth, $quality = 100)
{
$im1=ImageCreateFromJPEG($pic);
//if(function_exists("exif_read_data")){
$exif = exif_read_data($pic);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$im1 = imagerotate($im1,90,0);
break;
case 3:
$im1 = imagerotate($im1,180,0);
break;
case 6:
$im1 = imagerotate($im1,-90,0);
break;
}
}
//}
$info = @getimagesize($pic);
$width = $info[0];
$w2=ImageSx($im1);
$h2=ImageSy($im1);
$w1 = ($thumbwidth <= $info[0]) ? $thumbwidth : $info[0] ;
$h1=floor($h2*($w1/$w2));
$im2=imagecreatetruecolor($w1,$h1);
imagecopyresampled ($im2,$im1,0,0,0,0,$w1,$h1,$w2,$h2);
$path=addslashes($thumb);
ImageJPEG($im2,$path,$quality);
ImageDestroy($im1);
ImageDestroy($im2);
}
The EXIF data is probably being ignored by this basic script.
Investigate whether your images have embedded rotation information using exif_read_data
, and then auto-correct the rotation with a function like this.
In your case,
- take the script I just linked,
- paste it above the first line of your code,
- change all the
$image->
bits to$resizeObj->
(vim-style grep:%s/image->/resizeObj->/g
)