vivabensmith Posted August 5, 2007 Share Posted August 5, 2007 Hi Guys, do you any tutorials on cropping an image one its been uploaded? Quote Link to comment https://forums.phpfreaks.com/topic/63439-possible/ Share on other sites More sharing options...
Fadion Posted August 5, 2007 Share Posted August 5, 2007 <a href="http://nodstrum.com/2006/12/09/image-manipulation-using-php/">Click here for a tutorial</a> There are 4 image manipulations. U can use the 'resize' for your needs, modifying the last two parameters imagecopyresized() which tell where the crop will start. U may consider using imagecopysampled() instead of that to mantain image quality. Hope it helped. Quote Link to comment https://forums.phpfreaks.com/topic/63439-possible/#findComment-316165 Share on other sites More sharing options...
vivabensmith Posted August 6, 2007 Author Share Posted August 6, 2007 Cool - found this thou, but is thre anyway to do this just in php not js? http://koivi.com/image-crop-selection/ Quote Link to comment https://forums.phpfreaks.com/topic/63439-possible/#findComment-316519 Share on other sites More sharing options...
wildteen88 Posted August 6, 2007 Share Posted August 6, 2007 Take a look at the script in the post. Should do what you want/near to what you want. The script can be easily adapted if you don't want multiple sizes. Quote Link to comment https://forums.phpfreaks.com/topic/63439-possible/#findComment-316783 Share on other sites More sharing options...
vivabensmith Posted August 6, 2007 Author Share Posted August 6, 2007 thanks wild, but this script resizes and saves as a thumnail, I want to be able to crop the uploaded image then save to db? The below example uses flash but can this be done just using php? http://www.sephiroth.it/file_detail.php?id=109 Quote Link to comment https://forums.phpfreaks.com/topic/63439-possible/#findComment-316927 Share on other sites More sharing options...
wildteen88 Posted August 6, 2007 Share Posted August 6, 2007 I have edited the script I have referred to to resize the image uploaded. <?php if(isset($_POST['Submit'])) { // define the max width and height of uploaded images $max_width = 600; $max_height = 600; // define folder in which the images get uploaded to $upload_dir = './pics/'; // get uploaded image data from form $file_name = $_FILES['image']['name']; $file_tmp = $_FILES['image']['tmp_name']; $file_size = $_FILES['image']['size']; $file_type = $_FILES['image']['type']; // get info about uploaded image list($width, $height, $type, $attr) = getimagesize($file_tmp); // set the full upload path for where to upload the image to. $img_path = $upload_dir . $file_name; // check to see if the uploaded image is bigger tha max width an height // if it is we'll crop the image if($width > $max_width && $height > $max_height) { // image is too big so we'll crop it // get the aspect ratio of the image // we do this so we can keep the aspect ratio when the image gets cropped. $aspect_ratio = $height / $width; $new_width = $max_height / $aspect_ratio; // create a blank canvas for the size of the image // this is what the image will get cropped to $img_rs = imagecreatetruecolor($new_width, $max_height); // setup a image resource to the uploaded file $img = imageCreateFromJPEG($file_tmp); // crop the image imagecopyresampled($img_rs, $img, 0, 0, 0, 0, $new_width, $max_height, ImageSX($img), ImageSY($img)); // create the image ImageJPEG($img_rs, $img_path); // destroy the temporary image imagedestroy($img_rs); } else { // uploaded image is under the max width and hieght // we'll just upload it straight away move_uploaded_file($file_tmp, $img_path); } // display the image echo "<h1>Upload Image:</h1>\n\n<img src=\"$img_path\">"; } else { echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '" enctype="multipart/form-data"> <input type="file" name="image"><p> <input type="Submit" name="Submit" value="Submit"> </form>'; } ?> That will resize the image if the width and height is greater than 600px. You can change this if you want by editing the following two variables: $max_width = 600; $max_height = 600; Quote Link to comment https://forums.phpfreaks.com/topic/63439-possible/#findComment-317027 Share on other sites More sharing options...
vivabensmith Posted August 7, 2007 Author Share Posted August 7, 2007 Thanks again, But this resizes the image to a set pixel size, I want to be able to crop part of that image: eg: 800x600 px - crop certain area to 300 x 100 pixels with distortion the image. http://www.sephiroth.it/file_detail.php?id=109# This crops the area but using flas, is this possiable just in php? Quote Link to comment https://forums.phpfreaks.com/topic/63439-possible/#findComment-317334 Share on other sites More sharing options...
wildteen88 Posted August 7, 2007 Share Posted August 7, 2007 Thanks again, But this resizes the image to a set pixel size, I want to be able to crop part of that image: eg: 800x600 px - crop certain area to 300 x 100 pixels with distortion the image. http://www.sephiroth.it/file_detail.php?id=109# This crops the area but using flas, is this possiable just in php? The script you linked to uses php to crop the image, but uses flash to provide a interface so the user can select where to crop the image to. You wont be able to prove an interface, like the flash interface, with just PHP alone. Quote Link to comment https://forums.phpfreaks.com/topic/63439-possible/#findComment-317422 Share on other sites More sharing options...
vivabensmith Posted August 7, 2007 Author Share Posted August 7, 2007 Thanks for clearing that up! Is this the best way to crop an image? or what would you suggest? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/63439-possible/#findComment-317676 Share on other sites More sharing options...
vivabensmith Posted August 9, 2007 Author Share Posted August 9, 2007 im guessing this is the best way? Quote Link to comment https://forums.phpfreaks.com/topic/63439-possible/#findComment-319552 Share on other sites More sharing options...
wildteen88 Posted August 9, 2007 Share Posted August 9, 2007 I'd say so yes. Or you can go the javascript way. There is probably a script out there like the flash script but in javascript. However, if the user has javascript disabled or hasn't got flash installed then they wont be able to resize their images. You'll want to detect where they javascript or flash enabled before you allow them to upload. Quote Link to comment https://forums.phpfreaks.com/topic/63439-possible/#findComment-319637 Share on other sites More sharing options...
lemmin Posted August 9, 2007 Share Posted August 9, 2007 You can do it with a simple style attribute. http://msdn2.microsoft.com/en-us/library/ms530748.aspx Quote Link to comment https://forums.phpfreaks.com/topic/63439-possible/#findComment-319641 Share on other sites More sharing options...
wildteen88 Posted August 9, 2007 Share Posted August 9, 2007 You can do it with a simple style attribute. http://msdn2.microsoft.com/en-us/library/ms530748.aspx That will only be compatible with IE. *off topic Nice to see Microsoft has made MSDN firefox friendly now, liking the new design. Quote Link to comment https://forums.phpfreaks.com/topic/63439-possible/#findComment-319644 Share on other sites More sharing options...
vivabensmith Posted August 10, 2007 Author Share Posted August 10, 2007 thanks guys Im running a mac thou! But will look into both options thanks! Quote Link to comment https://forums.phpfreaks.com/topic/63439-possible/#findComment-320152 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.