Home php教程 PHP源码 php缩略图类

php缩略图类

May 25, 2016 pm 05:08 PM

php代码

<?php
  define(&#39;ROOT&#39;,&#39;D:/Program Files/www/test/&#39;);
  class Gd_Thumbnail_Tool{
     protected static  $image_w;          //图像的宽
     protected static  $image_h;           //图像的高
     protected static  $image_ext;        //图像的后缀

     //缩略图方法(参1要处理的图,参2处理后的宽,参3处理后的高)
     public static function  mk_Thumb($image,$width=160,$height=160){
        //对中文进行转码处理
        $image=iconv(&#39;UTF-8&#39;,&#39;GB2312&#39;,$image);
        //获取图片信息
        self::image_Info($image);
        //验证是否获取到信息
        if( empty(self::$image_w) || empty(self::$image_h) || empty(self::$image_ext) ){return false;}
        //判断图片的大小是否需要进行等比例缩略
        if(self::$image_w<=$width && self::$image_h<=$height){$yes=false;}
        $yes=true;
        //按比例缩略
        if($yes){   
            if( (self::$image_w$height) ){
                //缩略后宽
               $n=$height*(self::$image_w/self::$image_h);
               $small_w=round($n);
                //缩略后高
               $small_h=$height;
             }
             
             if( (self::$image_w>=self::$image_h) && (self::$image_w>$width) ){
                 //缩略后宽
                 $small_w=$width;
                 //缩略后高
                 $n=$width*(self::$image_h/self::$image_w);
                 $small_h=round($n);
             }
        }
       //以原图做画布
       $a=&#39;imagecreatefrom&#39;.self::$image_ext;
       $original=$a($image);
       //创建小画布
       $litter=imagecreatetruecolor($width,$height);
       //把大图缩略放入画布
       $x=($width-$small_w)/2;
       $y=($height-$small_h)/2;
       if(!$rs=imagecopyresampled($litter,$original,$x,$y,0,0,$small_w,$small_h,self::$image_w,self::$image_h)){return false;}
       //保存路径
       $path=self::image_Dir().self::rand_Name().&#39;.&#39;.self::$image_ext;
       //保存图片
       $keep=&#39;image&#39;.self::$image_ext;
       $keep($litter,$path);
       //关闭图片
       imagedestroy($original);
       imagedestroy($litter);
       //返回路径
       return $path=strtr($path,array(ROOT=>&#39;&#39;));
     }
    
     //获取图片信息方法
     protected static function image_Info($image){
       if($info=getimagesize($image)){
            //图像的宽
            self::$image_w=$info[0];                    
            //图像的高
            self::$image_h=$info[1];
            //图像的后缀
            $ext=image_type_to_extension($info[2]);
            $ext=ltrim($ext,&#39;.&#39;);
            self::$image_ext=$ext;
       }
     }
    
     //生成路径
     protected static function image_Dir(){
       $dir=ROOT.&#39;Data/images/&#39;.date(&#39;Y/m/d/&#39;,time());
       if(!is_dir($dir)){mkdir($dir,0777,true);}
       return $dir;
     }
     //随机文件名
     protected static function rand_Name(){
      $name=str_shuffle(&#39;1234567890qwertyuiopasdfghjklmnbvcxz&#39;);
      $name=substr($name,0,7);
      return $name=$name.&#39;_smal&#39;;
     }
  }
  
  //测试
  //echo Gd_Thumbnail_Tool::mk_Thumb(&#39;啊.jpg&#39;);
  //返回效果:Data/images/2013/02/01/ei3ufpr_smal.jpeg

?>
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1675
14
PHP Tutorial
1278
29
C# Tutorial
1257
24