Amit Dhamu

Writes Code

On The Fly Thumbnails

// thumbnail.php
<?php

  $image_path = $_GET["img"];

  // Exact Size
 $thumb_width = (int)$_GET["width"];
 $thumb_height = (int)$_GET["height"];

  // Maintain Aspect Ratio
  $max_width = (int)$_GET["maxw"];
    $max_height = (int)$_GET["maxh"];

  $resize_type = ($max_width && $max_height) ? "scale" : "exact";

  $img = NULL;

 $extension = strtolower(end(explode(".", $image_path)));

  if($extension == "jpg" || $extension == "jpeg") {

     $img = @imagecreatefromjpeg($image_path)
          or die("Cannot create new JPEG image");

 } else if($extension == "png") {

     $img = @imagecreatefrompng($image_path)
           or die("Cannot create new PNG image");

 } else if($extension == "gif") {

     $img = @imagecreatefromgif($image_path)
           or die("Cannot create new GIF image");

 }

 if($img) {

     $original_width = imagesx($img);
      $original_height = imagesy($img);

      switch($resize_type) {

          case "scale":
               $scale = min($max_width/$original_width, $max_height/$original_height);

             if($scale < 1) {

                 $new_width = floor($scale*$original_width);
                   $new_height = floor($scale*$original_height);

                 $tmp_img = imagecreatetruecolor($new_width, $new_height);

                 imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);

                 imagedestroy($img);
                   $img = $tmp_img;
              }    
             break;

          case "exact":

              $scale = max($thumb_width/$original_width, $thumb_height/$original_height);

             if($scale < 1) {

                 $new_width = floor($scale*$original_width);
                   $new_height = floor($scale*$original_height);

                 $tmp_img = imagecreatetruecolor($new_width, $new_height);
                 $tmp2img = imagecreatetruecolor($thumb_width, $thumb_height);

                 imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);

                 if($new_width == $thumb_width) {

                     $yAxis = ($new_height/2) - ($thumb_height/2);
                     $xAxis = 0;

                 } else if ($new_height == $thumb_height)  {

                     $yAxis = 0;
                       $xAxis = ($new_width/2) - ($thumb_width/2);

                 } 

                 imagecopyresampled($tmp2img, $tmp_img, 0, 0, $xAxis, $yAxis, $thumb_width, $thumb_height, $thumb_width, $thumb_height);

                 imagedestroy($img);
                   imagedestroy($tmp_img);
                   $img = $tmp2img;
              }  
               break;
        }
     header("Content-type: image/jpeg");
     imagejpeg($img);
  }
?>

// demo.html
<img src="thumbnail.php?img=image.jpg&width=200&height=100" alt=""/>


Source: WebGeekly


thumbnail generation / php

← Fixed Position on Scroll iOS and Android Detection →