subscribe

Rotating an image, retaining the original size

UPDATE: Ignore this post, this ranks number one in uselessness on my blog so far.. ImageRotate already does what I tried to achieve, and I didn't try it out, I just relied on a confusing line in the php manual.. For details : read pierre's response on this blog

.

Here's a small code snippet which allows you to rotate images, while the dimensions are being retained (width becomes height, height becomes width).

The image argument should be an image handler, created by for example imagecreatefromjpeg(), or imagecreatetruecolor().

The rotation argument expects 0, 1, 2, 3, for 0 degrees, 90 degrees, 180 degrees or 270 degrees rotation. Unlike php's imagerotate function, this is clockwise..

<?php

function imageSmartRotate(&$image,$rotation) {

    $width = imagesx($image);
    $height = imagexy($image);

    switch ($rotation) {

        case 0 : return true;
        case 2 : $image = imagerotate($image,180); return true;
        case 1 :
        case 3:
            // checking the biggest coordinate (width or height)
            $maxXY = $width>$height?$width:$height;

            // create our new canvas
            $rImg = imagecreatetruecolor($maxXY,$maxXY);

            // copy our image
            imagecopy($rImg,$image,0,0,0,0,$width,$height);

            // rotate it (this function is counter clockwise (and counter intuitive))
            $rImg = imagerotate($Img,(360-($rotate*90)),0);

            // swap width/height
            $h = $width;
            $width = $height;
            $height = $width;

            // create the final canvas, with the proper dimensions
            $image = imagecreatetruecolor($width,$height);

            // if rotated 90 degrees the image will be in the bottom right corner
            // if rotated 270 degrees the image will be in the top left corner

            if ($rotate == 1) {
                imagecopy($image,$rImg,0,0,$max-$width,$max-$height,$maxXY,$maxXY);
            } else {
                imagecopy($image,$rImg,0,0,0,0,$width,$height);
            }
            return true;

        default :
            return false;


    }

}

?>

This method does take a bit more memory than usual, because it needs to create 2 other images of the same size to complete.. Are there more efficient ways to do this?

Web mentions

Comments

  • Pierre

    Pierre

    "Are there more efficient ways to do this?"

    Yes, if you have imagerotate, use it, multiple of 90° are special cases and are fast, it is a two lines loop in C. If you don't like the clockwise argument, that's easy to change when you call imagerotate no?

    btw, the function should be name imageflip in this case, it does not really "rotate at any angle" :)
  • Evert

    Evert

    Pierre,

    ImageFlip would be a better name :)

    The reason I needed this is the following, read this quote from the manual:

    "The center of rotation is the center of the image, and the rotated image is scaled down so that the whole rotated image fits in the destination image - the edges are not clipped."

    My function does not scale the image down.. if an image is 320x240 and you rotate it 90 degrees, it will end up being 240x320.. with imagerotate() the image will still be 320x240, will be scaled down and have black (or any other color) borders.
  • Pierre

    Pierre


    I have to say that this sentence is confusion and does reflect how it works.

    For 90° multiples, it is really a image flip, no more (and it's fast), the dimension are kept.

    For custom angles, the destination image will be as large as necessary to contain the rotated image.

    See my post here for an example:
    http://blog.thepimp.net/index.php/post/2007/02/06/imagerotate-little-confusions-in-the-manual-damned-I-cannot-say-RTFM
  • Tracy

    Tracy

    I'm Sorry,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,WHAT??
  • Evert

    Evert

    Pierre,

    Thats actually good news =) will reduce the amount greatly..

    I feel stupid now for not just trying it out first..

    Evert