si-blog

Rotating an image

Posted Jan 26, 2005 in PHP.

I have started playing with the GD Library functions available to PHP for the first time. One of the things I would like to do is rotate a JPEG through 270 degrees, and then save it.

Performing the rotation itself was easy:

$filename = "test.jpg";
$source = imagecreatefromjpeg($filename);
$rotate = imagerotate($source,270,0);
imagejpeg($rotate);

I thought I would be able to save the file (actually overwrite the old file) by simply altering the imagejpeg() function:

imagejpeg($rotate,$filename,100);

Unfortunately, this does not work. Does anyone know how this seemingly simple task might be accomplished?

Update: The technique I described does seem to be working now. I am not exactly sure what I screwed-up before, but it seems that there was an error in my testing example. My bad.

Comments

  1. Gravatar

    In the past I have used output buffering to capture the output and then send the resulting string to a file.

    Posted by Justin on Jan 26, 2005.

  2. Gravatar

    I can't believe there isn't a less convoluted solution than that. I'm sure I just missing something really obvious.

    Posted by Simon Jessey on Jan 26, 2005.

  3. Gravatar

    Well it seems that you are using imagejpeg() correctly. If you are using this in a mod_php scenario, I'd guess you don't have permission to write to the directory you are trying to.

    Of course "doesn't work" as a bug report doesn't give us much to go on :).

    Posted by Jim on Jan 26, 2005.

  4. Gravatar

    Sorry, Jim. That was rather lame of me. Please see the updated entry.

    Posted by Simon Jessey on Jan 26, 2005.