PHP Automatic Generation Thumbnail Function Source Code Example

  • 2021-12-04 09:43:16
  • OfStack

1 simple but more complete function of automatic generation of thumbnail function, you can zoom, cropping, lock the width or height of the picture, use blank fill

The following is the source code, which is relatively simple. I believe it is easy to understand. Remember to open the support of GD library:


<?php
/**
 *  Generate thumbnails 
 * @param string    Absolute complete address of source image { With file name and suffix }
 * @param string    Absolute complete address of target map { With file name and suffix }
 * @param int     Thumbnail width { Value is set to 0 The target height cannot be 0 The target width is the source image width *( Target height / Source image height )}
 * @param int     Thumbnail height { Value is set to 0 The target width cannot be 0 The target height is the height of the source image *( Target width / Source image width )}
 * @param int     Cut or not { Width , High must not 0}
 * @param int/float  Zoom {0: Non-scaling , 0<this<1: Zoom to the appropriate scale ( At this time, the width and height limit and cutting are invalid )}
 * @return boolean
 */
function img2thumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0)
{
  if(!is_file($src_img))
  {
    return false;
  }
  $ot = fileext($dst_img);
  $otfunc = 'image' . ($ot == 'jpg' ? 'jpeg' : $ot);
  $srcinfo = getimagesize($src_img);
  $src_w = $srcinfo[0];
  $src_h = $srcinfo[1];
  $type = strtolower(substr(image_type_to_extension($srcinfo[2]), 1));
  $createfun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);
  $dst_h = $height;
  $dst_w = $width;
  $x = $y = 0;
  /**
   *  Thumbnails do not exceed the size of the source image (provided that the width or height is only 1 A) 
   */
  if(($width> $src_w && $height> $src_h) || ($height> $src_h && $width == 0) || ($width> $src_w && $height == 0))
  {
    $proportion = 1;
  }
  if($width> $src_w)
  {
    $dst_w = $width = $src_w;
  }
  if($height> $src_h)
  {
    $dst_h = $height = $src_h;
  }
  if(!$width && !$height && !$proportion)
  {
    return false;
  }
  if(!$proportion)
  {
    if($cut == 0)
    {
      if($dst_w && $dst_h)
      {
        if($dst_w/$src_w> $dst_h/$src_h)
        {
          $dst_w = $src_w * ($dst_h / $src_h);
          $x = 0 - ($dst_w - $width) / 2;
        }
        else
        {
          $dst_h = $src_h * ($dst_w / $src_w);
          $y = 0 - ($dst_h - $height) / 2;
        }
      }
      else if($dst_w xor $dst_h)
      {
        if($dst_w && !$dst_h) // Width without height 
        {
          $propor = $dst_w / $src_w;
          $height = $dst_h = $src_h * $propor;
        }
        else if(!$dst_w && $dst_h) // Height and width 
        {
          $propor = $dst_h / $src_h;
          $width = $dst_w = $src_w * $propor;
        }
      }
    }
    else
    {
      if(!$dst_h) // There is no height when cutting 
      {
        $height = $dst_h = $dst_w;
      }
      if(!$dst_w) // No width when cutting 
      {
        $width = $dst_w = $dst_h;
      }
      $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1);
      $dst_w = (int)round($src_w * $propor);
      $dst_h = (int)round($src_h * $propor);
      $x = ($width - $dst_w) / 2;
      $y = ($height - $dst_h) / 2;
    }
  }
  else
  {
    $proportion = min($proportion, 1);
    $height = $dst_h = $src_h * $proportion;
    $width = $dst_w = $src_w * $proportion;
  }
  $src = $createfun($src_img);
  $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h);
  $white = imagecolorallocate($dst, 255, 255, 255);
  imagefill($dst, 0, 0, $white);
  if(function_exists('imagecopyresampled'))
  {
    imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
  }
  else
  {
    imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
  }
  $otfunc($dst, $dst_img);
  imagedestroy($dst);
  imagedestroy($src);
  return true;
}
function fileext($file)
{
  return pathinfo($file, PATHINFO_EXTENSION);
}
?>

Use example:


<?php
$src_img = "./test.jpg"; // Full path and name of the original picture with picture extension 
$dst_img = "./test_thumb.jpg"; // The full path and name of the generated thumbnail 
/*  Generated width 300px , high 200px The thumbnail of the, without cropping, and the blank part will be filled with the background color  */
$stat = img2thumb($src_img, $dst_img, $width = 300, $height = 200, $cut = 0, $proportion = 0);
if($stat){
  echo 'Resize Image Success!<br />';
  echo '<img src="'.$dst_img.'" />';  
}else{
  echo 'Resize Image Fail!'; 
}
?>

Summarize


Related articles: