|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-10-24 04:30 UTC] 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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 00:00:01 2025 UTC |
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; } }