resize an image whilst keeping proprotions correct...

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

User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

resize an image whilst keeping proprotions correct...

Post 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
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post 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
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Your script isn't finding the image it's trying to resize.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Is (real) a real type? If so, what is it?
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post 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
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post 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
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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. :?
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post 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áä
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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.
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

What does "include ('http://www.madashatters.com/header.php');" send? Coz I think it's something in that file.
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

onion2k wrote:What does "include ('http://www.madashatters.com/header.php');" send? Coz I think it's something in that file.
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
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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.
Post Reply