请选择 进入手机版 | 继续访问电脑版

ylsunyuan技术论坛中心

 找回密码
 注册(请使用中文注册)
搜索
热搜: 活动 交友 discuz
查看: 1137|回复: 0

php产生缩略图

[复制链接]

124

主题

127

帖子

619

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
619
发表于 2016-6-7 13:32:55 | 显示全部楼层 |阅读模式
核心类
  1. <?php

  2. /**
  3. * 生成缩略图
  4. * @author yangzhiguo0903@163.com
  5. * @param string     源图绝对完整地址{带文件名及后缀名}
  6. * @param string     目标图绝对完整地址{带文件名及后缀名}
  7. * @param int        缩略图宽{0:此时目标高度不能为0,目标宽度为源图宽*(目标高度/源图高)}
  8. * @param int        缩略图高{0:此时目标宽度不能为0,目标高度为源图高*(目标宽度/源图宽)}
  9. * @param int        是否裁切{宽,高必须非0}
  10. * @param int/float  缩放{0:不缩放, 0<this<1:缩放到相应比例(此时宽高限制和裁切均失效)}
  11. * @return boolean
  12. */
  13. function img2thumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0)
  14. {
  15.     if(!is_file($src_img))
  16.     {
  17.         return false;
  18.     }
  19.     $ot = fileext($dst_img);
  20.     $otfunc = 'image' . ($ot == 'jpg' ? 'jpeg' : $ot);
  21.     $srcinfo = getimagesize($src_img);
  22.     $src_w = $srcinfo[0];
  23.     $src_h = $srcinfo[1];
  24.     $type  = strtolower(substr(image_type_to_extension($srcinfo[2]), 1));
  25.     $createfun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);

  26.     $dst_h = $height;
  27.     $dst_w = $width;
  28.     $x = $y = 0;

  29.     /**
  30.      * 缩略图不超过源图尺寸(前提是宽或高只有一个)
  31.      */
  32.     if(($width> $src_w && $height> $src_h) || ($height> $src_h && $width == 0) || ($width> $src_w && $height == 0))
  33.     {
  34.         $proportion = 1;
  35.     }
  36.     if($width> $src_w)
  37.     {
  38.         $dst_w = $width = $src_w;
  39.     }
  40.     if($height> $src_h)
  41.     {
  42.         $dst_h = $height = $src_h;
  43.     }

  44.     if(!$width && !$height && !$proportion)
  45.     {
  46.         return false;
  47.     }
  48.     if(!$proportion)
  49.     {
  50.         if($cut == 0)
  51.         {
  52.             if($dst_w && $dst_h)
  53.             {
  54.                 if($dst_w/$src_w> $dst_h/$src_h)
  55.                 {
  56.                     $dst_w = $src_w * ($dst_h / $src_h);
  57.                     $x = 0 - ($dst_w - $width) / 2;
  58.                 }
  59.                 else
  60.                 {
  61.                     $dst_h = $src_h * ($dst_w / $src_w);
  62.                     $y = 0 - ($dst_h - $height) / 2;
  63.                 }
  64.             }
  65.             else if($dst_w xor $dst_h)
  66.             {
  67.                 if($dst_w && !$dst_h)  //有宽无高
  68.                 {
  69.                     $propor = $dst_w / $src_w;
  70.                     $height = $dst_h  = $src_h * $propor;
  71.                 }
  72.                 else if(!$dst_w && $dst_h)  //有高无宽
  73.                 {
  74.                     $propor = $dst_h / $src_h;
  75.                     $width  = $dst_w = $src_w * $propor;
  76.                 }
  77.             }
  78.         }
  79.         else
  80.         {
  81.             if(!$dst_h)  //裁剪时无高
  82.             {
  83.                 $height = $dst_h = $dst_w;
  84.             }
  85.             if(!$dst_w)  //裁剪时无宽
  86.             {
  87.                 $width = $dst_w = $dst_h;
  88.             }
  89.             $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1);
  90.             $dst_w = (int)round($src_w * $propor);
  91.             $dst_h = (int)round($src_h * $propor);
  92.             $x = ($width - $dst_w) / 2;
  93.             $y = ($height - $dst_h) / 2;
  94.         }
  95.     }
  96.     else
  97.     {
  98.         $proportion = min($proportion, 1);
  99.         $height = $dst_h = $src_h * $proportion;
  100.         $width  = $dst_w = $src_w * $proportion;
  101.     }

  102.     $src = $createfun($src_img);
  103.     $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h);
  104.     $white = imagecolorallocate($dst, 255, 255, 255);
  105.     imagefill($dst, 0, 0, $white);

  106.     if(function_exists('imagecopyresampled'))
  107.     {
  108.         imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
  109.     }
  110.     else
  111.     {
  112.         imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
  113.     }
  114.     $otfunc($dst, $dst_img);
  115.     imagedestroy($dst);
  116.     imagedestroy($src);
  117.     return true;
  118. }
复制代码
代码补充
  1. function fileext($file)
  2. {
  3.     return pathinfo($file, PATHINFO_EXTENSION);
  4. }
复制代码
Demo示例
  1. $src_img = "./ROSI_050_002.JPG";
  2. $dst_img = "./ROSI_050_002_thumb.jpg";
  3. $stat = img2thumb($src_img, $dst_img, $width = 200, $height = 300, $cut = 0, $proportion = 0);
  4. if($stat){
  5.     echo 'Resize Image Success!<br />';
  6.     echo '<img src="'.$dst_img.'" />';   
  7. }else{
  8.     echo 'Resize Image Fail!';  
  9. }
复制代码


回复

使用道具 举报

本版积分规则

QQ|Archiver|手机版|小黑屋|ylsunyuan技术论坛 ( 桂ICP备14005218号-1

GMT+8, 2024-4-18 11:33 , Processed in 0.061123 second(s), 39 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表