?
Solved

php re-size images

Posted on 2007-10-09
3
Medium Priority
?
175 Views
Last Modified: 2008-01-09
Hi, I have a website, where people upload their logos...unfortunatley they are all different sizes, some even huge...now I re-seize them manually...their logos are in three separate places on the website, so I can change the size of the image display. Bu the someone uploads a logo that is 150 x 250 rather than 250 x 150...so manually change sizes throughs some logos out.  so really my question is there a simple piece of PHP to auto resize? so if the logo is more horizontal than vertical it won't look to out of place?  Waffling now...lol Any help will be appreciated.
0
Comment
Question by:wood1e
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
nizsmo earned 2000 total points
ID: 20045744
I will share a piece of code which I wrote a long time ago, which resizes images to a certain size proportionally. The script is a bit long, but its quite easy to understand:

##############
<?php
require_once("imagescale.php");

// Getting image out of the database here first....not included since its different for everybody

// Testing Arguments
if(!is_numeric($_REQUEST['id'])) die("No image with the ID: ".$_REQUEST['id']);

//Grab the File
$result = retrieveModelImage($_REQUEST['id']);  // Database connection related, so function not included. But you can write your own

// And also the file type (picture type)
$filetype = retrieveModelImageType($_REQUEST['id']);  // Database connection related, also not included.

if($result == null) // if no picture uploaded yet?
{
      // Header information
      header("Content-Type: image/jpeg");
      
      // resize image
      $im = imagecreatefromjpeg('../images/npa.jpg');  // Default no image, change as you like
      
      
      $imageXy = "";
      
      $imageXy = scaleImage(imagesx($im), imagesy($im), "46",  "32");
      
      
      $imgw = $imageXy['x'];
      $imgh = $imageXy['y'];
      
      // create thumbnail
      $thumb=ImageCreateTrueColor($imgw,$imgh);
      ImageCopyResampled($thumb,$im,0,0,0,0,$imgw,$imgh,ImageSX($im),ImageSY($im));
      
      //display output
      $out = ImagejpeG($thumb);
      print($out);
      
      // clean memory
      imagedestroy ($im);
      imagedestroy ($thumb);
}
else  // Some image is there, so display it...
{
      //set the output type
      header("Content-type: $filetype");
      
      //print $result;
      
      
      // resize image
      $im = imagecreatefromstring($result);
      
      
      $imageXy = "";
      
      $imageXy = scaleImage(imagesx($im), imagesy($im), "46",  "32"); // Choose the dimensions HERE
      
      
      $imgw = $imageXy['x'];
      $imgh = $imageXy['y'];
      
      // create thumbnail
      $thumb=ImageCreateTrueColor($imgw,$imgh);
      ImageCopyResampled($thumb,$im,0,0,0,0,$imgw,$imgh,ImageSX($im),ImageSY($im));
      
      //display output
      $out = ImagejpeG($thumb);
      print($out);
      
      // clean memory
      imagedestroy ($im);
      imagedestroy ($thumb);
}


$db->sql_close();
unset($db);


?>

##################

Notice that there is also an include file, which I will provide below:

imagescale.php
#################
<?php
function scaleAxis($a, $b){
      //calculate the image axis while maintaining proportion
      return $b / (($a + 1) / $a);
}


function scaleImage($width, $height, $maxBoundW, $maxBoundH){
      //tests height vs width and scales the image based on the larger axis
      if ($height > $width){
 
            while ($width + 1 > $maxBoundW) {
                $height -= 1;         
                $width = scaleAxis($height, $width);
            }
            
            while ($height + 1 > $maxBoundH) {
                $height -= 1;         
                $width = scaleAxis($height, $width);
            }
            
      }elseif($width > $height){
              
                  while ($width + 1 > $maxBoundW) {
                      $width -= 1;         
                      $height = scaleAxis($width, $height);
                  }
                  
                  while ($height + 1 > $maxBoundH) {
                      $width -= 1;         
                      $height = scaleAxis($width, $height);
                  }
                  
      }else{
                  while ($width + 1 > $maxBoundW) {
                      $height -= 1;         
                      $width = scaleAxis($height, $width);
                  }
                  
                  while ($height + 1 > $maxBoundH) {
                      $height -= 1;         
                      $width = scaleAxis($height, $width);
                  }
      }
      
      $imageXy['x'] = $width;
      $imageXy['y'] = $height;
      
      return $imageXy;
}
?>
##################


Hope this helps get some idea of what you are wanting to do, the script does not work just copy and pasting though, you need to modify and add some functions. Also you can specify the dimensions in the above script where I have commented to change the dimensions.

To use, you have to enter something like:
http://www.mydomain.com/resizerFileName.php?id=25

Let me know how you go.
0
 

Author Comment

by:wood1e
ID: 20046641
crickeys, thats brilliant and many thanks for sharing...unfortunatly my PHP knowledge is not good enough for this...:)

Where I have the three iamges on separate pages, I have just added in the height and size for that specific page, but as I tried to explain above, that does not take into account logos, or different proportions...Is there not something slightly more simple?

I will give it ago though :)
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20046845
ok, good luck and let me know how you go and if you have any questions :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 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