1
votes

I am trying to rotate an image using php. I cannot seem to get the 'imagerotate' function to work. This is my code:

<?php
$degree = 45;

$img = imagecreatefromjpeg("mike.jpeg");

if($img == false){
echo 'imagecreatefromjpeg fail <br />';
}else{
echo 'imagecreatefromjpeg success <br />';
echo 'value $img: '.$img.'<br />'; //'Resource id #3' given.
}


$imgRotated = imagerotate($img, $degree, -1);// This is the problem!

if($imgRotated == false){
echo 'imgRotate fail <br />';
echo 'imgRotate value: '.$imgRotated.'<br />'; // '0' is returned every time
}else{
echo 'imgRotate success <br />';
}


 imagejpeg($imgRotated, 'mikeRotated.jpeg', 100); // ERROR: 'imagejpeg() expects parameter 1 to be resource, boolean given'
 ?>
 <img src="mike.jpeg"/>
 <img src="mikeRotated.jpeg"/>

This is my gd_info():

GD Version / bundled (2.1.0 compatible) FreeType Support / 1 FreeType Linkage / with freetype T1Lib Support / 1 GIF Read Support / 1 GIF Create Support / 1 JPEG Support / 1 PNG Support / 1 WBMP Support / 1 XPM Support / 1 XBM Support / 1 JIS-mapped Japanese Font Support /

2
What is the problem you are facing? - Icaro
the bottom line is i cannot rotate the image. so the code above was an attempt to find out where the problem lay. So the error i am getting is that quoted above in the code (see ERROR message) - dave
I'm just testing your code - should be able to give you an answer in a minute or two :D - Lucas
Is minus one a valid value as third argument? Seems to me it's only valid as transparency information in PNG files. - Bernhard Döbler

2 Answers

2
votes

Faced with this problem. All one to one. It turned out that it is impossible to send imagerotate batskground of -1 (rotating jpg-file). Put to 0, worked. The quality problems because of the choice of colors are not seen.

1
votes

I just tested your code, the only problem I found was that you wrote <img src="mikeRotated.jpg"/>, rather than mikeRotated.jpeg, which is the name of your picture.

Refer to the screenshot below, using your exact code (except for the above change):

screenshot 1

Not too sure why your code won't work. Could you provide a link to the image you are using?

Edit: used your image as well. Still not sure what's wrong... are you sure you've included the GD library inside php.ini? Refer to this link to check

screenshot 2