louis567 Posted October 7, 2007 Share Posted October 7, 2007 Does anyone know how to scale an image to fit a certain a size and center it, then fill the remainder in white so I always have the same size image with aspect ratio remaining? Thanks. Would help me a lot. Quote Link to comment https://forums.phpfreaks.com/topic/72177-scale-image-to-fit-certain-width-and-height/ Share on other sites More sharing options...
AV1611 Posted October 7, 2007 Share Posted October 7, 2007 Do you mean with GD? Otherwise, you can just use html/css to do it Quote Link to comment https://forums.phpfreaks.com/topic/72177-scale-image-to-fit-certain-width-and-height/#findComment-364085 Share on other sites More sharing options...
louis567 Posted October 8, 2007 Author Share Posted October 8, 2007 Umm what GU??? If I use CSS/HTML it will lose quality which I would rather not lose. Is there any image manipulation functions in PHP that allow me to scale it to a certain size but maintaining the ratio??? Quote Link to comment https://forums.phpfreaks.com/topic/72177-scale-image-to-fit-certain-width-and-height/#findComment-364424 Share on other sites More sharing options...
jellis Posted October 8, 2007 Share Posted October 8, 2007 You can start with something like this and customise it to suit your needs. As for filling the background with white, you'd be better off setting the background area to white with css/html rather than insist the server do all of that extra image processing work. Most of the stuff inside that script is pretty self explanatory and if you've got a single nuance of php ability you should be able to work it out. Quote Link to comment https://forums.phpfreaks.com/topic/72177-scale-image-to-fit-certain-width-and-height/#findComment-364452 Share on other sites More sharing options...
louis567 Posted October 9, 2007 Author Share Posted October 9, 2007 Maybe its not possible in PHP. You can do this in ColdFusion! ColdFusion > PHP anyday. You get what you pay for I say. I prefer a better scripting language thats why I shall use ColdFusion. Quote Link to comment https://forums.phpfreaks.com/topic/72177-scale-image-to-fit-certain-width-and-height/#findComment-365302 Share on other sites More sharing options...
~n[EO]n~ Posted October 9, 2007 Share Posted October 9, 2007 It is possible in PHP (anything is possible in PHP ) check this function <?php function ImageResize($filepath,$savefilepath,$imagetype,$w,$h) { $filename=$filepath; $full_url=$savefilepath; // Get new dimensions list($width, $height) = getimagesize($filename); // image width $sw = $width;//$x[0]; // image height $sh = $height;//$x[1]; $percent=0; $fixH=0; $fixW=0; if ($percent > 0) { // calculate resized height and width if percent is defined $percent = $percent * 0.01; $w = $sw * $percent; $h = $sh * $percent; } else { if($sw>$sh){ if($sw>$w){//--take ration $fixW=$w; $fixH=($w/$sw)*$sh; if($fixH>$h){ $imageErr=0; return $imageErr; } } elseif($sw<=$w){ if($sh>$h){ $imageErr=1; return $imageErr; } else{ $fixH=$sh; $fixW=$sw; } } } elseif($sw<=$sh){ if($sh>$h){//--take ration $fixH=$h; $fixW=round(($h/$sh)*$sw,2); if($fixW>$w){ $imageErr=2; return $imageErr; } } elseif($sh<=$h){ if($sw>$w){ $imageErr=3; return $imageErr; } else{ $fixH=$sh; $fixW=$sw; } } } } $new_width=$fixW; $new_height=$fixH; //echo $fixH."s".$fixW; //exit; // Resample $image_p = imagecreatetruecolor($new_width, $new_height); if(strtolower($imagetype)=="jpg"||strtolower($imagetype)=="jpeg"){ $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //imagejpeg($image_p, null, 100); imagejpeg($image_p,$full_url,100); } elseif(strtolower($imagetype)=="gif"){ $image = imagecreatefromgif($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagegif($image_p,$full_url,100); } elseif(strtolower($imagetype)=="png"){ $image = imagecreatefrompng($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagepng($image_p,$full_url,100); } imagedestroy($image); $imageErr=-1; return $imageErr; }?> Quote Link to comment https://forums.phpfreaks.com/topic/72177-scale-image-to-fit-certain-width-and-height/#findComment-365309 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.