resizing images in php

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

User avatar
soianyc
Forum Commoner
Posts: 61
Joined: Wed Mar 30, 2005 2:48 pm
Location: NY

resizing images in php

Post 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???
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

The [url=http://www.php.net/imagecopyresampled]imagecopyresampled[/url]() function reference wrote:

Code: Select all

&lt;?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);
?&gt;[/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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

setImageSize() seems to waste a lot of cycles getting the difference between $maxSize and $width or $height... :?
User avatar
soianyc
Forum Commoner
Posts: 61
Joined: Wed Mar 30, 2005 2:48 pm
Location: NY

Post by soianyc »

I tried out the code you gave me. When i run the script i get thousands of lines of crap.

Why????
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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='' />
:)
User avatar
soianyc
Forum Commoner
Posts: 61
Joined: Wed Mar 30, 2005 2:48 pm
Location: NY

Post 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
User avatar
soianyc
Forum Commoner
Posts: 61
Joined: Wed Mar 30, 2005 2:48 pm
Location: NY

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

Post by onion2k »

Are you sending the right header()?
User avatar
soianyc
Forum Commoner
Posts: 61
Joined: Wed Mar 30, 2005 2:48 pm
Location: NY

Post by soianyc »

are you talking about this??

Code: Select all

header('Content-type: image/jpeg');
Forgive the dumb questions, i am a noob.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Well, that should be the right header, but it depends on the image. Are you using Scorphus' code?
User avatar
soianyc
Forum Commoner
Posts: 61
Joined: Wed Mar 30, 2005 2:48 pm
Location: NY

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
soianyc
Forum Commoner
Posts: 61
Joined: Wed Mar 30, 2005 2:48 pm
Location: NY

Post by soianyc »

No junk at all either inside or outside the <? ?>. Im confused at this point, maybe its not a php issue at all???
User avatar
soianyc
Forum Commoner
Posts: 61
Joined: Wed Mar 30, 2005 2:48 pm
Location: NY

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