全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
12
返回列表 发新帖
楼主: 360安全卫士

小弟初到贵地,求个PHP的缩略图类

[复制链接]
发表于 2012-9-14 17:20:53 | 显示全部楼层
来扣分
发表于 2012-9-14 17:21:51 | 显示全部楼层
给艹10分  不给艹 0分
发表于 2012-9-14 17:24:52 | 显示全部楼层
  1. <?php

  2. /*
  3. * Created on 20:31 2011-8-2
  4. * Author : LKK , http://lianq.net
  5. * 使用方法:
  6. *$resizeimage = new myThumbClass($file_name,120,90,$thumb,0,0);  //生成120x90大小
  7. *$resizeimage = new myThumbClass($file_name,1,2,$thumb,1,233);   //生成高,宽之中最大233
  8. *$resizeimage = new myThumbClass($file_name,660,1,$thumb,-1,0);  //生成宽度660
  9. *$resizeimage = new myThumbClass($file_name,1,660,$thumb,-1,0);  //生成高度660
  10. *注意:新高度或新宽度都不能为0
  11. */

  12. class myThumbClass{

  13.         public $sur_file;        //读取的原图片
  14.         public $des_file;        //生成目标图片
  15.         public $tem_file;        //临时图片
  16.         public $tag;                //缩略标签  0,默认,按固定的高宽生成  1,按比列或固定最大长度生成  -1,按某个宽度或某个高度缩小
  17.         public $resize_width;                //$tag为0时,目标文件宽
  18.         public $resize_height;                //$tag为0时,目标文件高
  19.         public $sca_max;        //$tag为1时,<0$sca_max<1时为缩小比例;$sca_max>1时为最大长度(高或宽之中的最大值)

  20.         public $type;                //图片类型
  21.         public $width;                //原图片宽
  22.         public $height;                //原图片高

  23.         //构造函数
  24.         public function __construct($surpic, $reswid, $reshei, $despic, $mark, $scamax){
  25.                 $this->sur_file = $surpic;
  26.                 $this->resize_width = $reswid;
  27.                 $this->resize_height = $reshei;
  28.                 $this->tag = $mark;
  29.                 $this->sca_max = $scamax;

  30.                 $this->type = strtolower(substr(strrchr($this->sur_file,"."),1));        //获取图片类型
  31.                 $this->init_img();        //初始化图片
  32.                 $this->des_file = $despic;        //目标图片地址
  33.                 $this->width = imagesx($this->tem_file);
  34.                 $this->height = imagesy($this->tem_file);
  35.                 $this->new_img();
  36.                 imagedestroy($this->tem_file);
  37.         }

  38.         //图片初始化函数
  39.         private function init_img(){
  40.                 if($this->type == 'jpeg'){
  41.                         $this->tem_file = imagecreatefromjpeg($this->sur_file);
  42.                 }elseif($this->type == 'jpg'){
  43.                         $this->tem_file = imagecreatefromjpeg($this->sur_file);
  44.                 }elseif($this->type == 'gif'){
  45.             $this->tem_file = imagecreatefromgif($this->sur_file);
  46.                 }elseif($this->type == 'png'){
  47.             $this->tem_file = imagecreatefrompng($this->sur_file);
  48.                 }elseif($this->type == 'bmp'){
  49.          //   $this->tem_file = imagecreatefrombmp($this->sur_file);        //bmp.php中包含
  50.                 }
  51.         }

  52.         //图片生成函数
  53.         private function new_img(){
  54.                 $ratio = ($this->width)/($this->height);        //原图比例
  55.                 $resize_ratio = ($this->resize_width)/($this->resize_height);        //缩略后比例
  56.                 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);//生成新图片

  57.                 if($this->tag == 0){        //按固定高宽截取缩略图
  58.                         $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);//生成新图片
  59.                         if($ratio>=$resize_ratio){//即等比例下,缩略图的高比原图长,因此高不变
  60.                                 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
  61.                         }elseif($ratio<$resize_ratio){//即等比例下,缩略图的宽比原图长,因此宽不变
  62.                                 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width,$this->resize_height, $this->width, (($this->width)/$resize_ratio));
  63.                         }
  64.                 }elseif($this->tag == 1){        //按固定比例或最大长度缩小
  65.                         if($this->sca_max < 1){        //按比例缩小
  66.                                 $newimg = imagecreatetruecolor((($this->width)*($this->sca_max)),(($this->height)*($this->sca_max)));//生成新图片
  67.                                 imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, (($this->width)*($this->sca_max)), (($this->height)*($this->sca_max)), $this->width, $this->height);

  68.                         }elseif($this->sca_max > 1){        //按某个最大长度缩小
  69.                                 if($ratio>=1){        //宽比高长
  70.                                         $newimg = imagecreatetruecolor($this->sca_max,($this->sca_max/$ratio));//生成新图片
  71.                                         imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->sca_max,($this->sca_max/$ratio), $this->width, $this->height);
  72.                                 }else{
  73.                                         $newimg = imagecreatetruecolor(($this->sca_max*$ratio),$this->sca_max);//生成新图片
  74.                                         imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, ($this->sca_max*$ratio),$this->sca_max, $this->width, $this->height);
  75.                                 }
  76.                         }
  77.                 }elseif($this->tag == -1){        //按某个宽度或某个高度缩小
  78.                   if($resize_ratio>=1){//新高小于新宽,则图片按新宽度缩小
  79.                     $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width/$ratio));//生成新图片
  80.                         imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width,($this->resize_width/$ratio), $this->width, $this->height);
  81.                   }elseif($resize_ratio<1){//新宽小于新高,则图片按新高度缩小
  82.                     $newimg = imagecreatetruecolor(($this->resize_height*$ratio),$this->resize_height);//生成新图片
  83.                                         imagecopyresampled($newimg, $this->tem_file, 0, 0, 0, 0, ($this->resize_height*$ratio),$this->resize_height, $this->width, $this->height);
  84.                   }
  85.                 }

  86.                 //输出新图片
  87.                 if($this->type == 'jpeg' || $this->type == 'jpg'){
  88.                         imagejpeg($newimg,$this->des_file);
  89.                 }elseif($this->type == 'gif'){
  90.                         imagegif($newimg,$this->des_file);
  91.                 }elseif($this->type == 'png'){
  92.                         imagepng($newimg,$this->des_file);
  93.                 }elseif($this->type == 'bmp'){
  94.                         imagebmp($newimg,$this->des_file);//bmp.php中包含
  95.                 }

  96.         }#function new_img() end

  97. }#end class

  98. ?>
复制代码
这个是我改blog的时候 在网上找的   基本功能有了
发表于 2012-9-14 19:48:33 | 显示全部楼层
scand 发表于 2012-9-14 17:24
这个是我改blog的时候 在网上找的   基本功能有了

大牛啊
发表于 2012-9-14 19:53:05 | 显示全部楼层
采集图片 切掉水印...
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-4-28 18:38 , Processed in 0.064313 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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