php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77052 imagescale() rotates image anti-clockwise by 90 degree after scaling
Submitted: 2018-10-24 04:30 UTC Modified: 2018-10-26 04:49 UTC
From: hyh19962008 at gmail dot com Assigned:
Status: Not a bug Package: GD related
PHP Version: 7.2.11 OS: Windows Server 2012 R2
Private report: No CVE-ID: None
 [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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-10-24 05:42 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2018-10-24 05:42 UTC] 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-26 04:47 UTC] 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-26 04:49 UTC] requinix@php.net
-Status: Feedback +Status: Not a bug
 [2018-10-26 04:49 UTC] requinix@php.net
Glad you have it solved.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 06:01:30 2024 UTC