Page 1 of 2
resize an image whilst keeping proprotions correct...
Posted: Tue Dec 06, 2005 5:40 pm
by ericburnard
im not sure if this is the correct forum but heres the question (please move if it is) i have an image, say 1200 x 850, and i wanted it to be displayed as half the size but whilst keeping the proportions correct so that the image is not distorted. I saw a code a while ago that let you do this. does anyone no of it?
thanks
eric
Posted: Tue Dec 06, 2005 5:45 pm
by trukfixer
tried searching the forums? also check php.net and search on imagecopyresized - php's manual has an example that does exactly what you want - resize an image to half size
http://us3.php.net/manual/en/function.i ... esized.php
Posted: Tue Dec 06, 2005 5:54 pm
by ericburnard
ok for some reason all i seem to get is a tonne of gibberish
http://www.madashatters.com/?image
any idea why??
Eric
Posted: Tue Dec 06, 2005 6:31 pm
by onion2k
Your script isn't finding the image it's trying to resize.
Posted: Tue Dec 06, 2005 8:50 pm
by AKA Panama Jack
Code: Select all
function percent($p, $w)
{
return (real)(100 * ($p / $w));
}
function unpercent($percent, $whole)
{
return (real)(($percent * $whole) / 100);
}
$imageinfo = getimagesize($filename);
$imagewidth = $imageinfo[0];
$imageheight = $imageinfo[1];
$maximagewidth = $imagewidth / 2;
$maximageheight = 3000;
if($maximagewidth < $imagewidth) {
$imageheight = unpercent(percent($maximagewidth, $imagewidth), $imageheight);
$imagewidth = $maximagewidth;
}
if($maximageheight < $imageheight) {
$imagewidth = unpercent(percent($maximageheight, $imageheight), $imagewidth);
$imageheight = $maximageheight;
}
The $maximagewidth and $maximageheight is the size you want the image. It will keep the proportions correct. If you have a maximum width for an image of say 640 you can set the maximum height for the image to 5000 if you want. The height will be set to match the proper aspect ratio.
The above example will halve the image width while setting the height to the proper value for the correct aspect ratio even though the maximum height is set to 3000.
The $imagewidth and $imageheight will contain the values you should use for the new image size.
I use the above routine to keep the image width for all displayed images to 80 pixels wide while allowing the the height to be any size and it keeps the aspect ratio correct for all images.
Posted: Tue Dec 06, 2005 9:01 pm
by Ambush Commander
Is (real) a real type? If so, what is it?
Posted: Tue Dec 06, 2005 10:41 pm
by RobertPaul
It's an alias for (float), I'm guessing...
Code: Select all
$ php -r '$var = 23.456*32.857342; $var2 = (real)$var; echo $var2;'
770.701813952
$ php -r '$var = 23.456*32.857342; $var2 = (int)$var; echo $var2;'
770
$ php -r '$var = 23.456*32.857342; $var2 = (float)$var; echo $var2;'
770.701813952
$ php -r '$var = 23.456*32.857342; $var2 = (gibberish)$var; echo $var2;'
PHP Parse error: syntax error, unexpected T_VARIABLE in Command line code on line 1
Posted: Wed Dec 07, 2005 4:55 am
by ericburnard
ok that eror was not what i had last night.
have played around with that peice of code and the others on the php.net site and have got one to work perfectly.
but... now i have tried to put a header and footer in it dosnt seem to want to work. i have tried just putting the html code infront but it still wont work. (
http://www.madashatters.com/gallery/ima ... 0_1271.jpg)
any advise would be great
thanks
eric
Posted: Wed Dec 07, 2005 5:04 am
by foobar
That last link doesn't even want to load fully. And it's not my connection that is at fault. I'm using my unis 2Gbit connection here.

Posted: Wed Dec 07, 2005 5:07 am
by ericburnard
which link the ,
http://www.madashatters.com/gallery/ima ... 0_1271.jpg , one.
well all i get on the page is my header displayed then the following
Code: Select all
Warning: Cannot modify header information - headers already sent by (output started at /home/madashat/public_html/gallery/image.php:4) in /home/madashat/public_html/gallery/image.php on line 38
ÿØÿàJFIFÿþΉø•"D‰J‚ís¢LÒ,J2áä
Posted: Wed Dec 07, 2005 5:08 am
by foobar
It's a good habit to read your error messages:
Warning: Cannot modify header information - headers already sent by (output started at /home/madashat/public_html/gallery/image.php:4) in /home/madashat/public_html/gallery/image.php on line 38
You need to send your header() stuff before you try to output the image.
Posted: Wed Dec 07, 2005 5:17 am
by ericburnard
my header was sent before the image was created. i have my include before, but i have also placed it at different points after the header but it still wont work.
Code: Select all
<?
include ('http://www.madashatters.com/header.php');
// The file
$filename2 = $_GET['image'];
$filename = 'http://www.madashatters.com/gallery/gallery/all/'.$filename2.'#';
// Set a maximum height and width
$width = 600;
$height = 600;
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Have tried putting the include here and at the bottom of the page but still getting the same errors
// Output
imagejpeg($image_p, null, 100);
?>
Thanks
Eric
Posted: Wed Dec 07, 2005 5:25 am
by onion2k
What does "include ('
http://www.madashatters.com/header.php');" send? Coz I think it's something in that file.
Posted: Wed Dec 07, 2005 5:31 am
by ericburnard
This is the header.php file but i cant see anything that would affect it as its more or less all html :S
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/><title>.:: Eric - Buy One Get One Free ::.</title>
<style type="text/css" media="screen">
@import url(http://www.madashatters.com/style.css);
</style>
</head>
<body>
<div id="thebox">
<div id="logo">MAD<strong>AS</strong>HATTERS</div>
<div id="nav">
<ul>
<li><a href="http://www.madashatters.com/?blog">Home</a></li>
<li><a href="http://www.madashatters.com/?me">About</a></li>
<li><a href="http://www.madashatters.com/?archives">Archives</a></li>
<li><a href="http://www.madashatters.com/?comments">Comments</a></li>
<li><a href="http://www.madashatters.com/?pictures">Gallery</a></li>
<li><a href="http://www.madashatters.com/?links">Links</a></li>
<li><a href="http://www.madashatters.com/?contact">Contact</a></li>
</ul>
</div>
<div id="content">
Eric
Posted: Wed Dec 07, 2005 6:35 am
by foobar
ericburnard wrote:This is the header.php file but i cant see anything that would affect it as its more or less all html :S
There you go. It _outputs_ stuff. That's your problem. You can't send headers after outputting anything.