Resizing images with GD library

Just looking for some help with this. I’ve basically used this in the past, and its worked fine.

But I’m working on a site now where my client is using some small images as well as larger images.

Up until now I think I had it in my head that the script restricted the size of larger images to a max height and width. So I thought that if the image was smaller than the max height and width it would just be shown as its actual size.

But I’ve now realised its enlarging smaller images, which in this scenario I don’t really want to do, as it results in this sort of thing :

http://www.travel7107.com/destinationDetails.php?destinationID=37

The script looks like :


<?php 
// Max Sizes 
$th_max_width = 182;  // Maximum width of the thumbnail 
$th_max_height = 182; // Maximum height of the thumbnail 

//------------------- 

// File 
$filename = $_GET['file']; 

// Get image sizes, type & mime 
$img_data = getimagesize($filename); 
$width = $img_data[0]; 
$height = $img_data[1]; 
$img_type = $img_data[2]; // Numeric value (check in manual) 
$mime = $img_data['mime']; 

// Content type 
header('Content-type: ' . $mime); 


// Resize 
$ratio = ($width > $height) ? $th_max_width/$width : $th_max_height/ 
$height; 
$newwidth = round($width * $ratio); 
$newheight = round($height * $ratio); 

// Load 
$thumb = imagecreatetruecolor($newwidth, $newheight); 

switch ($img_type){ 
case 1: 
  $source = imagecreatefromgif($filename); 
  break; 

case 2: 
  $source = imagecreatefromjpeg($filename); 
  break; 
//  Other file types... 
} 

//Resize 
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight,   
$width, $height); 

// Output 
imagejpeg($thumb); 
?> 

<?php 
exit; 
?> 

Can this be amended to show images smaller than 182 x 182 in their original size?

Thanks.

Replacing


//Resize 
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight,   
$width, $height); 

// Output 
imagejpeg($thumb); 
?> 

with


// if the new dimensions are smaller then the original dimensions
if ($newwidth <= $width && $newheight <= $height)
{
  //Resize 
  imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight,   
  $width, $height); 

  // Output thumbnail
  imagejpeg($thumb); 
}
else // the new dimensions are not smaller than the original dimensions
{
  // Output original image
  imagejpeg($source);
}
?> 

should do the trick :slight_smile:

Thank you - that indeed did do the trick.

:slight_smile: