Link to home
Create AccountLog in
Avatar of weikelbob
weikelbobFlag for United States of America

asked on

resampling image with GD LIbrary

What is the GD Library script to resample an image to 100 pixels tall in good quality?
Avatar of Brian Bush
Brian Bush
Flag of United States of America image

<?php
  $_src = imagecreatefromjpeg("C:\DSCN1311.JPG");
  if($_src) {
      $_sw = imagesx($_src);
      $_sh = imagesy($_src);

      $_dw = 100; // Largest target dimension
      $_dh = ($_sh / $_sw) * $_dw;
      $_dest = imagecreatetruecolor($_dw,$_dh);
      imagecopyresampled($_dest, $_src, 0, 0, 0, 0, $_dw, $_dh, $_sw, $_sh);

      imagejpeg($_dest, "c:\out.jpg", 75);
      imagedestroy($_src);
      imagedestroy($_dest);
  }
?>
Avatar of weikelbob

ASKER

Great. Can I check to see if it's a GIF and do something else in that case?  The max height should be 100px

Thanks.
You can use imagecreatefromgif and it works fine.

You can swap the $_dh and $_dw for the 100px height.
--brian
How do I test to see if it's a gif or jpg, then run the appropriate script?

Thanks for the good script, BTW.
ASKER CERTIFIED SOLUTION
Avatar of Brian Bush
Brian Bush
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Brian,

I'm still implimenting this.