avario Posted December 2, 2009 Share Posted December 2, 2009 I have a script that uploads an image using a form. It creates a preview version of image (width of 500px) and a thumbnail version of the image (width of 100px). But I want all of the thumbnails created be the script to be square, but not distorted, so I need to scale the image so that it maximum dimension is 100 (which I can do) but then I need to crop the other dimension to 100 to make it square (which I can't do). mean? Does anyone know a script that I can add to my upload script that will crop the image? Any help is much appreciated, Thanks. This is my upload script if it helps: $imagename = $_FILES['new_image']['name']; $source = $_FILES['new_image']['tmp_name']; $target = "Images/Photos/".$imagename; move_uploaded_file($source, $target); $imagepath = $imagename; $save = "Images/Photos/Previews/" . $imagepath; //This is the new file you saving $file = "Images/Photos/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; $modwidth = 500; $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; $save = "Images/Photos/Thumbnails/" . $imagepath; //This is the new file you saving $file = "Images/Photos/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; $modwidth = 100; $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; Link to comment https://forums.phpfreaks.com/topic/183712-cropping-an-uploaded-image/ Share on other sites More sharing options...
Kieran Menor Posted December 2, 2009 Share Posted December 2, 2009 I created this function at some point. It may be of use. <?php function createThumbnail($src_file, $dst_file, $dst_width, $dst_height, $dst_quality = 75) { if(!file_exists($src_file)) { // Source file does not exist return false; } if(file_exists($dst_file)) { // Destination file already exists return false; } if(!is_writable(dirname($dst_file))) { // Destinaton directory is not writable return false; } list($src_width, $src_height, $src_type) = getimagesize($src_file); switch($src_type) { case IMAGETYPE_GIF: $src_image = imagecreatefromgif($src_file); break; case IMAGETYPE_JPEG: $src_image = imagecreatefromjpeg($src_file); break; case IMAGETYPE_PNG: $src_image = imagecreatefrompng($src_file); break; default: // Unsupported image type return false; } $dst_image = imagecreatetruecolor($dst_width, $dst_height); if($src_width > $src_height) { $x_offset = floor( ( $src_width - $src_height ) / 2 ); imagecopyresampled($dst_image, $src_image, 0, 0, $x_offset, 0, $dst_width, $dst_height, $src_height, $src_height); } elseif($src_width < $src_height) { $y_offset = floor( ( $src_height - $src_width ) / 2 ); imagecopyresampled($dst_image, $src_image, 0, 0, 0, $y_offset, $dst_width, $dst_height, $src_width, $src_width); } else { imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); } imagejpeg($dst_image, $dst_file, $dst_quality); imagedestroy($src_image); imagedestroy($dst_image); return true; } ?> Link to comment https://forums.phpfreaks.com/topic/183712-cropping-an-uploaded-image/#findComment-969648 Share on other sites More sharing options...
avario Posted December 3, 2009 Author Share Posted December 3, 2009 Thank you so much Boom.dk. The script is just what I was looking for and worked perfectly. Thank you. Link to comment https://forums.phpfreaks.com/topic/183712-cropping-an-uploaded-image/#findComment-970394 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.