Jump to content

Archived

This topic is now archived and is closed to further replies.

craygo

Dynamic thumbnail creation

Recommended Posts

Here is a function that will resize an image to a specified maxsize. The maxsize is either width or height. It will calculate which one to use and then resizes but keeps the original aspect ratio so the images are not stretched.

 

<?php
/**********************************
*  Will resize an image to a      *
*  max width or height and keep   *
*  aspect ratio Name this file    *
*  anything you like.             *
*  I will use imageresize.php     *
**********************************/

header('Content-type: image/jpeg');
function resampleimage($maxsize, $sourcefile, $imgcomp=0){
// SET THE IMAGE COMPRESSION
$g_imgcomp=100-$imgcomp;
  // CHECK TO SEE IF THE IMAGE EXISTS FIRST
  if(file_exists($sourcefile)){
  // FIRST WE GET THE CURRENT IMAGE SIZE
  $g_is=getimagesize($sourcefile);
    /********* CALCULATE THE WIDTH AND THE HEIGHT ***************/
    // CHECK TO SEE IF THE WIDTH AND HEIGHT ARE ALREADY SMALLER THAN THE MAX SIZE
    if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize){
    // LEAVE WIDTH AND HEIGHT ALONE IF IMAGE IS SMALLER THAN MAXSIZE
    $new_width=$g_is[0];
    $new_height=$g_is[1];
    } else {
    // GET VALUE TO CALCULATE WIDTH AND HEIGHT
    $w_adjust = ($maxsize / $g_is[0]);
    $h_adjust = ($maxsize / $g_is[1]);
      // CHECK TO WHICH DIMENSION REQUIRES THE SMALLER ADJUSTMENT
      if($w_adjust <= $h_adjust){
      // CALCULATE WIDTH AND HEIGHT IF THE WIDTH VALUE IS SMALLER
      $new_width=($g_is[0]*$w_adjust);
      $new_height=($g_is[1]*$w_adjust);
      } else {
      // CALCULATE WIDTH AND HEIGHT IF THE HEIGHT VALUE IS SMALLER
      $new_width=($g_is[0]*$h_adjust);
      $new_height=($g_is[1]*$h_adjust);
      }
    }
  //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER THE LAST "." )
$image_type = strrchr($sourcefile, ".");

//SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
switch($image_type) {
	case '.jpg':
		$img_src = imagecreatefromjpeg($sourcefile);
		break;
	case '.jpeg':
		$img_src = imagecreatefromjpeg($sourcefile);
		break;
	case '.png':
		$img_src = imagecreatefrompng($sourcefile);
		break;
	case '.gif':
		$img_src = imagecreatefromgif($sourcefile);
		break;
	default:
		echo("Error Invalid Image Type");
		die;
		break;
}
  // CREATE THE TRUE COLOR IMAGE WITH NE WIDTH AND HEIGHT
  $img_dst=imagecreatetruecolor($new_width,$new_height);
  // RESAMPLE THE IMAGE TO NEW WIDTH AND HEIGHT
  imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]);
  // OUTPUT THE IMAGE AS A JPEG.
  // THIS CAN BE CHANGED IF YOU WANT TRANSPARENCY OR PREFER ANOTHER FORMAT. MAKE SURE YOU CHANGE HEADER ABOVE.
  imagejpeg($img_dst);
  // DESTROY THE NEW IMAGE
  imagedestroy($img_dst);
  return true;
  } else {
  return false;
  }
}
// NOW CALL THE IMAGE FROM ANY OTHER PAGE WITH <img src="imageresize.php?maxsize=xxx&source=path/to/image/file" border=0 /> xxx=a value for the max size
resampleimage($_GET['maxsize'], $_GET['source']);
?>

 

Now you can do this for a folder loop

<?php
$files = glob('images/*.jpg');
  foreach($files as $f){
  echo "<img src=\"imageresize.php?maxsize=200&source=".$f."\" />\n";
  }
?>

 

Or in a while loop from a database

<?php
// if path to pictures is not included with filename add here
$image_path = 'images/';
// do sql statement and run query below

// start while loop
while($row = mysql_fetch_assoc($result)){
  echo "<img src=\"imageresize.php?maxsize=200&source=".$image_path.$row['image']."\" />\n";
}
?>

 

Hope it is useful

 

Ray

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.