resampling image with GD LIbrary

What is the GD Library script to resample an image to 100 pixels tall in good quality?
LVL 7
weikelbobAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian BushSolutions ArchitectCommented:
<?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);
  }
?>
weikelbobAuthor Commented:
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.
Brian BushSolutions ArchitectCommented:
You can use imagecreatefromgif and it works fine.

You can swap the $_dh and $_dw for the 100px height.
--brian
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

weikelbobAuthor Commented:
How do I test to see if it's a gif or jpg, then run the appropriate script?

Thanks for the good script, BTW.
Brian BushSolutions ArchitectCommented:
Try this code:

<?php

$_inFile = "C:\before-after.jpg";
$_info = getimagesize($_inFile);

$_outFile = "C:\imageOut";

if ($_info[2] == IMG_JPG) {
    $_src = imagecreatefromjpeg($_inFile);
} elseif ($_info[2] == IMG_GIF) {
    $_src = imagecreatefromgif($_inFile);
} else {
      echo "You can add other types later, but right its just GIF and JPG.";
}

if($_src) {
    $_dh = 100;
    $_dw = ($_info[0] / $_info[1]) * $_dh;
    $_dest = imagecreatetruecolor($_dw,$_dh);
    imagecopyresampled($_dest, $_src, 0, 0, 0, 0, $_dw, $_dh, $_info[0], $_info[1]);

    if ($_info[2] == IMG_JPG) {
        imagejpeg($_dest, $_outFile . ".jpg", 75);
    } elseif ($_info[2] == IMG_GIF) {
        imagegif($_dest, $_outFile . ".gif", 75);
    } else {
          echo "You can add other types later, but right its just GIF and JPG.";
    }

    imagedestroy($_src);
    imagedestroy($_dest);
}
?>


--brian

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
weikelbobAuthor Commented:
Brian,

I'm still implimenting this.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.