Image Rotation Problem using imagerotate

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bensaunders9
Forum Newbie
Posts: 1
Joined: Fri Mar 16, 2007 10:23 am

Image Rotation Problem using imagerotate

Post by bensaunders9 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello Folks

The code posted below I found on the internet, and I've been using in my gallery to rotate pictures. It works very well except that when an image is rotated it appears to shift the picture down below the picture axis and show a black area at the top of the picture (the same size as the amount of picture lost below the axis). If anyone can see where I've gone wrong, please let me know.

Thanks

Ben   

Da Code:

Code: Select all

function rotatepix($sourcefile, $degrees, $id) {
    // make source file absolute
    $sourcefile="http://www.mydomain.com/".$sourcefile;

    $img_size = getimagesize($sourcefile);
    $x = $img_size[0];
    $y = $img_size[1];
    if ($x > $y)
    {
        $dst_x = 0;
        $dst_y = $x - $y;
        $newd = $x;
    }else{
        $dst_x = $y - $x;
        $dst_y = 0;
        $newd = $y;
    }
    $src_img = imagecreatefromjpeg($sourcefile);
    $dst_img = imagecreatetruecolor($newd,$newd);
 
    if ((($x > $y) && ($degrees < 180)) || (($y > $x) && ($degrees > 180)))
        imagecopyresampled($dst_img,$src_img,0,0,0,0,$x,$y,$x,$y);
    else
        imagecopyresampled($dst_img,$src_img,$dst_x,$dst_y,0,0,$x,$y,$x,$y);
 
    $rotated_img = imagerotate($dst_img, $degrees,0);
 
    if($degrees != 180)
    {
        // 90 counterclockwise or clockwise
        $final_img = imagecreatetruecolor($y,$x);
        imagecopyresampled($final_img,$rotated_img,0,0,0,0,$y,$x,$y,$x);
    }else{
        // 180 degrees
        $final_img = imagecreatetruecolor($x,$y);
        imagecopyresampled($final_img,$rotated_img,0,0,0,0,$x,$y,$x,$y);
    }
    
    // this retrieves the actual image data from the output buffer and returns it (imagejpeg just returns boolean)
    ob_start();
     imagejpeg($final_img, NULL,100);
    $data = base64_encode(ob_get_contents());
    ob_end_clean();
    return $data;
 }

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply