Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Created December 19, 2012 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdeepkhalsa/4335807 to your computer and use it in GitHub Desktop.
Save jasdeepkhalsa/4335807 to your computer and use it in GitHub Desktop.
How to crop an image using PHP compiled with the GD library
<?php
// Example from http://php.net/manual/en/function.imagecopy.php
// Usage:
// bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
// Copy a part of src_im onto dst_im starting at the x,y coordinates src_x, src_y with a width of src_w and a height of src_h. The portion defined will be copied onto the x,y coordinates, dst_x and dst_y.
// Create image instances
$src = imagecreatefromgif('php.gif');
$dest = imagecreatetruecolor(80, 40);
// Copy
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);
// Output and free from memory
header('Content-Type: image/gif');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment