Page 1 of 2
resizing images in php
Posted: Sat Apr 16, 2005 12:41 pm
by soianyc
I am currently setting up site to display my garments. Ive decided use one file and resize the images in php. Ive read a lot about GD. Still not to sure how it is implemnted though. What happens is when i resize the image, it looks like crap. This is the code im using.
Code: Select all
<? function setImageSize($image_file) {
$maxSize = 300; // set this varible to max width or height
$image_size = getimagesize($image_file,&$image_info);
$width = $image_size[0];
$height = $image_size[1];
if($width > $maxSize || $height > $maxSize) {
if($width > $maxSize) {
$z = $width;
$i = 0;
while($z > $maxSize) {
--$z; ++$i;
}
$imgSizeArray[0] = $z;
$imgSizeArray[1] = $height - ($height * ($i / $width));
} else {
$z = $height;
$i = 0;
while($z > $maxSize) {
--$z; ++$i;
}
$imgSizeArray[0] = $width - ($width * ($i / $height));
$imgSizeArray[1] = $z;
}
} else {
$imgSizeArray[0] = $width;
$imgSizeArray[1] = $height;
}
return $imgSizeArray;
} ?>
<? $imgSize = setImageSize("8200fwmc4.jpg"); ?>
<img src="8200fwmc4.jpg" width="<? echo $imgSize[0];?>" height="<? echo $imgSize[1]?>">
how can i use GD to get nice crisper images???
Posted: Sat Apr 16, 2005 1:33 pm
by scorphus
The [url=http://www.php.net/imagecopyresampled]imagecopyresampled[/url]() function reference wrote:Code: Select all
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>[/quote]
Now just adapt it to fit your needs. You might wanna use $_GET['filename'] to pass the image by GET and use something like <img src="res_img.php?filename=8200fwmc4.jpg">
-- Scorphus
Posted: Sat Apr 16, 2005 2:01 pm
by feyd
setImageSize() seems to waste a lot of cycles getting the difference between $maxSize and $width or $height...

Posted: Tue Apr 19, 2005 10:47 am
by soianyc
I tried out the code you gave me. When i run the script i get thousands of lines of crap.
Why????
Posted: Tue Apr 19, 2005 10:53 am
by feyd
did you add the script to an existing script? That's why.
The script example scorphus posted is a stand-alone script. It cannot have any other junk around it or you will get garbage on the browser. The script generates an image directly to the browser.
Posted: Tue Apr 19, 2005 11:11 am
by vigge89
feyd wrote:did you add the script to an existing script? That's why.
The script example scorphus posted is a stand-alone script. It cannot have any other junk around it or you will get garbage on the browser. The script generates an image directly to the browser.
... which basically means that the script generates (and outputs) an image, which you link to like a normal image:
Code: Select all
<img src='generate_img.php' alt='' />

Posted: Tue Apr 19, 2005 11:17 am
by soianyc
No i added no other code to the script. Just made a file with that script and only that script in it. Still not gettting anything though
Posted: Tue Apr 19, 2005 11:21 am
by soianyc
Now i get about 2 lines of mumbo jumbo outputting. I checked to see if i havd GD installed, using phpinfo, which i do. Im not sure whats going on now.
Posted: Tue Apr 19, 2005 12:52 pm
by onion2k
Are you sending the right header()?
Posted: Tue Apr 19, 2005 12:55 pm
by soianyc
are you talking about this??
Code: Select all
header('Content-type: image/jpeg');
Forgive the dumb questions, i am a noob.
Posted: Tue Apr 19, 2005 12:58 pm
by onion2k
Well, that should be the right header, but it depends on the image. Are you using Scorphus' code?
Posted: Tue Apr 19, 2005 1:00 pm
by soianyc
yep, im using scorphus code. I added my own filename, but the bigger the file i put into the script the more jibberish i get coming out. This seems to tell me that the code in itself is working. I cannot output the image though.
Posted: Tue Apr 19, 2005 1:46 pm
by feyd
are you sure you don't have extra stuff in the file? Line breaks of any sort on either side of the <? and ?> will do it... literally anything outside of php tags will... along with any echo's will mangle the output.
Posted: Tue Apr 19, 2005 1:51 pm
by soianyc
No junk at all either inside or outside the <? ?>. Im confused at this point, maybe its not a php issue at all???
Posted: Tue Apr 19, 2005 1:55 pm
by soianyc
My mistake, i found a short gd script and ran it with the same results. I had one extra line above the first <? and was getting all that crap. Wow i cant believe that was it.
Thanx all