Bug #77052 [Fbk->Nab]: imagescale() rotates image anti-clockwise by 90 degree after scaling
Edit report at https://bugs.php.net/bug.php?id=77052&edit=1
ID: 77052
Updated by: requinix@php.net
Reported by: hyh19962008 at gmail dot com
Summary: imagescale() rotates image anti-clockwise by 90
degree after scaling
-Status: Feedback
+Status: Not a bug
Type: Bug
Package: GD related
Operating System: Windows Server 2012 R2
PHP Version: 7.2.11
Block user comment: N
Private report: N
New Comment:
Glad you have it solved.
Previous Comments:
------------------------------------------------------------------------
[2018-10-26 04:47:59] hyh19962008 at gmail dot com
Thanks! That solved the problem.
I think I can post some code to help those who have encountered the same problem:
First, open the exif extension in your php.ini by adding:
extension=exif;
Then:
$filename="C:\inetpub\wwwroot\\apc\\img\\1.JPG";
$exif=exif_read_data($filename);
$pic=imagecreatefromjpeg($filename);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$pic = imagerotate($pic,90,0);
break;
case 3:
$pic = imagerotate($pic,180,0);
break;
case 6:
$pic = imagerotate($pic,-90,0);
break;
}
}
------------------------------------------------------------------------
[2018-10-24 05:42:30] requinix@php.net
The image you expect to see is not the actual image. It has been rotated for viewing according to
EXIF data embedded in the file. You must read that orientation data to determine the
"correct" image alignment, then base your rotation accordingly.
https://www.google.com/search?q=php+exif+image+orientation
------------------------------------------------------------------------
[2018-10-24 04:30:32] hyh19962008 at gmail dot com
Description:
------------
When scaling an image using the GD function imagescale(), the image is rotated anti-clockwise by 90
degree automatically.
This happens most frequently when the given image has a greater height than width, and was shot by
camera or mobile.
Test script:
---------------
<?php
$filename="C:\inetpub\wwwroot\\apc\\img\\1.JPG";
$pic=imagecreatefromjpeg($filename);
$ratio=imagesy($pic)/imagesx($pic); //=height/width
$height=136;
$width=136/$ratio;
$pic=imagescale($pic,$width,$height);
imagejpeg($pic,"min//1.jpeg",86);
imagedestroy($pic);
?>
Expected result:
----------------
image with the following w/h
width=136/ratio;
height=136;
height is greater than width, the image should be presented vertically.
Actual result:
--------------
image with the following w/h
width=136;
height=136/ratio;
width is greater than height, the image is presented horizontally, because it's rotated
anti-clockwise by 90 degree comparing to the original image.
I have uploaded the script and sample picture to my website:
https://www.swuplaf.cn/abc/test.php //script
https://www.swuplaf.cn/abc/img/1.JPG
//original picture
https://www.swuplaf.cn/abc/min/1.jpeg
//scaled picture
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77052&edit=1
Thread (4 messages)