zoukankan      html  css  js  c++  java
  • 缩略图的生成

     1 //生成缩略图
     2     public function wgetimg($topath, $w, $h, $url, $picname, $type) {
     3         $sRealPath = realpath('./');
     4         $sSelfPath = $_SERVER['PHP_SELF'];
     5         $sSelfPath = substr($sSelfPath, 0, strrpos($sSelfPath, '/'));
     6         $sSelfPath = substr($sRealPath, 0, strlen($sRealPath) - strlen($sSelfPath));
     7         
     8         $topath = $sSelfPath.$topath;
     9         include_once('./apps/index/Lib/Action/imgAction.class.php');
    10         $type = strtolower($type);
    11         if (!file_exists($topath . $picname . '.' . $type)) {
    12             //exec("wget -c -t 100 -T 60 -O ".$topath.$picname.'.'.$type." ".$url);
    13             exec("wget -c -t 3 -T 60 -O " . $topath . $picname . '.' . $type . " " . $url);
    14         }
    15         
    16         if (!file_exists($topath . '(' . $w . 'x' . $h . ')_' . $picname . '.' . $type) && file_exists($topath . $picname . '.' . $type)) {
    17             /* 生成图片 */
    18             $arrayName = array(
    19                     'path' => $topath . $picname . '.' . $type,
    20                     'name' => '(' . $w . 'x' . $h . ')_' . $picname,
    21                     'type' => $type,
    22                     'topath' => $topath,
    23                     'scale' => true,
    24                     'tow' => $w,
    25                     'toh' => $h,
    26                     'poi' => 1,
    27                     'strict' => false
    28             );
    29             $a = new imgAction($arrayName);
    30         }
    31         //return $arrayName['name'].$arrayName['type'];
    32     }

    调用:

    第一个参数:缩略图要存放的目录,后面两个参数是缩略图的大小。$img1:是要被缩略的原图地址;$picname是缩略图的名称,不含后缀。$type是图片的类型即后缀.


    $this->wgetimg('/data/Uploads/ad/', 318, 395, $img1, $picname, $type);

    imgAction.class.php:

      1 <?php
      2 /*
      3 图片处理类
      4 输入参数    $imgInfo array
      5                 ['raww'] int 原始图片宽度
      6                 ['rawh'] int 原始图片高度
      7                 ['rawtype'] string 图片类型
      8                 ['rawext'] 图片后缀
      9                 ['rsize'] 原图大小
     10                 ['dirname'] 图片与后缀的分割符
     11                 ['tmpimg'] 初始化后的图片
     12 
     13                 *['scale'] = true/flase 
     14                 true 设置固定尺寸,不够大放大,太大缩小
     15                 flase 按比例缩放
     16                 true 按框高等比缩放
     17                 ['tow']  int 缩放的宽 px
     18                 ['toh']  int 缩放的高 px
     19 
     20                 ['poi'] int
     21                 0 坐标0 截取保留上部分
     22                 1 截取保留中间部分
     23                 2 截取保留下面部分
     24 
     25                 ['name'] string 空使用原来的名字
     26                 ['type'] = string 空保留原来的图片类型
     27 
     28                 *['path'] string 图片来路
     29                 *['topath'] sring 保存的路径
     30                 ['strict'] 是否严格较验图片类型 , 类型与后缀需相同 默认为 true
     31 
     32                 ['show'] = true/false 
     33                 true 直接输出图片 
     34 
     35                 ['quality'] int 60-100 图片质量
     36 
     37                 ['watermark'] array
     38                     ---['type'] text 文字水印 img 图片水印
     39                     ---['path'] string 来路
     40                     ---['forcing'] = true/false
     41                        true 强制水印,不管图片高宽
     42                     ---['poi']  int 水印位置,从左到右
     43                               0 1 2
     44                               3 4 5
     45                               6 7 8
     46                               9    9为随机
     47 
     48                 ['goimg'] array 内容生成图片
     49                     ---['content'] string 内容
     50                     ---['font'] string 字体
     51                     ---['size'] int 字号
     52                     ---['bold'] = true/false 
     53                     true 加粗
     54                 ['msg'] = true/flase
     55                 true 只返回成功失败信息
     56 
     57 输出参数     imgInfo array 
     58                   'path' => string 'teJkZsyiFzngfNDeKU.jpg' (length=22)
     59                   'name' => string 'aa' (length=2)
     60                   'type' => string 'jpg' (length=3)
     61                   'topath' => string 'D:svn	mp	est/' (length=16)
     62                   'fiexd' => boolean true
     63                   'tow' => int 160
     64                   'toh' => int 40
     65                   'poi' => int 1
     66                   'raww' => int 533
     67                   'rawh' => int 800
     68                   'rawtype' => string 'jpg' (length=3)
     69                   'rawext' => string 'jpg' (length=3)
     70                   'dirname' => string '.' (length=1)
     71                   'tmpimg' => resource(4, gd)
     72                   'time' => int 1358504732
     73                   'w' => float 160
     74                   'h' => float 240.15009380863
     75                   'msg' => int 1
     76                   
     77 PS: 按比例缩小只需写宽或高,缩放时按大的尺寸缩放 如 宽 0 高 10 则按高度10缩放
     78 
     79 */
     80 
     81 class imgAction{
     82     public function __construct($imgInfo){
     83         !is_array($imgInfo)? exit('not array'):'';
     84         
     85         $this->imgInfo = $imgInfo;
     86         if (!$this->exists($this->imgInfo['path'])){
     87             return false;
     88         }
     89 
     90         $imagesize = $this->getimagesize($this->imgInfo['path'],$this->imgInfo['strict']);
     91         if (!$imagesize){
     92             return false;
     93         }
     94         $this->imgInfo['raww'] = $imagesize['0'];
     95         $this->imgInfo['rawh'] = $imagesize['1'];
     96         $this->imgInfo['rawtype'] = $imagesize['type'];
     97         $this->imgInfo['rawext'] = $imagesize['extension'];
     98         $this->imgInfo['name'] = isset($imgInfo['name']) ? $imgInfo['name'] : $imagesize['filename']; 
     99         $this->imgInfo['type'] = isset($imgInfo['type']) ? $imgInfo['type'] : $imagesize['type']; 
    100         $this->imgInfo['dirname'] = $imagesize['dirname'];
    101         $this->imgInfo['tmpimg'] = $imagesize['tmpimg'];
    102         $this->pyresizepic();
    103     }
    104     /*
    105     图片处理缩略
    106     */
    107     private function pyresizepic(){
    108         $ratio = $this->ratio($this->imgInfo);
    109         $this->imgInfo['w'] = $ratio['w'];
    110         $this->imgInfo['h'] = $ratio['h'];
    111         if ($this->imgInfo['scale']){
    112             $this->fiexd($ratio);
    113         }else{
    114             $this->scale($ratio);
    115         }
    116         
    117     }
    118     /*固定大小的缩放*/
    119     private function fiexd($ratio){
    120         $newimg = imagecreatetruecolor($this->imgInfo['tow'],$this->imgInfo['toh']);
    121         imagecopyresampled($newimg,$this->imgInfo['tmpimg'], 0, 0, $ratio['x'], $ratio['y'], $ratio['w'],$ratio['h'], $this->imgInfo['raww'], $this->imgInfo['rawh']);
    122         $this->printpic($newimg);
    123     }
    124     /*按比例的缩放*/
    125     private function scale($ratio){
    126         $newimg = imagecreatetruecolor($ratio['w'],$ratio['h']);
    127         imagecopyresampled($newimg,$this->imgInfo['tmpimg'], 0, 0, 0, 0, $ratio['w'],$ratio['h'], $this->imgInfo['raww'], $this->imgInfo['rawh']);
    128         $this->printpic($newimg);
    129     }
    130     /*将bmp改成jpg*/
    131     private function bmp($im,$path,$w,$h){
    132         $newimg = imagecreatetruecolor($w,$h);
    133         imagecopyresampled($newimg,$im, 0, 0, 0, 0, $w,$h, $w,$h);
    134         imagejpeg($newimg,$path,100);
    135     }
    136     
    137     /*生成图片*/
    138     private function printpic($newimg){
    139        if (!$this->exists($this->imgInfo['topath'])){
    140             if (!mkdir($this->imgInfo['topath'],777)){
    141                     $this->text($this->imgInfo['topath']);
    142             }
    143        }
    144         ImageInterlace($newimg,1);
    145         switch ($this->imgInfo['type']) {
    146                 case 'gif':
    147                     imagegif($newimg,$this->imgInfo['topath'].$this->imgInfo['name'].'.'.$this->imgInfo['type']);
    148                     break;
    149                 case 'jpg':
    150                 case 'jpeg':
    151                     imagejpeg($newimg,$this->imgInfo['topath'].$this->imgInfo['name'].'.'.$this->imgInfo['type']);
    152                     break;
    153                 case 'png':
    154                     imagepng($newimg,$this->imgInfo['topath'].$this->imgInfo['name'].'.'.$this->imgInfo['type']);
    155                     break;
    156                 case 'bmp':
    157                     imagejpeg($newimg,$this->imgInfo['topath'].$this->imgInfo['name'].'.'.$this->imgInfo['type']);
    158                     break;
    159                 default:
    160                     $this->text($this->imgInfo['topath']);
    161                     break;
    162         }
    163         $this->imgInfo['time'] = time();
    164         $this->imgInfo['msg'] = 1 ;
    165         //return $this->imgInfo;
    166         //$this->text($this->imgInfo);
    167     }
    168 
    169     /*计算缩放比例*/
    170     private function ratio(){
    171          $ratio = ($this->imgInfo['raww'] >= $this->imgInfo['rawh']) ? $this->imgInfo['toh']/$this->imgInfo['rawh'] : $this->imgInfo['tow']/$this->imgInfo['raww'];
    172          
    173          $p = 0.3132530120481928; //坐标偏移,截取中间时,某些尺寸会出错,需设置便宜值
    174          
    175             $scrWH['w'] = $this->imgInfo['raww']*$ratio;
    176             $scrWH['h'] = $this->imgInfo['rawh']*$ratio;
    177             
    178          if ($scrWH['w'] <  $this->imgInfo['tow'] || $scrWH['h'] <  $this->imgInfo['toh']){
    179           $ratio = ($this->imgInfo['raww'] >= $this->imgInfo['rawh']) ? $this->imgInfo['tow']/$this->imgInfo['raww'] : $this->imgInfo['toh']/$this->imgInfo['rawh'];
    180             if ($scrWH['w'] <  $this->imgInfo['tow'] ){
    181                 $scrWH['w'] = $this->imgInfo['tow'];
    182                 $scrWH['h'] = $this->imgInfo['rawh']*$ratio;
    183             }
    184             if ($scrWH['w'] <  $this->imgInfo['tow'] ){
    185                 $scrWH['w'] = $this->imgInfo['raww']*$ratio;
    186                 $scrWH['h'] = $this->imgInfo['rawh'];
    187             }
    188          }
    189          
    190          
    191          if ($this->imgInfo['poi'] == 1){
    192 
    193          $scrWH['x'] = ($scrWH['w'] > $this->imgInfo['tow']) ? ($scrWH['w'] - $this->imgInfo['tow'])/2 : 0;
    194          $scrWH['y'] = ($scrWH['h'] > $this->imgInfo['toh']) ? ($scrWH['h'] - $this->imgInfo['toh'])/2 : 0;
    195          $scrWH['x'] = $scrWH['x']*$p ;
    196          $scrWH['y'] = $scrWH['y']*$p ;
    197         
    198          }elseif ($this->imgInfo['poi'] == 2){
    199              $scrWH['x']=0;
    200              $scrWH['y'] = ($scrWH['h'] > $this->imgInfo['toh']) ?$scrWH['h']-$this->imgInfo['toh'] : 0;
    201          }else{
    202              $scrWH['x']=0;
    203              $scrWH['y']=0;
    204          }
    205          return $scrWH;
    206     }
    207     /*
    208         1 为 GIF 格式、 2 为 JPEG/JPG 格式、3 为 PNG
    209     */
    210     private function getimagesize($string,$strict){
    211         $_getimagesize = getimagesize($string);
    212         $inarray = array('jpg','png','gif','bmp');
    213         switch ($_getimagesize[2]) {
    214             case 1:
    215                 $type = 'gif';
    216                 break;
    217             case 2:
    218                 $type = 'jpg';
    219                 break;
    220             case 3:
    221                 $type = 'png';
    222                 break;
    223             case 6:
    224                 $type = 'bmp';
    225                 break;
    226             default:
    227                 $this->text($_getimagesize[2]);
    228                 break;
    229         }
    230         switch ($_getimagesize['mime']) {
    231             case 'image/png':
    232                 $mine = 'png';
    233                 break;
    234             case 'image/jpeg':
    235                 $mine = 'jpg';
    236                 break;
    237             case 'image/gif':
    238                 $mine = 'gif';
    239                 break;
    240             case 'image/x-ms-bmp':
    241                 $mine = 'bmp';
    242                 break;
    243             default:
    244                 $this->text($_getimagesize['mime']);
    245                 break;
    246         }
    247         
    248 
    249         $pathinfo = $this->pathinfo($string);
    250         
    251         
    252 
    253         if ($mine == 'bmp'){    
    254             rename (dirname(__FILE__).'/'.$pathinfo['dirname'].'/'.$pathinfo['basename'],dirname(__FILE__).'/'.$pathinfo['dirname'].'/'.$pathinfo['filename'].'.'.$mine) ;
    255             $string = $pathinfo['dirname'].'/'.$pathinfo['filename'].'.'.$mine;
    256             $pathinfo = $this->pathinfo($string);
    257         }
    258 
    259         if ($strict){
    260             /*严格校验*/
    261             $type = $type != $mine ? false :$type;
    262             $type = $type != $pathinfo['extension'] ? false :$type;
    263             $type = $mine != $pathinfo['extension'] ? false :$type;
    264             
    265             if (!$type){
    266                 return false;
    267             }
    268             
    269         }else{
    270             /*宽松较验*/
    271             if (!in_array($type,$inarray) || !in_array($mine,$inarray) || !in_array($pathinfo['extension'],$inarray)){
    272                 return false;
    273             }
    274         }
    275 
    276         $_getimagesize['type'] = $pathinfo['extension'];
    277         $_getimagesize['dirname'] = $pathinfo['dirname'];
    278         $_getimagesize['filename'] = $pathinfo['filename'];
    279         $_getimagesize['extension'] = $pathinfo['extension'];
    280         
    281         switch ($type) {
    282             case 'gif':
    283                 $_getimagesize['tmpimg'] = imagecreatefromgif($string);
    284                 break;
    285             case 'jpg':
    286                 $_getimagesize['tmpimg'] = imagecreatefromjpeg($string);
    287                 break;
    288             case 'png':
    289                 $_getimagesize['tmpimg'] = imagecreatefrompng($string);
    290                 break;
    291             case 'bmp':
    292                 $_getimagesize['tmpimg'] = $this->imagecreatefrombmp($string);
    293                 break;
    294             default:
    295                 $this->text($type);
    296                 break;
    297         }
    298         if ($type == 'bmp'){
    299              imagejpeg($_getimagesize['tmpimg'], $_getimagesize['dirname'].'/'.$_getimagesize['filename'].'.jpg');
    300              unlink($_getimagesize['dirname'].'/'.$_getimagesize['filename'].'.'.$_getimagesize['extension']);
    301              $_getimagesize['extension'] = 'jpg';
    302              $_getimagesize['tmpimg'] = imagecreatefromjpeg($_getimagesize['dirname'].'/'.$_getimagesize['filename'].'.'.$_getimagesize['extension']);
    303         }    
    304         return $_getimagesize;
    305     }
    306     /*
    307     文件名字信息
    308     */
    309     private function pathinfo($string)
    310     {
    311                 $extend = pathinfo($string);
    312                 $extend['extension'] = strtolower($extend['extension']);
    313                 if ($extend['extension'] == 'jpeg')  $extend['extension'] = 'jpg';
    314                 return $extend;
    315     }
    316     /*
    317     文件存在
    318     */
    319     private function exists($string){
    320         if (!file_exists($string)){
    321             return false;
    322         }
    323         return true;
    324     }
    325     /*test 用,可以保存未知文件的LOG信息*/
    326     private function text($str){
    327     return false;
    328         //var_dump($str);
    329         //exit;
    330     }
    331     private function imagecreatefrombmp($string){
    332         $tmp_name = tempnam("tmp", "GD");
    333          if($this->ConvertBMP2GD($string, $tmp_name)) {
    334           $img = imagecreatefromgd($tmp_name);
    335           unlink($tmp_name);
    336           return $img;
    337          }
    338          return false;
    339     }
    340     /* 处理PNG 图片*/
    341      private function ConvertBMP2GD($src, $dest = false) {
    342  if(!($src_f = fopen($src, "rb"))) {
    343   return false;
    344  }
    345  if(!($dest_f = fopen($dest, "wb"))) {
    346   return false;
    347  }
    348  $header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f, 14));
    349  $info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", fread($src_f, 40));
    350  extract($info);
    351  extract($header);
    352  if($type != 0x4D42) { // signature "BM"
    353   return false;
    354  }
    355  $palette_size = $offset - 54;
    356  $ncolor = $palette_size / 4;
    357  $gd_header = "";
    358  // true-color vs. palette
    359  $gd_header .= ($palette_size == 0) ? "xFFxFE" : "xFFxFF";
    360  $gd_header .= pack("n2", $width, $height);
    361  $gd_header .= ($palette_size == 0) ? "x01" : "x00";
    362  if($palette_size) {
    363   $gd_header .= pack("n", $ncolor);
    364  }
    365  // no transparency
    366  $gd_header .= "xFFxFFxFFxFF";
    367  fwrite($dest_f, $gd_header);
    368  if($palette_size) {
    369   $palette = fread($src_f, $palette_size);
    370   $gd_palette = "";
    371   $j = 0;
    372   while($j < $palette_size) {
    373    $b = $palette{$j++};
    374    $g = $palette{$j++};
    375    $r = $palette{$j++};
    376    $a = $palette{$j++};
    377    $gd_palette .= "$r$g$b$a";
    378   }
    379   $gd_palette .= str_repeat("x00x00x00x00", 256 - $ncolor);
    380   fwrite($dest_f, $gd_palette);
    381  }
    382  $scan_line_size = (($bits * $width) + 7) >> 3;
    383  $scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03) : 0;
    384  for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
    385  // BMP stores scan lines starting from bottom
    386   fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l));
    387   $scan_line = fread($src_f, $scan_line_size);
    388   if($bits == 24) {
    389    $gd_scan_line = "";
    390    $j = 0;
    391    while($j < $scan_line_size) {
    392     $b = $scan_line{$j++};
    393     $g = $scan_line{$j++};
    394     $r = $scan_line{$j++};
    395     $gd_scan_line .= "x00$r$g$b";
    396    }
    397   }
    398   else if($bits == 8) {
    399    $gd_scan_line = $scan_line;
    400   }
    401   else if($bits == 4) {
    402    $gd_scan_line = "";
    403    $j = 0;
    404    while($j < $scan_line_size) {
    405     $byte = ord($scan_line{$j++});
    406     $p1 = chr($byte >> 4);
    407     $p2 = chr($byte & 0x0F);
    408     $gd_scan_line .= "$p1$p2";
    409    }
    410    $gd_scan_line = substr($gd_scan_line, 0, $width);
    411   }
    412   else if($bits == 1) {
    413    $gd_scan_line = "";
    414    $j = 0;
    415    while($j < $scan_line_size) {
    416     $byte = ord($scan_line{$j++});
    417     $p1 = chr((int) (($byte & 0x80) != 0));
    418     $p2 = chr((int) (($byte & 0x40) != 0));
    419     $p3 = chr((int) (($byte & 0x20) != 0));
    420     $p4 = chr((int) (($byte & 0x10) != 0));
    421     $p5 = chr((int) (($byte & 0x08) != 0));
    422     $p6 = chr((int) (($byte & 0x04) != 0));
    423     $p7 = chr((int) (($byte & 0x02) != 0));
    424     $p8 = chr((int) (($byte & 0x01) != 0));
    425     $gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8";
    426    }
    427    $gd_scan_line = substr($gd_scan_line, 0, $width);
    428   }
    429   fwrite($dest_f, $gd_scan_line);
    430  }
    431  fclose($src_f);
    432  fclose($dest_f);
    433  return true;
    434 }
    435 }
    436 /*
    437 $arrayName = array(
    438     'path'     => '0a9eaff4-b692-49df-95ba-e32c108be4d7.jpg',
    439     'name'     => 'aa',
    440     'type' => 'jpg',
    441     'topath' =>  dirname(__FILE__).'/',
    442     'scale' => true,
    443     'tow'    => 140,
    444     'toh'    => 140,
    445     'poi'   => 1,
    446     'strict' => false
    447      );
    448 $a = new imgAction($arrayName);
    449 */
    450  ?>
  • 相关阅读:
    系统管理员必须掌握的20个Linux监控工具
    JavaWeb基础—MVC与三层架构
    JavaWeb基础—JavaBean
    JavaWeb基础—JSP
    myeclipse(eclipse)IDE配置
    JavaWeb基础—会话管理之Session
    JavaWeb基础—会话管理之Cookie
    JavaWeb基础—项目名的写法
    JavaWeb基础—HttpServletRequest
    JavaWeb基础—VerifyCode源码
  • 原文地址:https://www.cnblogs.com/xingmeng/p/3412097.html
Copyright © 2011-2022 走看看