TGWSE_GY Posted June 30, 2009 Share Posted June 30, 2009 Hi guys, I am trying to display members main profile image on their profile page, and I am wanting to scale that image to a specific size 130x252. Is there a way in php gdl to keep the proportions of the image while scaling it to the previous designated size. thanks Quote Link to comment https://forums.phpfreaks.com/topic/164201-php-image-manipulation/ Share on other sites More sharing options...
flyhoney Posted June 30, 2009 Share Posted June 30, 2009 You're going to have to do some math You can get the height and width using http://us2.php.net/getimagesize And then you just have to do some old fashioned geometry to determine how you want to resize. Here is a simple algorithm that should work (untested) <?php $max_width = 130; $max_height = 252; list($width, $height, $type, $attr) = getimagesize($filename); $percentage = ($width > $height) ? ($max_width / $width) : ($max_height / $height); // new width and height $width = round($width * $percentage); $height = round($height * $percentage); Quote Link to comment https://forums.phpfreaks.com/topic/164201-php-image-manipulation/#findComment-866164 Share on other sites More sharing options...
Psycho Posted June 30, 2009 Share Posted June 30, 2009 If you want the image to "fill" a specific boundry such that it will crop the side that is relatively too big for the boundry, I wrote a function just a couple days ago for someone on this forum: http://www.phpfreaks.com/forums/index.php/topic,258146.msg1214847.html#msg1214847 Or if you want the image to be scaled down such that it completely "fits" into the designated boundry such that the longest relative size fits (and the short side is smaller than the boundry) then you can retrofit that accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/164201-php-image-manipulation/#findComment-866165 Share on other sites More sharing options...
TGWSE_GY Posted June 30, 2009 Author Share Posted June 30, 2009 Thanks flyhoney, I think I understand what you are saying. This is what my script looks like so far, the area marked <insert here> is where I am thinking it should be inserted. Please correct me if I am wrong. <?php include('/home/thegayestever/thegayestcommunityever.com/dev_final/php/includes/configs/db_config.php'); // Get the username from the session $varUser = $_COOKIE['User']; //Get the users CustID $GetCustID = mysql_query("SELECT CustID FROM Login WHERE User = '$varUser'"); $CustID = mysql_fetch_array($GetCustID); $varCustID = $CustID['CustID']; //Get array of records from db holding the location of the users images $UserImgArr = mysql_query("SELECT * FROM Profile_User_Images WHERE CustID = '$varCustID' and MainImage = '1'"); $UserImgs = mysql_fetch_array($UserImgArr); $ImgCount = mysql_num_rows($UserImgArr); echo "<table cellspacing=\"10\" cellpadding=\"0\" align=\"center\"> <tr valign=\"top\">"; //while ($UserImgs = mysql_fetch_array($UserImgArr)) { //if ($ColCount == 4) { //$ColCount = 1; //} //if ($ColCount == 3) { $img = $UserImgs['ImageName']; <insert here> along with image location ie. php/includes/content/members_home/user_images/perm_images/$varUser/$img echo "<td align=\"center\">"; echo "<h2>$varUser</h2>"; echo "<img src=\"php/includes/content/members_home/user_images/perm_images/$varUser/$img\" height=\"252\" width=\"130\" />"; echo "</td>"; echo "</tr>"; echo "<tr valign=\"top\">"; //$ColCount = $ColCount + 1; /*} else { $img = $UserImgs['ImageName']; echo "<td align=\"center\">"; echo "<img src=\"php/includes/content/members_home/user_images/perm_images/$varUser/$img\" height=\"100\" width=\"100\" />"; echo "<br />"; echo "Set Profile Photo"; echo "<br />"; echo "Comment"; echo "</td>"; $ColCount = $ColCount + 1; } }*/ //if ($ColCount == 3) { echo "</table>"; /*} else { echo "</tr>"; echo "</table>"; } }*/ ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/164201-php-image-manipulation/#findComment-866166 Share on other sites More sharing options...
flyhoney Posted June 30, 2009 Share Posted June 30, 2009 Yes, that is where it can go. Quote Link to comment https://forums.phpfreaks.com/topic/164201-php-image-manipulation/#findComment-866176 Share on other sites More sharing options...
Psycho Posted June 30, 2009 Share Posted June 30, 2009 If you go with flyhoney's code, there is an error that needs to be fixed. This $percentage = ($width > $height) ? ($max_width / $width) : ($max_height / $height); Should be this $percentage = ( ($width/$height) > ($max_width/$max_height) ) ? ($max_width / $width) : ($max_height / $height); Determining which direction to scale on needs to be on the relative difference between the image ratio and the maximum size ratio. Quote Link to comment https://forums.phpfreaks.com/topic/164201-php-image-manipulation/#findComment-866177 Share on other sites More sharing options...
TGWSE_GY Posted June 30, 2009 Author Share Posted June 30, 2009 and $filename would be replaced with image location correct? Also thanks mjdamato I was getting a division by zero issue with flyhoney's orriginal code. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/164201-php-image-manipulation/#findComment-866184 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.