当前位置:首页 > PHP教程 > php图像处理 > 列表

PHP自动生成缩略图函数的源码示例

发布:smiling 来源: PHP粉丝网  添加日期:2021-11-13 11:49:17 浏览: 评论:0 

今天小编就为大家分享一篇关于PHP自动生成缩略图函数的源码示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧。

一个简单但功能比较完善的自动生成缩略图的函数,可以按需要对图片进行缩放、裁切、锁定宽或高、使用空白填充

以下为源码,比较简单,相信很容易看明白,记得打开 GD 库的支持哦:

  1. <?php 
  2. /** 
  3.  * 生成缩略图 
  4.  * @param string   源图绝对完整地址{带文件名及后缀名} 
  5.  * @param string   目标图绝对完整地址{带文件名及后缀名} 
  6.  * @param int    缩略图宽{值设为0时目标高度不能为0,目标宽度为源图宽*(目标高度/源图高)} 
  7.  * @param int    缩略图高{值设为0时目标宽度不能为0,目标高度为源图高*(目标宽度/源图宽)} 
  8.  * @param int    是否裁切{宽,高必须非0} 
  9.  * @param int/float 缩放{0:不缩放, 0<this<1:缩放到相应比例(此时宽高限制和裁切均失效)} 
  10.  * @return boolean 
  11.  */ 
  12. function img2thumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0) 
  13. { 
  14.   if(!is_file($src_img)) 
  15.   { 
  16.     return false; 
  17.   } 
  18.   $ot = fileext($dst_img); 
  19.   $otfunc = 'image' . ($ot == 'jpg' ? 'jpeg' : $ot); 
  20.   $srcinfo = getimagesize($src_img); 
  21.   $src_w = $srcinfo[0]; 
  22.   $src_h = $srcinfo[1]; 
  23.   $type = strtolower(substr(image_type_to_extension($srcinfo[2]), 1)); 
  24.   $createfun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type); 
  25.   $dst_h = $height; 
  26.   $dst_w = $width; 
  27.   $x = $y = 0; 
  28.   /** 
  29.    * 缩略图不超过源图尺寸(前提是宽或高只有一个) 
  30.    */ 
  31.   if(($width> $src_w && $height> $src_h) || ($height> $src_h && $width == 0) || ($width> $src_w && $height == 0)) 
  32.   { 
  33.     $proportion = 1; 
  34.   } 
  35.   if($width> $src_w) 
  36.   { 
  37.     $dst_w = $width = $src_w; 
  38.   } 
  39.   if($height> $src_h) 
  40.   { 
  41.     $dst_h = $height = $src_h; 
  42.   } 
  43.   if(!$width && !$height && !$proportion) 
  44.   { 
  45.     return false; 
  46.   } 
  47.   if(!$proportion) 
  48.   { 
  49.     if($cut == 0) 
  50.     { 
  51.       if($dst_w && $dst_h) 
  52.       { 
  53.         if($dst_w/$src_w> $dst_h/$src_h) 
  54.         { 
  55.           $dst_w = $src_w * ($dst_h / $src_h); 
  56.           $x = 0 - ($dst_w - $width) / 2; 
  57.         } 
  58.         else 
  59.         { 
  60.           $dst_h = $src_h * ($dst_w / $src_w); 
  61.           $y = 0 - ($dst_h - $height) / 2; 
  62.         } 
  63.       } 
  64.       else if($dst_w xor $dst_h) 
  65.       { 
  66.         if($dst_w && !$dst_h) //有宽无高 
  67.         { 
  68.           $propor = $dst_w / $src_w; 
  69.           $height = $dst_h = $src_h * $propor; 
  70.         } 
  71.         else if(!$dst_w && $dst_h) //有高无宽 
  72.         { 
  73.           $propor = $dst_h / $src_h; 
  74.           $width = $dst_w = $src_w * $propor; 
  75.         } 
  76.       } 
  77.     } 
  78.     else 
  79.     { 
  80.       if(!$dst_h) //裁剪时无高 
  81.       { 
  82.         $height = $dst_h = $dst_w; 
  83.       } 
  84.       if(!$dst_w) //裁剪时无宽 
  85.       { 
  86.         $width = $dst_w = $dst_h; 
  87.       } 
  88.       $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1); 
  89.       $dst_w = (int)round($src_w * $propor); 
  90.       $dst_h = (int)round($src_h * $propor); 
  91.       $x = ($width - $dst_w) / 2; 
  92.       $y = ($height - $dst_h) / 2; 
  93.     } 
  94.   } 
  95.   else 
  96.   { 
  97.     $proportion = min($proportion, 1); 
  98.     $height = $dst_h = $src_h * $proportion; 
  99.     $width = $dst_w = $src_w * $proportion; 
  100.   } 
  101.   $src = $createfun($src_img); 
  102.   $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h); 
  103.   $white = imagecolorallocate($dst, 255, 255, 255); 
  104.   imagefill($dst, 0, 0, $white); 
  105.   if(function_exists('imagecopyresampled')) 
  106.   { 
  107.     imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h); 
  108.   } 
  109.   else 
  110.   { 
  111.     imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h); 
  112.   } 
  113.   $otfunc($dst, $dst_img); 
  114.   imagedestroy($dst); 
  115.   imagedestroy($src); 
  116.   return true; 
  117. } 
  118. function fileext($file) 
  119. { 
  120.   return pathinfo($file, PATHINFO_EXTENSION); 
  121. } 
  122. ?> 

使用示例:

  1. <?php 
  2. $src_img = "./test.jpg"; //原图片完整路径和名称,带图片扩展名 
  3. $dst_img = "./test_thumb.jpg"; //生成的缩略图存放的完整路径和名称 
  4. /* 生成宽300px,高200px的缩略图,不进行裁切,空白部分将会使用背景色填充 */ 
  5. $stat = img2thumb($src_img, $dst_img, $width = 300, $height = 200, $cut = 0, $proportion = 0); 
  6. if($stat){ 
  7.   echo 'Resize Image Success!<br />'; 
  8.   echo '<img src="'.$dst_img.'" />';   
  9. }else{ 
  10.   echo 'Resize Image Fail!';  
  11. } 
  12. ?>

Tags: PHP自动生成缩略图

分享到: