isedeasy Posted February 14, 2010 Share Posted February 14, 2010 I have a slight problem, the code works but it does not do exactally as I want //list the width and height and keep the height ratio. list($width, $height) = getimagesize($file_tmp); //calculate the image ratio $imgratio = $width/$height; if ( $imgratio > 1 ) { $newwidth = THUMB_WIDTH; $newheight = THUMB_WIDTH/$imgratio; } else { $newheight = THUMB_WIDTH; $newwidth = THUMB_WIDTH*$imgratio; } //function for resize image. $resized_img = imagecreatetruecolor($newwidth,$newheight); //the resizing is going on here! imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); //finally, save the image imagejpeg ($resized_img, 'd:/www'.THUMB_DIR.$image_name, 100); //move_uploaded_file ($new_img, THUMB_DIR.$image_name); ImageDestroy ($new_img); ImageDestroy ($resized_img); The above code scales the image whilst keeping proportions, that works fine. The problem is that I want the image to always be 210px by 210px. Depending on the image either the height or width may be less than 210px. I need to keep the proportions the same but some how add white-space to compensate the shorter edge. Is this possible? Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 14, 2010 Share Posted February 14, 2010 I do not know of a way that this can be done the way you want, but what I would do is to center the resultant image in a 210x210 px div with a white background Quote Link to comment Share on other sites More sharing options...
isedeasy Posted February 14, 2010 Author Share Posted February 14, 2010 I do not know of a way that this can be done the way you want, but what I would do is to center the resultant image in a 210x210 px div with a white background That's how I am doing it at the moment, the problem with this method is when you get an image that's not 210px high. I can't vertically centre it in a div which makes it look amatuar. Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 14, 2010 Share Posted February 14, 2010 why cant you vertically center it in a div? you may need some css like display:table-cell;vertical-align:middle Quote Link to comment Share on other sites More sharing options...
isedeasy Posted February 14, 2010 Author Share Posted February 14, 2010 The only problem with that is ie7 downwards does not support display:table-cell Quote Link to comment Share on other sites More sharing options...
ldb358 Posted February 14, 2010 Share Posted February 14, 2010 couldn't you use http://www.php.net/manual/en/function.imagecreate.php http://www.php.net/manual/en/function.imagecolorset.php to create a blank white image then use this: http://www.php.net/manual/en/function.imagecopyresampled.php to copy your original image on to the white image and center by changing the x and y values Quote Link to comment Share on other sites More sharing options...
isedeasy Posted February 14, 2010 Author Share Posted February 14, 2010 Thanks thats what I am looking for. I have got the image positioning working but I can't seem to set the background to white. What ever I try I get a black bg. //list the width and height and keep the height ratio. list($width, $height) = getimagesize($file_tmp); //calculate the image ratio $imgratio = $width/$height; if ( $imgratio > 1 ) { $newwidth = THUMB_WIDTH; $newheight = THUMB_WIDTH/$imgratio; $y = (THUMB_WIDTH - $newheight)/2; $x = 0; } else { $newheight = THUMB_WIDTH; $newwidth = THUMB_WIDTH*$imgratio; $x = (THUMB_WIDTH - $newwidth)/2; $y = 0; } //function for resize image. $resized_img = imagecreatetruecolor(THUMB_WIDTH,THUMB_WIDTH); imagecolorallocate($resized_img, 255, 225, 225); //the resizing is going on here! imagecopyresampled ($resized_img, $new_img, $x, $y, 0, 0, $newwidth, $newheight, $width, $height); //finally, save the image imagejpeg ($resized_img, 'd:/www'.THUMB_DIR.$image_name, 100); //move_uploaded_file ($new_img, THUMB_DIR.$image_name); ImageDestroy ($new_img); ImageDestroy ($resized_img); Quote Link to comment Share on other sites More sharing options...
isedeasy Posted February 14, 2010 Author Share Posted February 14, 2010 Ok this one is solved, thanks for the help guys $resized_img = imagecreate(THUMB_WIDTH,THUMB_WIDTH); imagecolorallocate($resized_img, 255, 255, 255); imagecopyresampled ($resized_img, $new_img, $x, $y, 0, 0, $newwidth, $newheight, $width, $height); imagejpeg ($resized_img, 'd:/www'.THUMB_DIR.$image_name, 100); ImageDestroy ($new_img); ImageDestroy ($resized_img); 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.