Question

I'm trying to use the following code to rotate and save an image, but it doesn't appear to be doing anything, nor is it spitting out any errors?

Here's my code:

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

$path   = '/Volumes/yoda/websites/zepel.website.2013/images/blog/display';
$file   = '10.jpg';
$degrees    = 90;

$filename   = $path."/".$file;

$source     = imagecreatefromjpeg($filename) or notfound();
$rotate     = imagerotate($source,$degrees,0);

imagejpeg($filename,$rotate);

imagedestroy($source);
imagedestroy($rotate);

Any help would be greatly appreciated. My images folder is set to 777 too. I can't seem to figure out why it's not working?

Was it helpful?

Solution

Give this a whirl: (tested)

<?php

$path   = '/Volumes/yoda/websites/zepel.website.2013/images/blog/display';
$file   = '10.jpg';
$degrees = 90;

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

$filename = $path . "/" .$file;

$source = imagecreatefromjpeg($filename) or notfound();
$rotate = imagerotate($source,$degrees,0);

imagejpeg($rotate);
imagedestroy($source);
imagedestroy($rotate);

?>

OTHER TIPS

the problem is in the imagejpg function. it shoul be, like this:

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

chek imagerotate manual link

EDIT

the problem, is that you are showing the image in the browser and algo another text, thats is why you get that "strange text", try to save the image whith:

imagejpeg($rotate, "test.jpeg");

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

You can use this for rotate image

$image = 'images/me.jpg';



// Set the path to the image to rotate
$input_image = $image;
//How many degrees you wish to rotate
$degrees = 90;
// Create the canvas

            $info = getimagesize($image);

            if($info['mime']) {
                if($info['mime']=='image/gif'){
                    $canvas = imagecreatefromgif($image);
                    // Rotates the image
                    $rotate = imagerotate( $canvas, $degrees, 0 ) ;
                    // Save the image as output.jpg
                    imagegif( $rotate, $image );
                }elseif($info['mime']=='image/jpeg'){
                    $canvas = imagecreatefromjpeg($image);
                    // Rotates the image
                    $rotate = imagerotate( $canvas, $degrees, 0 ) ;
                    // Save the image as output.jpg
                    imagejpeg( $rotate, $image );

                }elseif($info['mime']=='image/png'){
                    $canvas = imagecreatefrompng($image);
                    // Rotates the image
                    $rotate = imagerotate( $canvas, $degrees, 0 ) ;
                    // Save the image as output.jpg
                    imagepng( $rotate, $image );
                }
            }
// Clear the memory of the tempory image
imagedestroy( $canvas );

?>
<img src="<?=$image?>">

This worked for me, (change for png ) You may use header if header data has not gone out yet. If header has gone out, use " /> If you do not add the time, your browser may only show you the cached version, not the rotated version you just updated. Be aware using lastmod causes higher page load time, especially if the image is large, but if the user wants to see what happened, you will have to use this.

$rotateFilename="path to file (not http)";
$degrees = 90;

if($fileType == 'jpg' || $fileType == 'jpeg'){
 // if header data has not gone out yet...  header('Content-type: image/jpeg');
$source = imagecreatefromjpeg($rotateFilename);
 // Rotate
$rotate = imagerotate($source, $degrees, 0); //do the deed
imagejpeg($rotate,$rotateFilename); //saves the file


// Free the memory
imagedestroy($source);
imagedestroy($rotate);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top