SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Cropping photos

  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cropping photos

    Hey,

    I am using the following code to crop photos.. But I would like to crop the from the center out aposed to from the top left corner.. How can I do that?

    PHP Code:
        // Crop Thumbs
        
    $width 70;
        
    $height 70;
        
    $dimensions getimagesize($images_dir."/tb_".$filename);
        
    $canvas imagecreatetruecolor($width,$height);
        
    $piece imagecreatefromjpeg($images_dir."/tb_".$filename);

        
    $newwidth 70;
        
    $newheight 70;
        
    $cropLeft = ($newwidth2) - ($width2);
        
    $cropHeight = ($newheight2) - ($height2);
        
        
    imagecopyresized($canvas$piece0,0$cropLeft$cropHeight$width$height$newwidth$newheight);
        if (
    imagejpeg($canvas,$images_dir."/tb_".$filename,100)) {
        echo 
    'Image crop successful';
        } else {
        echo 
    'Image crop failed';
        }
        
    imagedestroy($canvas);
        
    imagedestroy($piece); 
    Thanks

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    For cropLeft and cropHeight I think you need to calculate the coordinates on the source image you want to crop (the top-left corner).

    Something like this (untested):

    PHP Code:
    $w $dimensions[0]; // get width of original image
    $h $dimensions[1]; // get height of original image 

    $cropLeft = ($w/2) - ($newwidth/2);
    $cropHeight = ($h/2) - ($newheight/2); 

  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh yes I see now.. Works a treat..

    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •