PHP code for cropping images
|
The following example shows how to crop the 100x100 part in the upper left corner of the image. You can modify the crop range by modifying the values of $ src_x, $ src_y, $ src_w, and $ src_h.
- $ Filename = "test.jpg ";
- List ($ w, $ h, $ type, $ attr) = getimagesize ($ filename );
- $ Src_im = imagecreatefromjpeg ($ filename );
- $ Src_x = '0'; // begin x
- $ Src_y = '0'; // begin y
- $ Src_w = '000000'; // width
- $ Src_h = '000000'; // height
- $ Dst_x = '0'; // destination x
- $ Dst_y = '0'; // destination y
- $ Dst_im = imagecreatetruecolor ($ src_w, $ src_h );
- $ White = imagecolorallocate ($ dst_im, 255,255,255 );
- Imagefill ($ dst_im, 0, 0, $ white );
- Imagecopy ($ dst_im, $ src_im, $ dst_x, $ dst_y, $ src_x, $ src_y, $ src_w, $ src_h );
- Header ("Content-type: image/png ");
- Imagepng ($ dst_im );
- Imagedestroy ($ dst_im );
- ?>
|
PHP