resize an image whilst keeping proprotions correct...
Moderator: General Moderators
- ericburnard
- Forum Contributor
- Posts: 104
- Joined: Wed Jun 15, 2005 5:11 pm
- Location: Chesterfield, UK
resize an image whilst keeping proprotions correct...
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
thanks
eric
- trukfixer
- Forum Contributor
- Posts: 174
- Joined: Fri May 21, 2004 3:14 pm
- Location: Miami, Florida, USA
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
http://us3.php.net/manual/en/function.i ... esized.php
- ericburnard
- Forum Contributor
- Posts: 104
- Joined: Wed Jun 15, 2005 5:11 pm
- Location: Chesterfield, UK
ok for some reason all i seem to get is a tonne of gibberish
http://www.madashatters.com/?image
any idea why??
Eric
http://www.madashatters.com/?image
any idea why??
Eric
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
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 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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
-
RobertPaul
- Forum Contributor
- Posts: 122
- Joined: Sun Sep 18, 2005 8:54 pm
- Location: OCNY
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- ericburnard
- Forum Contributor
- Posts: 104
- Joined: Wed Jun 15, 2005 5:11 pm
- Location: Chesterfield, UK
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
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
- ericburnard
- Forum Contributor
- Posts: 104
- Joined: Wed Jun 15, 2005 5:11 pm
- Location: Chesterfield, UK
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
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áäIt's a good habit to read your error messages:
You need to send your header() stuff before you try to output the image.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![]()
- ericburnard
- Forum Contributor
- Posts: 104
- Joined: Wed Jun 15, 2005 5:11 pm
- Location: Chesterfield, UK
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.
Thanks
Eric
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);
?>Eric
What does "include ('http://www.madashatters.com/header.php');" send? Coz I think it's something in that file.
- ericburnard
- Forum Contributor
- Posts: 104
- Joined: Wed Jun 15, 2005 5:11 pm
- Location: Chesterfield, UK
This is the header.php file but i cant see anything that would affect it as its more or less all html :Sonion2k wrote:What does "include ('http://www.madashatters.com/header.php');" send? Coz I think it's something in that file.
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">