gerkintrigg Posted August 9, 2006 Share Posted August 9, 2006 Hi everyone!I'd like to find out how to change the current script I'm using. It resizes images fine, but rather than setting them to absolute figures, I'd like to work out a way whereby I can set maximum figures.So for example, rather than saying "it MUST be 400 pixels wide by 20 Px high" I'd like to say: "it can be a maximum of 400 px wide by 20 px high, but the proportions must stay the same.This is the script I have so far:[code]function makeimage($filename,$newfilename,$path,$newwidth,$newheight) { //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER . ) $image_type = strstr($filename, '.'); //SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION switch($image_type) { case '.jpg': $source = imagecreatefromjpeg($filename); break; case '.png': $source = imagecreatefrompng($filename); break; case '.gif': $source = imagecreatefromgif($filename); break; default: echo("Error Invalid Image Type"); die; break; } //CREATES THE NAME OF THE SAVED FILE $file = $newfilename . $filename; //CREATES THE PATH TO THE SAVED FILE $fullpath = $path . $file; //FINDS SIZE OF THE OLD FILE list($width, $height) = getimagesize($filename); //CREATES IMAGE WITH NEW SIZES $thumb = imagecreatetruecolor($newwidth, $newheight); //RESIZES OLD IMAGE TO NEW SIZES imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); //SAVES IMAGE AND SETS QUALITY || NUMERICAL VALUE = QUALITY ON SCALE OF 1-100 imagejpeg($thumb, $fullpath, 60); //CREATING FILENAME TO WRITE TO DATABSE $filepath = $fullpath; //RETURNS FULL FILEPATH OF IMAGE ENDS FUNCTION return $filepath;}[/code]Thanks in advance for any help you might be able to give. Quote Link to comment Share on other sites More sharing options...
shocker-z Posted August 9, 2006 Share Posted August 9, 2006 if (($height > 20) or ($width > 400)) {imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);}Would that not do the job??RegardsLiam Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted August 9, 2006 Author Share Posted August 9, 2006 er... no. I tried it, but no cigar. Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted August 9, 2006 Share Posted August 9, 2006 Add this code:[code=php:0] // Set the maximum width and height $max_width = 400; $max_height = 20; // Check if either dimension is over the maximum if (($height > $max_height) || ($width > $max_width)) { // Set a size factor variable, depending on which // dimension is greater if ($height > $width) { $sizefactor = (double) ($max_height / $height); } else { $sizefactor = (double) ($max_width / $width) ; } $newwidth = (int) ($image_width * $sizefactor); $newheight = (int) ($image_height * $sizefactor); }[/code]After this line:[code=php:0] //FINDS SIZE OF THE OLD FILE list($width, $height) = getimagesize($filename);[/code]Let us know if that works... ;) Quote Link to comment Share on other sites More sharing options...
litebearer Posted August 9, 2006 Share Posted August 9, 2006 Might be of some use...http://www.nstoia.com/toh/technical/imageresize/index.phpLite... 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.