?
Solved

resampling image with GD LIbrary

Posted on 2006-03-22
6
Medium Priority
?
670 Views
Last Modified: 2012-06-27
What is the GD Library script to resample an image to 100 pixels tall in good quality?
0
Comment
Question by:weikelbob
  • 3
  • 3
6 Comments
 
LVL 6

Expert Comment

by:Brian Bush
ID: 16266305
<?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);
  }
?>
0
 
LVL 7

Author Comment

by:weikelbob
ID: 16291864
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.
0
 
LVL 6

Expert Comment

by:Brian Bush
ID: 16292065
You can use imagecreatefromgif and it works fine.

You can swap the $_dh and $_dw for the 100px height.
--brian
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Author Comment

by:weikelbob
ID: 16292073
How do I test to see if it's a gif or jpg, then run the appropriate script?

Thanks for the good script, BTW.
0
 
LVL 6

Accepted Solution

by:
Brian Bush earned 1200 total points
ID: 16294227
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
0
 
LVL 7

Author Comment

by:weikelbob
ID: 16404153
Brian,

I'm still implimenting this.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question