View Full Version : Resizing an Image
izwar
05-17-2006, 03:14 PM
Hello,
on my profile site where users have there profile picture, how do i make it a default size so even if the image is veyr big it will not distort the page, for example as of right now an image that is to big is doing this to the page
http://www.hotclubber.com/memberinfo.php?id=36
This is the coding around the image area including the table tag
<td width="350" align="center"><?php
if ($row['size']>0)
{
?>
<img src="image1.php?id=<?php echo $row['urid'];?>" border="0" />
<?php
}
else
{ ?>
<img src="images/comingsoon.gif" width="100" height="100" />
<?php
} ?></td>
if someone can suggest something, i have my old developers telling me there gonna use java to solve this but htat doesnt really make sense. Anyone know any php code or something to solve this.
izwar
05-17-2006, 03:23 PM
just letting you know just by putting a size value in the img src filed with the PHP tag, i can make the images and size, but how do i make it so it resizes accordignly the image without distroting instead of putting every image to a size and distorting it.
Here is what I would do as far as steps:
1. define a $maxwidth
2. get the image's $oldwidth and $oldheight using the getimagesize() function
3. open an if ($oldwidth > $maxwidth) condition
4. have $newwidth = $maxwidth
5. find the number ($scale) that $oldwidth would divide by to = $newwidth
6. divide the $oldheight by $scale to get the scaled down height ($newheight)
7. close the if, open an else
8. have the $newwidth and $newheight = the $oldwidth and $oldheight
9. close the else
Sorry about not doing the actual coding, but I think it's good practice to do the actual coding yourself and just get help with the ideas behind the coding.
izwar
05-17-2006, 06:37 PM
that helps tremendoulsey thank you, and yes i will try this for practice my self, hopefully ill get it lol. If worst comes to worst i mite hav to ask you for the code lol. Ill do it in a few hours, thanks man appreciate it.
P.S does the PHP url of the image go in the Getimagesize ( ) brackets?
ima get to testing this out ill post my code when i get errors lol cuz i know ill get some.
P.S does the PHP url of the image go in the Getimagesize ( ) brackets?
Here's the php.net function page for getimagesize():
http://us2.php.net/manual/en/function.getimagesize.php
Basically, with this:
"$size = getimagesize("http://www.example.com/gifs/logo.gif");"
$size[0] will equal the width in pixels
$size[1] will equal the height in pixels
Hope that helps
izwar
05-17-2006, 07:56 PM
5. find the number ($scale) that $oldwidth would divide by to = $newwidth
how would i find this since every image would be different.
man im trying this out and its really hard dont know if ill get it.
izwar
05-17-2006, 08:01 PM
This is what i could try out, as i didnt understand the scale thing, but ive onyl really been doing php a few days so i think this mite be a lil to advanced. Can you just help me out with the code as i dont have time to get this working cuz its the last thing i need to launch my site. Thanks man Greatly greatly appreciated. I dont even know were to start the code you gave me, so its prlly in the wrong area.
<td width="350" align="center"><?
$maxwidth = 300;
$size = getimagesize("image1.php?id=<?php echo $row['urid'];?>");
if ($oldwidth > $maxwidth) {
$newwidth = $maxwidth;
$scale =
$newheight = $oldheight / $scale;
}
else { ($newwidth $$ $newheight = $oldwidth $$ $oldheight);
}
?>
<?php
if ($row['size']>0)
{
?>
<img src="image1.php?id=<?php echo $row['urid'];?>" border="0" />
<?php
}
else
{ ?>
<img src="images/comingsoon.gif" width="100" height="100" />
<?php
} ?>
Ok, this might not be the best way to do it (just quickly coded) but it should work:
<?
$imageurl = "http://www.domain.com/folder/image.gif";
$maxwidth = 300;
$size = getimagesize("$imageurl");
$oldwidth = $size[0];
$oldheight = $size[1];
if ($oldwidth > $maxwidth) {
$scale = $oldwidth / $maxwidth;
$newwidth = $oldwidth / $scale;
$newheight = $oldheight / $scale;
}
else {
$newwidth = $oldwidth;
$newheight = $oldheight;
}
?>
<img src="<? echo $imageurl; ?>" width="<? echo $newwidth; ?>" height="<? echo $newheight; ?>" >
izwar
05-17-2006, 10:02 PM
for $imageurl woudl i put this
image1.php?id=<?php echo $row['urid'];?>
as every image depends on their user id.
you wouldn't need the echo etc. because you're already in PHP, but yeah basically. Try:
$imageurl = "image1.php?id=$row['urid']";
ogito
05-18-2006, 05:16 AM
better way is to resize the image and save it on the server with the new width/height (there is free php classes for image manipulaton), otherwise the user will download the whole image , but just resized virtually to the width/height in the <img> tag. You and the user will save traffic.
for example:
http://www.phpclasses.org/browse/package/1450.html
http://www.phpclasses.org/browse/package/558.html
http://www.phpclasses.org/browse/package/1980.html
http://www.phpclasses.org/browse/package/871.html
izwar
05-18-2006, 11:58 AM
im still expirementing, but my old programmer is saying that hes gonna make a java script for the resizing and it will be better then using hte PHP way? any one agree with this or give me input.
Powered by vBulletin® Version 4.2.2 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.