image editing
|crop image
simple image crop snippet.
make sure the directory where the cropped image is saved is writeable.
full source of crop image [ line 1 - 69 ] | download crop image
| 1 | <?php |
| 2 | $image = 'pics/whatever.jpg'; // the image to crop |
| 3 | $dest_image = 'pics/cropped_whatever.jpg'; // make sure the directory is writeable |
| 4 | $margin = 10; // to keep the image and layer in sync |
| 5 | if(!isset($_POST['step'])) $title = '1'; |
| 6 | else $title = $_POST['step']; |
| 7 | echo ' |
| 8 | <html> |
| 9 | <head> |
| 10 | <title>lixlpixel image crop | step '.$title.'</title> |
| 11 | </head> |
| 12 | <body style="margin: '.$margin.'px;">'; |
| 13 | if(!isset($_POST['tx']) && !isset($_POST['fx'])) |
| 14 | { |
| 15 | echo ' |
| 16 | <form method="post" action="'.$_SERVER['PHP_SELF'].'"> |
| 17 | <input type="image" src="'.$image.'"><p>'; |
| 18 | if(!isset($_POST['x'])) |
| 19 | { |
| 20 | echo ' |
| 21 | <input type="hidden" name="step" value="2"> |
| 22 | click to mark first corner'; |
| 23 | }else{ |
| 24 | echo ' |
| 25 | <input type="hidden" name="step" value="3"> |
| 26 | <input type="hidden" name="fx" value="'.$_POST['x'].'"> |
| 27 | <input type="hidden" name="fy" value="'.$_POST['y'].'"> |
| 28 | click to mark second corner | <a href="'.$_SERVER['PHP_SELF'].'">start over</a>'; |
| 29 | } |
| 30 | echo ' |
| 31 | </form> |
| 32 | '; |
| 33 | } |
| 34 | if(isset($_POST['fx'])) |
| 35 | { |
| 36 | echo ' |
| 37 | <form method="post" action="'.$_SERVER['PHP_SELF'].'"> |
| 38 | <input type="hidden" name="step" value="4"> |
| 39 | <input type="image" src="'.$image.'"> |
| 40 | <input type="hidden" name="tx" value="'.$_POST['fx'].'"> |
| 41 | <input type="hidden" name="ty" value="'.$_POST['fy'].'"> |
| 42 | <input type="hidden" name="width" value="'.($_POST['x']-$_POST['fx']).'"> |
| 43 | <input type="hidden" name="height" value="'.($_POST['y']-$_POST['fy']).'"><p> |
| 44 | <div style="position: absolute; |
| 45 | left:'.($_POST['fx']+$margin).'px; |
| 46 | top: '.($_POST['fy']+$margin).'px; |
| 47 | width: '.($_POST['x']-$_POST['fx']).'px; |
| 48 | height: '.($_POST['y']-$_POST['fy']).'px; |
| 49 | border: 1px solid #fff;"> |
| 50 | </div> |
| 51 | click image to crop rectangle | <a href="'.$_SERVER['PHP_SELF'].'">start over</a> |
| 52 | </form>'; |
| 53 | } |
| 54 | if(isset($_POST['tx'])) |
| 55 | { |
| 56 | $img = imagecreatetruecolor($_POST['width'],$_POST['height']); |
| 57 | $org_img = imagecreatefromjpeg($image); |
| 58 | $ims = getimagesize($image); |
| 59 | imagecopy($img,$org_img, 0, 0, $_POST['tx'], $_POST['ty'], $ims[0], $ims[1]); |
| 60 | imagejpeg($img,$dest_image,90); |
| 61 | imagedestroy($img); |
| 62 | echo ' |
| 63 | <img src="'.$dest_image.'" width="'.$_POST['width'].'" height="'.$_POST['height'].'"><p> |
| 64 | <a href="'.$_SERVER['PHP_SELF'].'">try again</a>'; |
| 65 | } |
| 66 | echo ' |
| 67 | </body> |
| 68 | </html>'; |
| 69 | ?> |
5 hits by 4 users in the last 30 minutes.