Jump to content

PHP Image Manipulation


TGWSE_GY

Recommended Posts

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  ;D

Link to comment
Share on other sites

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);  

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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  ;D

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.