Php Image Crop Function

How can I crop an image using php? I can use imagecopy() but the manual is very confusing. I have 4 coordinates, x1 y1 and x2 y2.

Someone explain to me how to use imagecopy() and where to put the variables because it just doesn’t make sense to me. Save me from my insanity. :frowning:

PHP.net is a great place to learn about functions and their arguments. See this:

http://www.php.net/imagecopy

I know that because I said:

…but the manual is very confusing.

I asked for someone to explain the manual because it doesn’t make it clear enough. Can someone do that?

If you scroll down on the manual you will see a bunch of explanations and implementations. This one should work for you.


Basic way to implement a "crop" feature : given an image (src), an offset (x, y) and a size (w, h).

crop.php :
<?php
$w=$_GET['w'];
$h=isset($_GET['h'])?$_GET['h']:$w;    // h est facultatif, =w par d&#233;faut
$x=isset($_GET['x'])?$_GET['x']:0;    // x est facultatif, 0 par d&#233;faut
$y=isset($_GET['y'])?$_GET['y']:0;    // y est facultatif, 0 par d&#233;faut
$filename=$_GET['src'];
header('Content-type: image/jpg');
header('Content-Disposition: attachment; filename='.$src);
$image = imagecreatefromjpeg($filename); 
$crop = imagecreatetruecolor($w,$h);
imagecopy ( $crop, $image, 0, 0, $x, $y, $w, $h );
imagejpeg($crop);
?>

Call it like this :

<img src="crop.php?x=10&y=20&w=30&h=40&src=photo.jpg">