View Single Post
  #5 (permalink)  
Old 08-16-03, 03:47 AM
borgo borgo is offline
Newbie Coder
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Allright, this is it...

PHP Code:

<?


    
//**************************************
    // Name: Image rotation using GD extension
    // Description:A function with the intention of rotating a Jpeg image 90° Clockwise, Counterclockwise and 180°(Flip).
    // By: Karl Blessing
    //
    //
    // Inputs:Source file ( path to the source )
    //Destination ( path to the destination, can be same as source if you want to overwrite )
    //degrees (integer value of 90(counterclockwise), 270(clockwise) or 180)
    //
    // Returns:does not return a value, will save the destination, no indication if successful is returned.
    //
    //Assumes:When writing this I found various behaviors between images with greater x than y's and so forth. Thus is why all the condiotionals. The function has not been tested for images with same x and y dimensions, and will not likely work. 
    //IMPORTANT:
    //You must have PHP 4.3.* or above for this function to work, a PHP version lower will not work. I was using PHP 4.3.2 for Win32, with GD Extension version 2 ( bundled with PHP ).
    //
    //Side Effects:None
    //This code is copyrighted and has limited warranties.
    //Please see [url]http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.1074/lngWId.8/qx/vb/scripts/ShowCode.htm[/url]
    //for details.
    //**************************************
    
    
function rotatePix($sourcefile$targetfile$degrees)
    {
        
$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);
        }
        
ImageJPEG($final_img$targetfile);    
    }

        
rotatePix("foto.jpg""foto.jpg""270");

?>

Last edited by borgo; 08-16-03 at 03:50 AM.
Reply With Quote