
|
View Full Version : php and image size / resize
variable 06-13-2005, 03:09 PM i know there is other libraries i could install that would let me do this, but it is not my server to install on, and i want my code to be portable anyways.
i need to resize an image if it is too large for the page. the way i was going to do this was to check the size of the image, and if it is to big just resize it with html width=, height= . is there any way to find the width of an image with php?
thanks
mouldy_punk 06-13-2005, 03:12 PM getimagesize() (http://uk2.php.net/getimagesize) sounds like what you want.
the--dud 06-13-2005, 03:23 PM GD is an excellent image manipulation library for PHP.
Alternatively, you can use ImageMagick or netbpm.
serversphere 06-13-2005, 10:27 PM I love GD, but it depeds on if you have it available or not. Here's a quick example with getimagesize() that limits height to 200px or less I used for a client's site:
$fileToExamine = "imgs/picture.jpg"; //actually pulled from db
list($width, $height, $type, $attr) = getimagesize($fileToExamine);
echo "<img src='imgs/picture.jpg' height=";
if ($height>200) {
echo "200";
} else {
echo $height;
}
echo ">";
Found ready to use function in my classes
function resizeImage($imagePath, $newImagePath, $x, $y)
{
if (!$attr = getimagesize($imagePath))
{
trigger_error("GD: Image does not exist. Must be gif, jpeg, or png!", E_USER_ERROR);
}
switch ($attr[2])
{
case 1:
$image = imagecreatefromgif($imagePath);
break;
case 2:
$image = imagecreatefromjpeg($imagePath);
break;
case 3:
$image = imagecreatefrompng($imagePath);
break;
default:
exit;
}
if(!$image)
{
exit;
}
$newimage = imagecreatetruecolor($x,$y);
imagecopyresampled($newimage, $image, 0, 0, 0, 0, $x, $y, $attr[0], $attr[1]);
imagejpeg($newimage, $newImagePath);
imagedestroy($image);
imagedestroy($newimage);
}
variable 06-14-2005, 01:49 AM getimagesize() says it supports .png, but so far every time i run a png through it, it locks up completely.
anyone else having this problem?
serversphere 06-14-2005, 07:10 AM Originally posted by variable
getimagesize() says it supports .png, but so far every time i run a png through it, it locks up completely.
anyone else having this problem?
Does your image filename have any special chars in it? Spaces, underscores, etc?
Phach 06-14-2005, 07:22 AM Originally posted by USWEB-Darren
I love GD, but it depeds on if you have it available or not. Here's a quick example with getimagesize() that limits height to 200px or less I used for a client's site:
with your script, if you have a big picture (2048x2048) it will load the large file (about 2-3 MB) and resize it to 200px.
I find it bad programming... Sure, it depends on your needs, but I won't never advice such a thing.
Use GD library, or ImageMagick. It's really safer and useful for the final consumer.
serversphere 06-14-2005, 07:45 AM Originally posted by Phach
with your script, if you have a big picture (2048x2048) it will load the large file (about 2-3 MB) and resize it to 200px.
I find it bad programming... Sure, it depends on your needs, but I won't never advice such a thing.
Use GD library, or ImageMagick. It's really safer and useful for the final consumer.
Agreed - it is bad programming. He he. Which is why I called it a "quick example". It doesn't check for validity of the image filename (no special chars), doesn't look at image file size, or check to be sure the image even exists! For this client, I control all of the images on the site so I really don't need to worry about these things. But I do appreciate you calling me out and slapping my wrist. ;)
Phach 06-14-2005, 08:00 AM it's not personnal ;)
serversphere 06-14-2005, 08:17 AM It wasn't taken personally, but you were right I should preface a quick code excerpt like that with something like "I wouldn't use this live but..."
variable 06-14-2005, 10:36 AM well i did use the function, but modified it.
it looks like this
//resize image if necessary
function resizeimg($img){
if($img){
list($width, $height, $type, $attr) = getimagesize($img);
echo "<img src=\"" . $img . "\" width=\"";
if ($width>450) {
echo "450";
} else {
echo $width;
}
echo "\">";
}
}
and say i feed it either:
$img = "http://www.libpng.org/pub/png/img_png/pnglogo-grr.png";
$img = "http://www.gamenation.org/sitefiles/pagefiles/header.png";
just as random .png to use. neither one loads.
if i feed it a .jpg, it works fine or gif. such as
http://www.webhostingtalk.com/whtimages_blue/interface/logo_new.gif
.
serversphere 06-14-2005, 11:34 AM Never used PNGs with it, so unsure how to help. You might want to post on the php newsgroup (at news://news.php.net) and see if you can get someone there to help out.
variable 06-14-2005, 01:14 PM btw is there any way that yo can tell if gd is installed on your server?
Phach 06-14-2005, 01:20 PM you can see it with the php infos function
variable 06-14-2005, 02:28 PM good news for me: i found out i do have gd installed on the server(i guess it is pretty much standard?) so what is the best way to do this with gd(since it seems that gd doesnt need to load the entire image to work, thus faster page loads?)?
thanks
|