newrehmi Posted March 7, 2010 Share Posted March 7, 2010 I am sorry if this has been questioned for many times... But i still can't find a way to do it how... how to resize the uploaded images to the certain scale, on the same ratio. Because all of the uploaded photo in my site is so big (like 1.5MB) that make it slow to load... I don't want to set the MAX_FILE_SIZE too low because it will be hard to upload... Edit: The extension is not only jpeg. Thanks very much indeed, and sorry for my broken english. Quote Link to comment https://forums.phpfreaks.com/topic/194399-how-to-resize-uploaded-images-to-the-certain-scale-on-the-same-ratio/ Share on other sites More sharing options...
ldb358 Posted March 7, 2010 Share Posted March 7, 2010 function generate_thumbnail($image, $max, $output){ if(!file_exists($output)){ $size = getimagesize($image); if($size[0] > $size[1]){ $type = trim(str_replace('image/',' ', image_type_to_mime_type($size[2]))); $newheight = $max*($size[1]/$size[0]); switch($type){ case "png": $oimage = imagecreatefrompng($image); break; case "jpg": case "jpeg": $oimage = imagecreatefromjpeg($image); break; case "gif": $oimage = imagecreatefromgif($image); break; default: trigger_error("Invalid Image Type", E_USER_WARNING); break; } $nimage = imagecreatetruecolor( $max , $newheight ); imagecopyresampled($nimage,$oimage, 0,0, 0, 0, $max , $newheight, $size[0] , $size[1]); imagegif($nimage,$output,100); }else{ $type = trim(str_replace('image/',' ', image_type_to_mime_type($size[2]))); $newwidth = $max*($size[0]/$size[1]); switch($type){ case "png": $oimage = imagecreatefrompng($image); break; case "jpg": case "jpeg": $oimage = imagecreatefromjpeg($image); break; case "gif": $oimage = imagecreatefromgif($image); break; default: trigger_error("Invalid Image Type", E_USER_WARNING); break; } $nimage = imagecreatetruecolor( $newwidth , $max ); imagecopyresampled($nimage,$oimage, 0,0, 0, 0, $newwidth , $max , $size[0] , $size[1]); imagegif($nimage,$output,100); } } } where the $image is the beginning image and the $output is the new image to be generated Quote Link to comment https://forums.phpfreaks.com/topic/194399-how-to-resize-uploaded-images-to-the-certain-scale-on-the-same-ratio/#findComment-1022620 Share on other sites More sharing options...
xcoderx Posted March 7, 2010 Share Posted March 7, 2010 function generate_thumbnail($image, $max, $output){ if(!file_exists($output)){ $size = getimagesize($image); if($size[0] > $size[1]){ $type = trim(str_replace('image/',' ', image_type_to_mime_type($size[2]))); $newheight = $max*($size[1]/$size[0]); switch($type){ case "png": $oimage = imagecreatefrompng($image); break; case "jpg": case "jpeg": $oimage = imagecreatefromjpeg($image); break; case "gif": $oimage = imagecreatefromgif($image); break; default: trigger_error("Invalid Image Type", E_USER_WARNING); break; } $nimage = imagecreatetruecolor( $max , $newheight ); imagecopyresampled($nimage,$oimage, 0,0, 0, 0, $max , $newheight, $size[0] , $size[1]); imagegif($nimage,$output,100); }else{ $type = trim(str_replace('image/',' ', image_type_to_mime_type($size[2]))); $newwidth = $max*($size[0]/$size[1]); switch($type){ case "png": $oimage = imagecreatefrompng($image); break; case "jpg": case "jpeg": $oimage = imagecreatefromjpeg($image); break; case "gif": $oimage = imagecreatefromgif($image); break; default: trigger_error("Invalid Image Type", E_USER_WARNING); break; } $nimage = imagecreatetruecolor( $newwidth , $max ); imagecopyresampled($nimage,$oimage, 0,0, 0, 0, $newwidth , $max , $size[0] , $size[1]); imagegif($nimage,$output,100); } } } where the $image is the beginning image and the $output is the new image to be generated sorry for the hijacking, well i too want to know the same like on site some users try to upload images which size are like 5mb, 10mb etc etc. and height and width also differs as some images are huge, so is there a specific code which will actually automatically reduce the 10mb pic size to some default like 500kb and some fixed height and width during the upload process? Quote Link to comment https://forums.phpfreaks.com/topic/194399-how-to-resize-uploaded-images-to-the-certain-scale-on-the-same-ratio/#findComment-1022626 Share on other sites More sharing options...
inversesoft123 Posted March 7, 2010 Share Posted March 7, 2010 Checkout another similar thread.. http://www.phpfreaks.com/forums/index.php/topic,289663 Quote Link to comment https://forums.phpfreaks.com/topic/194399-how-to-resize-uploaded-images-to-the-certain-scale-on-the-same-ratio/#findComment-1022632 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.