3

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?

1
  • 1
    Placement of header('Content-type: image/jpeg'); is important. Try my answer below, tested and working. Commented Jun 11, 2013 at 3:56

4 Answers 4

3

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);

?>
Sign up to request clarification or add additional context in comments.

7 Comments

I changed it to look like that, I'm still getting an output like this: ����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), default quality ��C and so forth..
My rotate script is inside a function, is that causing it to break?
@SoulieBaby There's an issue somewhere on your server or script. Like I stated, the above code was successfully tested on my server running PHP 5.2.3
Ok I've managed to fix it, I'm also using joomla, so I had to use Joomla's way of adding the content-type header.. $document =& JFactory::getDocument(); $document->setMimeEncoding('image/jpeg'); Thanks so much for your help :)
@SoulieBaby Right on, glad to hear it worked out, cheers and you're welcome.
|
1

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');

2 Comments

I'm getting an output like this now: ����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), default quality ��C $.' ",#(7),01444'9=82<.342��C 2!!22222222222222222222222222222222222222222222222222���"�� ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� ���w!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�
thats is cool, is the image it self.. in another format.. you need to put header('Content-type: image/jpeg'); before the imagejpeg function..
0
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?>">

Comments

0

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);
}

1 Comment

If header has gone out, use < img src="http_path.filename?lastmod=<?php time()'?>" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.