Posted By

welancers on 08/18/12


Tagged

php image crop


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tux-world


Crop Image and Display it using PHP


 / Published in: PHP
 

Displays a selected area of the image. In this example provided, it picks up from the upper left 100px x 100px

  1. $filename= "my_picture.jpg";
  2. list($w, $h, $type, $attr) = getimagesize($filename);
  3. $src_im = imagecreatefromjpeg($filename);
  4.  
  5. $src_x = '0'; // begin x
  6. $src_y = '0'; // begin y
  7. $src_w = '100'; // width
  8. $src_h = '100'; // height
  9. $dst_x = '0'; // destination x
  10. $dst_y = '0'; // destination y
  11.  
  12. $dst_im = imagecreatetruecolor($src_w, $src_h);
  13. $white = imagecolorallocate($dst_im, 255, 255, 255);
  14. imagefill($dst_im, 0, 0, $white);
  15.  
  16. imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
  17.  
  18. header("Content-type: image/png");
  19. imagepng($dst_im);
  20. imagedestroy($dst_im);

Report this snippet  

You need to login to post a comment.