0

This function works perfectly when defining the rotation degrees in text such as "-90" but when I try to us the degrees parameter as a $variable the image does not rotate. Any ideas? thanks in advance.

 function compress_image($source_url, $destination_url, $quality) {

    $info = getimagesize($source_url);

        if ($info['mime'] == 'image/jpeg')  
                $image = imagecreatefromjpeg($source_url);

        elseif ($info['mime'] == 'image/gif')
                $image = imagecreatefromgif($source_url);

    elseif ($info['mime'] == 'image/png')
                $image = imagecreatefrompng($source_url);


                $rotate = imagerotate($image,"-90",0); //Does Work
                $degrees = "-90"
                $rotate = imagerotate($image,$degrees,0); //Does Not Work
                $image = $rotate;

        imagejpeg($image, $destination_url, $quality);
    return $destination_url;
}
Ricky T
  • 243
  • 1
  • 13

3 Answers3

0

you can rotate as follow:

resource imagerotate ( resource $image , float $angle , int $bgd_color [, int $ignore_transparent = 0 ] )

Rotates the image image using the given angle in degrees.

The center of rotation is the center of the image, and the rotated image may have different dimensions than the original image.

Parameters :-

image:

An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().

angle

Rotation angle, in degrees. The rotation angle is interpreted as the number of degrees to rotate the image anticlockwise.

bgd_color

Specifies the color of the uncovered zone after the rotation

ignore_transparent

If set and non-zero, transparent colors are ignored (otherwise kept).

Example:

<?php
 // File and rotation
 $filename = 'test.jpg';
 $degrees = 180;

 // Content type
 header('Content-type: image/jpeg');

 // Load
 $source = imagecreatefromjpeg($filename);

 // Rotate
 $rotate = imagerotate($source, $degrees, 0);

 // Output
 imagejpeg($rotate);

 // Free the memory
 imagedestroy($source);
 imagedestroy($rotate);
?>
Amol Raje
  • 928
  • 3
  • 9
  • 16
  • See Also:https://stackoverflow.com/questions/11259881/how-to-rotate-image-and-save-the-image – Amol Raje Dec 09 '17 at 05:04
  • Ok Amol, I see what you mean. $degrees="-90" does not work, but $degrees=90 does. With that said, I am POSTing name="degrees" in a form on a different page then $degrees=$_REQUEST["degrees"] on my php script page. I am having in issue with variable types then. How to make my $degrees variable a valid type for imagerotate command – Ricky T Dec 09 '17 at 05:29
  • all rotation angles are between 0 and 360. – Amol Raje Dec 09 '17 at 05:40
  • tried positive 90 and 180 and still nothing. This is weird, it works if I hard code the degrees, but won't let me assign to variable – Ricky T Dec 09 '17 at 05:50
  • also I have tried $degress = intval($degrees); and no go – Ricky T Dec 09 '17 at 05:51
  • as you say POSTing name="degrees" in a form on a different page ..and it works if you hardcode.. may be variable not posted to another page .. – Amol Raje Dec 09 '17 at 05:56
  • check the variable on page where you passing..try to print and check the value – Amol Raje Dec 09 '17 at 05:58
  • I already checked that, I echo $_REQUEST['degrees'] and it is valid. Is there some rule i am unaware of when using variables in a function. I have tried multiple ideas, and read PHP manual. This should work. Is this possibly a bug in PHP? – Ricky T Dec 09 '17 at 19:27
  • The purpose for this is the following: When taking image from camera on cell phone and uploading to web site, it rotates the image 90 degrees counter clockwise. When uploading image from a PC or existing image, the image displays properly and needs no adjustment. – Ricky T Dec 09 '17 at 19:33
0

I tried you code. You code works fine totally except there is a mistake at:

$degrees = "-90"

It should be:

$degrees = "-90";

Hope it helps.

Phil
  • 1,444
  • 2
  • 10
  • 21
  • Sorry Phil, i was only using $degrees = "90" for example purposes and yes I see the typo. But in reality im passing $degrees from a different page as I mentioned above in my last comment. – Ricky T Dec 09 '17 at 05:38
  • That's weird then. Maybe you could echo your degree value and check what happens. – Phil Dec 09 '17 at 06:04
0

I was not passing the $degree variable into the function. The code below is correct.

function compress_image($source_url, $destination_url, $quality, $degree) {

$info = getimagesize($source_url);

    if ($info['mime'] == 'image/jpeg')  
            $image = imagecreatefromjpeg($source_url);

    elseif ($info['mime'] == 'image/gif')
            $image = imagecreatefromgif($source_url);

elseif ($info['mime'] == 'image/png')
            $image = imagecreatefrompng($source_url);

            $rotate = imagerotate($image,$degrees,0);
            $image = $rotate;

    imagejpeg($image, $destination_url, $quality);
return $destination_url;
}
Ricky T
  • 243
  • 1
  • 13