nathanblogs Posted November 22, 2007 Share Posted November 22, 2007 Hey, I'm currently trying to make a image cutting/cropping function. How it works is theres a basic page load with an image on it on that image contains some JavaScript which allows you to on-the-fly select a rectangle box of the picture, you can then hit the crop button which will post back to the page the details of the page, for example ((x1,y1),(x2,y2),...,... co-ordinates, width and height of the rectangle box). To crop the image I then run this code $file = "test.jpeg"; // make local variables from $_POST to make them more readable $x1 = $_POST['x1']; $y1 = $_POST['y1']; $x2 = $_POST['x2']; $y2 = $_POST['y2']; $width = $_POST['width']; $height = $_POST['height']; $info = getimagesize($file); list($width_old, $height_old) = $info; $dest_image = "test-2.jpg"; // make sure the directory is writeable switch ( $info[2] ) { case IMAGETYPE_GIF: $image = imagecreatefromgif($file); break; case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($file); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($file); break; default: return false; } // creates a new image object with dimensions 300x210 $image_resized = imagecreatetruecolor( 300, 210 ); imagecolortransparent($image_resized, imagecolorallocate($image_resized, 0, 0, 0) ); imagealphablending($image_resized, false); imagesavealpha($image_resized, true); imagecopyresampled($image_resized, $image, 0, 0, $x1, $y1, $width, $height, $width_old , $height_old ); // write to the file switch ( $info[2] ) { case IMAGETYPE_GIF: imagegif($image_resized, $dest_image); break; case IMAGETYPE_JPEG: imagejpeg($image_resized, $dest_image); break; case IMAGETYPE_PNG: imagepng($image_resized, $dest_image); break; default: return false; } //imagejpeg($image_resized,$dest_image,90); //imagedestroy($image); $try_again_html = "?section=files&mode=display_resize_image&id=" . $id; echo ' <img src="'. $dest_image.'" BORDER=1><p> <a href="' . $try_again_html . '">try again</a>'; exit; my understanding is that imagecopyresampled($image_resized, $image, 0, 0, $x1, $y1, $width, $height, $width_old , $height_old ); will take the old image and copy from (x1,y1) to (1+width, y1+height) into the new image. However this doesn't seem to be working Quote Link to comment Share on other sites More sharing options...
r-it Posted November 22, 2007 Share Posted November 22, 2007 I think the best option would be to download wordpress, and see how their cropping functionality works from their code. Quote Link to comment Share on other sites More sharing options...
nathanblogs Posted November 23, 2007 Author Share Posted November 23, 2007 Fixed it by first resizing the image, then cutting the image out of the resized image. Will have a look at wordpress thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.