Image Resizing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PastorHank
Forum Contributor
Posts: 117
Joined: Sat Jun 03, 2006 7:58 am
Location: Texas Hill Country

Image Resizing

Post by PastorHank »

The following code displays my raw image correctly

Code: Select all

echo "<table align='center' border='0' width='400' >";
echo "<tr>\n
	 <td align='center'><img border='0' src=\"".htmlspecialchars($imagetouse)."\"></td>
	 </tr>";
echo "<tr>\n
	 <td> </td>
	</tr>";
echo "</table>\n";
however the image is not the size I want it to be, I need it resized to 150x150 (or close to that) so I am using the following

Code: Select all

$mysock=getimagesize($imagetouse);
var_dump shows the following
array(7) {
[0]=> int(650)
[1]=> int(478)
[2]=> int(2)
[3]=> string(24) "width="650" height="478""
["bits"]=> int(8) ["channels"]=> int(3) ["mime"]=> string(10) "image/jpeg" }
Which is correct - so I have the following function IMMEDIATELY FOLLOWING THE getimagesize function

Code: Select all

$mysock=getimagesize($imagetouse);

function imageResize($width, $height, $target) { 

//takes the larger size of the width and height and applies the formula accordingly...this is so this script will work 
dynamically with any size image 

if ($width > $height) { 
$percentage = ($target / $width); 
} else { 
$percentage = ($target / $height); 
} 

//gets the new value and applies the percentage, then rounds the value 
$width = round($width * $percentage); 
$height = round($height * $percentage); 

//returns the new sizes in html image tag format...this is so you 
can plug this function inside an image tag and just get the 

return "width=\"$width\" height=\"$height\""; 

}
Now the question becomes where do I place this function? I have tried the following - which is placed after the function is declared...

Code: Select all

echo "<table align='center' border='0' width='400' >";
echo "<tr>\n
	 <td align='center'><img border='0' src=\"".htmlspecialchars($imagetouse)."\" imageResize($mysock[0], $mysock[1], 150)
></td>
	 </tr>";
echo "<tr>\n
	 <td> </td>
	</tr>";
echo "</table>\n";
However the image remains the original size. How do I get this function to resize the image?

Thank you
Post Reply