用户名:
密 码: 记住
您当前的位置:首页 > 网络编程 > php教程

php生成缩略图类,支持自定义高和宽,还支持按高和宽截图

时间:2015-01-23  来源:西部数据  作者:西部数据

php生成缩略图类,支持自定义高和宽,还支持按高和宽截图,实例代码如下:

  1. <?php   
  2. class resizeimage   
  3. {   
  4.     //图片类型   
  5.     var $type;   
  6.     //实际宽度   
  7.     var $width;   
  8.     //实际高度   
  9.     var $height;   
  10.     //改变后的宽度   
  11.     var $resize_width;   
  12.     //改变后的高度   
  13.     var $resize_height;   
  14.     //是否裁图   
  15.     var $cut;   
  16.     //源图象   
  17.     var $srcimg;   
  18.     //目标图象地址   
  19.     var $dstimg;   
  20.     //临时创建的图象   
  21.     var $im;   
  22.     function resizeimage($img$wid$hei,$c,$dstpath)   
  23.     {  //开源代码phpfensi.com 
  24.         $this->srcimg = $img;   
  25.         $this->resize_width = $wid;   
  26.         $this->resize_height = $hei;   
  27.         $this->cut = $c;   
  28.         //图片的类型  
  29.  
  30. $this->type = strtolower(substr(strrchr($this->srcimg,"."),1));   
  31.         //初始化图象   
  32.         $this->initi_img();   
  33.         //目标图象地址   
  34.         $this -> dst_img($dstpath);   
  35.         //--   
  36.         $this->width = imagesx($this->im);   
  37.         $this->height = imagesy($this->im);   
  38.         //生成图象   
  39.         $this->newimg();   
  40.         ImageDestroy ($this->im);   
  41.     }   
  42.     function newimg()   
  43.     {   
  44.         //改变后的图象的比例   
  45.         $resize_ratio = ($this->resize_width)/($this->resize_height);   
  46.         //实际图象的比例   
  47.         $ratio = ($this->width)/($this->height);   
  48.         if(($this->cut)=="1")   
  49.         //裁图   
  50.         {   
  51.             if($ratio>=$resize_ratio)   
  52.             //高度优先   
  53.             {   
  54.                 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);   
  55.                 imagecopyresampled($newimg$this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);   
  56.                 ImageJpeg ($newimg,$this->dstimg);   
  57.             }   
  58.             if($ratio<$resize_ratio)   
  59.             //宽度优先   
  60.             {   
  61.                 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);   
  62.                 imagecopyresampled($newimg$this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));   
  63.                 ImageJpeg ($newimg,$this->dstimg);   
  64.             }   
  65.         }   
  66.         else  
  67.         //不裁图   
  68.         {   
  69.             if($ratio>=$resize_ratio)   
  70.             {   
  71.                 $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);   
  72.                 imagecopyresampled($newimg$this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio$this->width, $this->height);   
  73.                 ImageJpeg ($newimg,$this->dstimg);   
  74.             }   
  75.             if($ratio<$resize_ratio)   
  76.             {   
  77.                 $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);   
  78.                 imagecopyresampled($newimg$this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio$this->resize_height, $this->width, $this->height);   
  79.                 ImageJpeg ($newimg,$this->dstimg);   
  80.             }   
  81.         }   
  82.     }   
  83.     //初始化图象   
  84.     function initi_img()   
  85.     {   
  86.         if($this->type=="jpg")   
  87.         {   
  88.             $this->im = imagecreatefromjpeg($this->srcimg);   
  89.         }   
  90.         if($this->type=="gif")   
  91.         {   
  92.             $this->im = imagecreatefromgif($this->srcimg);   
  93.         }   
  94.         if($this->type=="png")   
  95.         {   
  96.             $this->im = imagecreatefrompng($this->srcimg);   
  97.         }   
  98.     }   
  99.     //图象目标地址   
  100.     function dst_img($dstpath)   
  101.     {   
  102.         $full_length  = strlen($this->srcimg);   
  103.         $type_length  = strlen($this->type);   
  104.         $name_length  = $full_length-$type_length;  
  105.  
  106.         $name         = substr($this->srcimg,0,$name_length-1);   
  107.         $this->dstimg = $dstpath;  
  108.  
  109. //echo $this->dstimg;   
  110.     }   
  111. }  
  112.  
  113. $resizeimage = new resizeimage("11.jpg""200""150""1","17.jpg");  
来顶一下
返回首页
返回首页
推荐资讯
WiFi太不安全:7岁女孩11分钟内入侵公共网络 WiFi太不安全:7岁女孩11分钟内入侵近期刚刚发布研究说WiFi网络能获得人们手机里多少私人信息,
不服跑个分?人工智能也出现“刷分”乱象 不服跑个分?人工智能也出现“刷分2014年,人工智能领域突然爆发,成为了科研和科技创业的热门
相关文章
    无相关信息
栏目更新
栏目热门