01 <?php
02
03 // the image to crop
04 $image = 'pics/whatever.jpg';
05 // make sure the directory is writeable
06 $dest_image = 'pics/cropped_whatever.jpg';
07
08 // to keep the image and layer in sync
09 $margin = 10;
10
11 if(!isset($_POST['step'])) $title = '1';
12 else $title = $_POST['step'];
13
14 echo '
15 <html>
16 <head>
17 <title>lixlpixel image crop | step '.$title.'</title>
18 </head>
19 <body style="margin: '.$margin.'px;">';
20
21 if(!isset($_POST['tx']) && !isset($_POST['fx']))
22 {
23 echo '
24 <form method="post" action="'.$_SERVER['PHP_SELF'].'">
25 <input type="image" src="'.$image.'" /><br />';
26 if(!isset($_POST['x']))
27 {
28 echo '
29 <input type="hidden" name="step" value="2" />
30 click to mark first corner';
31 }else{
32 echo '
33 <input type="hidden" name="step" value="3" />
34 <input type="hidden" name="fx" value="'.$_POST['x'].'" />
35 <input type="hidden" name="fy" value="'.$_POST['y'].'" />
36 click to mark second corner | <a href="'.$_SERVER['PHP_SELF'].'">start over</a>';
37 }
38 echo '
39 </form>
40 ';
41 }
42
43 if(isset($_POST['fx']))
44 {
45 echo '
46 <form method="post" action="'.$_SERVER['PHP_SELF'].'">
47 <input type="hidden" name="step" value="4" />
48 <input type="image" src="'.$image.'" /><br />
49 <input type="hidden" name="tx" value="'.$_POST['fx'].'" />
50 <input type="hidden" name="ty" value="'.$_POST['fy'].'" />
51 <input type="hidden" name="width" value="'.($_POST['x']-$_POST['fx']).'" />
52 <input type="hidden" name="height" value="'.($_POST['y']-$_POST['fy']).'" />
53 <div style="position: absolute;
54 left:'.($_POST['fx']+$margin).'px;
55 top: '.($_POST['fy']+$margin).'px;
56 width: '.($_POST['x']-$_POST['fx']).'px;
57 height: '.($_POST['y']-$_POST['fy']).'px;
58 border: 1px solid #fff;">
59 </div>
60 click image to crop rectangle | <a href="'.$_SERVER['PHP_SELF'].'">start over</a>
61 </form>';
62 }
63
64 if(isset($_POST['tx']))
65 {
66 $img = imagecreatetruecolor($_POST['width'],$_POST['height']);
67 $org_img = imagecreatefromjpeg($image);
68 $ims = getimagesize($image);
69 imagecopy($img,$org_img, 0, 0, $_POST['tx'], $_POST['ty'], $ims[0], $ims[1]);
70 imagejpeg($img,$dest_image,90);
71 imagedestroy($img);
72 echo '
73 <img src="'.$dest_image.'"
74 width="'.$_POST['width'].'"
75 height="'.$_POST['height'].'" alt="" /><br />
76 <a href="'.$_SERVER['PHP_SELF'].'">try again</a>';
77 }
78
79 echo '
80 </body>
81 </html>';
82
83 ?>