<?php
echo get_thumb('img/1.jpg');

/**
 * 图片压缩处理
 * @param string $sFile 源图片路径
 * @param int $iWidth 自定义图片宽度
 * @param int $iHeight 自定义图片高度
 * @param bool $format 文件格式是否为 源文件_宽度_高度.后缀
 * @return string 压缩后的图片路径
 */
function get_thumb($sFile, $iWidth = '', $iHeight = '', $format = false)
{
   $suffix = ['jpg', 'png', 'jpeg', 'webp'];

   //判断该图片是否存在
   if (!file_exists($sFile)) return $sFile;

   //判断图片格式(图片文件后缀)
   $extend = explode(".", $sFile);
   $attach_fileext = strtolower($extend[count($extend) - 1]);
   if (!in_array($attach_fileext, $suffix)) {
      return '支持的文件后缀:'.implode(',',$suffix);
   }

   if (!$iWidth && !$iHeight) {
      $imageInfo = getImageInfo($sFile);
      $iWidth = $imageInfo['width'];
      $iHeight = $imageInfo['height'];
   }

   //压缩图片文件名称
   if($format){
      $sFileNameS = str_replace("." . $attach_fileext, "_" . $iWidth . '_' . $iHeight . '.' . $attach_fileext, $sFile);
   }else{
      $sFileNameS = $sFile;
   }

   //生成压缩图片,并存储到原图同路径下
   resize_image($sFile, $sFileNameS, $iWidth, $iHeight);
   if (!file_exists($sFileNameS)) {
      return $sFile;
   }
   return $sFileNameS;
}

/**
 * 生成图片
 * @param string $im 源图片路径
 * @param string $dest 目标图片路径
 * @param int $maxwidth 生成图片宽
 * @param int $maxheight 生成图片高
 */
function resize_image($im, $dest, $maxwidth, $maxheight)
{
   $img = getimagesize($im);
   switch ($img[2]) {
      case 1:
         $im = @imagecreatefromgif($im);
         break;
      case 2:
         $im = @imagecreatefromjpeg($im);
         break;
      case 3:
         $im = @imagecreatefrompng($im);
         break;
      case 18:
          $im = @imagecreatefromwebp($im);
          break;
   }

   $pic_width = imagesx($im);
   $pic_height = imagesy($im);
   $resizewidth_tag = false;
   $resizeheight_tag = false;
   if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
      if ($maxwidth && $pic_width > $maxwidth) {
         $widthratio = $maxwidth / $pic_width;
         $resizewidth_tag = true;
      }

      if ($maxheight && $pic_height > $maxheight) {
         $heightratio = $maxheight / $pic_height;
         $resizeheight_tag = true;
      }

      if ($resizewidth_tag && $resizeheight_tag) {
         if ($widthratio < $heightratio)
            $ratio = $widthratio;
         else
            $ratio = $heightratio;
      }

      if ($resizewidth_tag && !$resizeheight_tag)
         $ratio = $widthratio;
      if ($resizeheight_tag && !$resizewidth_tag)
         $ratio = $heightratio;
      $newwidth = $pic_width * $ratio;
      $newheight = $pic_height * $ratio;

      if (function_exists("imagecopyresampled")) {
         $newim = imagecreatetruecolor($newwidth, $newheight);
         imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
      } else {
         $newim = imagecreate($newwidth, $newheight);
         imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
      }
      imagesavealpha($newim,true);
      imagewebp($newim,$dest);
      imagedestroy($newim);
   } else {
        imagesavealpha($im,true);
        imagewebp($im,$dest);
        imagedestroy($im);
   }
}

/**
 * 获取图片信息
 * @param string $img 源图片路径
 * @return array 图片信息
 */
function getImageInfo($img)
{
   $img_info = getimagesize($img);
   switch ($img_info[2]) {
      case 1:
         $imgtype = "GIF";
         break;
      case 2:
         $imgtype = "JPG";
         break;
      case 3:
         $imgtype = "PNG";
         break;
   }
   $img_type = $imgtype;
   $img_size = ceil(filesize($img) / 1000) . "k"; //获取文件大小
   $new_img_info = array(
      "width" => $img_info[0],
      "height" => $img_info[1],
      "type" => $img_type,
      "size" => $img_size
   );
   return $new_img_info;
}

发表回复

后才能评论