SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    GD: Cropping with ImageCopyResampled

    Hey everyone,

    I'm relatively new to PHP, and I'm trying to resize an image and crop it down to 2x3 using ImageCopyResampled.

    Here's the code I'm using:
    PHP Code:
    $wh_ratio $width $height;
        if (
    $wh_ratio 2) {
            
    // Crop horizontal
            
    $crop_h = ($width - ($height 2)) / 2;
            
    $crop_v 0;
        }
        elseif (
    $wh_ratio 2) {
            
    // Crop vetical
            
    $crop_h 0;
            
    $crop_v = ($height - ($width 3)) / 2;
        }
        else {
            
    // No crop
            
    $crop_h 0;
            
    $crop_v 0;
        }
        
    // Create thumbnail
        
    $source $function ($tmp_name);
        
    $destination_thumb imagecreatetruecolor (14395);
        
    imagecopyresampled ($destination_thumb$source00$crop_h$crop_v14395$width$height);
        
    imagejpeg ($destination_thumb$images_dir.'/thumbs/'.$new_name100) or $error 'The thumbnail was not saved to the server.';
        
    // Free the memory
        
    imagedestroy ($source);
        
    imagedestroy ($destination_thumb); 
    The resizing is working fine, but without cropping it distorts the image. When I use the code above it seems to reposition the distorted image $crop_h/$crop_v pixels, rather than cutting off $crop_h/$crop_v pixels on both sides.

    Am I using this wrong? How could I get the desired effect? Thanks in advance for any ideas. -Matt

    Edit: I attached photos showing the problem... the first one is uncropped and distorted, the second one is using the code above.
    Attached Images Attached Images
    Last edited by MaverickDesign; Nov 8, 2006 at 23:56.

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *bump*

  3. #3
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Don't forget to decrease the width and height as you do the coordinates:
    PHP Code:
    imagecopyresampled ($destination_thumb$source00$crop_h$crop_v14395$width-2*$crop_h$height-2*$crop_v); 
    Saul

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    beautiful. it works perfectly.

    I'm voting for you in the sitepoint awards!
    Attached Images Attached Images

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Glad it worked and thanks
    Saul


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
  •