国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - php實現的支持imagemagick及gd庫兩種處理的縮略圖生成類

php實現的支持imagemagick及gd庫兩種處理的縮略圖生成類

2020-07-29 15:41PHP教程網 PHP教程

這篇文章主要介紹了php實現的支持imagemagick及gd庫兩種處理的縮略圖生成類,包含了用法的詳細描述,非常實用,需要的朋友可以參考下

本文實例講述了php實現的支持imagemagickgd庫兩種處理的縮略圖生成類及其用法實例,非常具有實用價值。分享給大家供大家參考。具體如下:

一、功能:

1.按比例縮小/放大
2.填充背景色
3.按區域裁剪
4.添加水印,包括水印的位置,透明度等

使用imagemagick/GD庫實現,imagemagick地址:www.imagemagick.org
需要安裝imagemagick,安裝方法如下:http://www.jfrwli.cn/article/89797.html

二、實現方法:

PicThumb.class.php類文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
<?php
/** 縮略圖生成類,支持imagemagick及gd庫兩種處理
*  Date:  2013-07-15
*  Author: fdipzone
*  Ver:  1.2
*
*  Func:
*  public set_config: 設置參數
*  public create_thumb: 生成縮略圖
*  private fit: 縮略圖片
*  private crop: 裁剪圖片
*  private gd_fit: GD庫縮略圖片
*  private gd_crop: GD庫裁剪圖片
*  private get_size: 獲取要轉換的size
*  private get_crop_offset: 獲取裁圖的偏移量
*  private add_watermark: 添加水印
*  private check_handler: 判斷處理程序是否已安裝
*  private create_dirs: 創建目錄
*  private exists: 判斷參數是否存在
*  private to_log: 記錄log
*  private hex2rgb: hex顏色轉rgb顏色
*  private get_file_ext: 獲取圖片類型
*
*  ver:  1.1 增加GD庫處理
*  ver:  1.2 增加width,height錯誤參數處理
*        增加當圖片colorspace不為RGB時作轉RGB處理
*        修正使用crop保存為gif時出現透明無效區域問題,使用+repage參數,刪除透明無效區域即可
*
*  tips:建議使用imagemagick
*    GD庫不支持透明度水印,如果必須使用透明水印,請將水印圖片做成有透明度。
*    GD庫輸出gif如加透明水印,會有問題。
*/
 
class PicThumb{ // class start
 
  private $_log = null;      // log file
  private $_handler = null;    // 進行圖片處理的程序,imagemagick/gd庫
  private $_type = 'fit';     // fit or crop
  private $_source = null;     // 原圖路徑
  private $_dest = null;      // 縮略圖路徑
  private $_watermark = null;   // 水印圖片
  private $_opacity = 75;     // 水印圖片透明度,gd庫不支持
  private $_gravity = 'SouthEast'; // 水印擺放位置 NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast
  private $_geometry = '+10+10'// 水印定位,gd庫不支持
  private $_croppos = 'TL';    // 截圖的位置 TL TM TR ML MM MR BL BM BR
  private $_bgcolor = null;    // 填充的背景色
  private $_quality = 90;     // 生成的圖片質量
  private $_width = null;     // 指定區域寬度
  private $_height = null;     // 指定區域高度
 
  // 初始化
  public function __construct($logfile=''){
    if($logfile!=''){
      $this->_log = $logfile;
    }
  }
 
  // 設置參數
  public function set_config($param=array()){
    $this->_handler = $this->exists($param, 'handler')? strtolower($param['handler']) : null;
    $this->_type = $this->exists($param, 'type')? strtolower($param['type']) : 'fit';
    $this->_watermark = $this->exists($param, 'watermark')? $param['watermark'] : null;
    $this->_opacity = $this->exists($param, 'opacity')? $param['opacity'] : 75;
    $this->_gravity = $this->exists($param, 'gravity')? $param['gravity'] : 'SouthEast';
    $this->_geometry = $this->exists($param, 'geometry')? $param['geometry'] : '+10+10';
    $this->_croppos = $this->exists($param, 'croppos')? $param['croppos'] : 'TL';
    $this->_bgcolor = $this->exists($param, 'bgcolor')? $param['bgcolor'] : null;
    $this->_quality = $this->exists($param, 'quality')? $param['quality'] : 90;
    $this->_width = $this->exists($param, 'width')? $param['width'] : null;
    $this->_height = $this->exists($param, 'height')? $param['height'] : null;
  }
 
  /** 創建縮略圖
  * @param String $source 原圖
  * @param String $dest  目標圖
  * @return boolean
  */
  public function create_thumb($source, $dest){
    // 檢查使用的handler是否已安裝
    if(!$this->check_handler()){
      $this->to_log('handler not installed');
      return false;
    }
    // 判斷區域寬高是否正確
    if(!is_numeric($this->_width) || !is_numeric($this->_height) || $this->_width<=0 || $this->_height<=0){
      $this->to_log('width or height invalid');
      return false;
    }
 
    // 判斷源文件是否存在
    if(!file_exists($source)){
      $this->to_log($source.' not exists');
      return false;
    }
 
    // 創建目標文件路徑
    if(!$this->create_dirs($dest)){
      $this->to_log(dirname($dest).' create fail');
      return false;
    }
 
    $this->_source = $source// 源文件
    $this->_dest = $dest;    // 目標文件
 
    // 處理圖片
    switch($this->_type){
      case 'fit':
        if($this->_handler=='imagemagick'){
          return $this->fit();
        }else{
          return $this->gd_fit();
        }
        break;
 
      case 'crop':
        if($this->_handler=='imagemagick'){
          return $this->crop();
        }else{
          return $this->gd_crop();
        }
        break;
 
      default:
        $this->to_log($this->_type.' not fit and crop');
        return false;
    }
  }
 
  /** 按比例壓縮或拉伸圖片
  * @return boolean
  */
  private function fit(){
 
    // 判斷是否填充背景
    $bgcolor = ($this->_bgcolor!=null)? 
    sprintf(" -background '%s' -gravity center -extent '%sx%s' ", $this->_bgcolor, $this->_width, $this->_height) : "";
 
    // 判斷是否要轉為RGB
    $source_info = getimagesize($this->_source);
    $colorspace = (!isset($source_info['channels']) || $source_info['channels']!=3)? ' -colorspace RGB ' : '';
 
    // 命令行
    $cmd = sprintf("convert -resize '%sx%s' '%s' %s -quality %s %s '%s'", $this->_width, $this->_height, $this->_source, $bgcolor, $this->_quality, $colorspace, $this->_dest);
 
    // 記錄執行的命令
    $this->to_log($cmd);
 
    // 執行命令
    exec($cmd);
 
    // 添加水印
    $this->add_watermark($this->_dest);
 
    return is_file($this->_dest)? true : false;
  }
 
  /** 裁剪圖片
  * @return boolean
  */
  private function crop(){
    // 獲取生成的圖片尺寸
    list($pic_w, $pic_h) = $this->get_size();
 
    // 獲取截圖的偏移量
    list($offset_w, $offset_h) = $this->get_crop_offset($pic_w, $pic_h);
 
    // 判斷是否要轉為RGB
    $source_info = getimagesize($this->_source);
    $colorspace = (!isset($source_info['channels']) || $source_info['channels']!=3)? ' -colorspace RGB ' : '';
 
    // 命令行
    $cmd = sprintf("convert -resize '%sx%s' '%s' -quality %s %s -crop %sx%s+%s+%s +repage '%s'", $pic_w, $pic_h, $this->_source, $this->_quality, $colorspace, $this->_width, $this->_height, $offset_w, $offset_h, $this->_dest);
 
    // 記錄執行的命令
    $this->to_log($cmd);
 
    // 執行命令
    exec($cmd);
 
    // 添加水印
    $this->add_watermark($this->_dest);
 
    return is_file($this->_dest)? true : false;
  }
 
  /** GD庫按比例壓縮或拉伸圖片
  * @return boolean
  */
  private function gd_fit(){
    // 獲取生成的圖片尺寸
    list($pic_w, $pic_h) = $this->get_size();
 
    list($owidth, $oheight, $otype) = getimagesize($this->_source);
 
    switch($otype){
      case 1: $source_img = imagecreatefromgif($this->_source); break;
      case 2: $source_img = imagecreatefromjpeg($this->_source); break;
      case 3: $source_img = imagecreatefrompng($this->_source); break;
      default: return false;
    }
 
    // 按比例縮略/拉伸圖片
    $new_img = imagecreatetruecolor($pic_w, $pic_h);
    imagecopyresampled($new_img, $source_img, 0, 0, 0, 0, $pic_w, $pic_h, $owidth, $oheight);
 
    // 判斷是否填充背景
    if($this->_bgcolor!=null){
      $bg_img = imagecreatetruecolor($this->_width, $this->_height);
      $rgb = $this->hex2rgb($this->_bgcolor);
      $bgcolor =imagecolorallocate($bg_img, $rgb['r'], $rgb['g'], $rgb['b']);
      imagefill($bg_img, 0, 0, $bgcolor);
      imagecopy($bg_img, $new_img, (int)(($this->_width-$pic_w)/2), (int)(($this->_height-$pic_h)/2), 0, 0, $pic_w, $pic_h);
      $new_img = $bg_img;
    }
 
    // 獲取目標圖片的類型
    $dest_ext = $this->get_file_ext($this->_dest);
 
    // 生成圖片
    switch($dest_ext){
      case 1: imagegif($new_img, $this->_dest, $this->_quality); break;
      case 2: imagejpeg($new_img, $this->_dest, $this->_quality); break;
      case 3: imagepng($new_img, $this->_dest, (int)(($this->_quality-1)/10)); break;
    }
 
    if(isset($source_img)){
      imagedestroy($source_img);
    }
 
    if(isset($new_img)){
      imagedestroy($new_img);
    }
 
    // 添加水印
    $this->add_watermark($this->_dest);
 
    return is_file($this->_dest)? true : false;
  }
 
  /** GD庫裁剪圖片
  * @return boolean
  */
  private function gd_crop(){
 
    // 獲取生成的圖片尺寸
    list($pic_w, $pic_h) = $this->get_size();
 
    // 獲取截圖的偏移量
    list($offset_w, $offset_h) = $this->get_crop_offset($pic_w, $pic_h);
 
    list($owidth, $oheight, $otype) = getimagesize($this->_source);
 
    switch($otype){
      case 1: $source_img = imagecreatefromgif($this->_source); break;
      case 2: $source_img = imagecreatefromjpeg($this->_source); break;
      case 3: $source_img = imagecreatefrompng($this->_source); break;
      default: return false;
    }
 
    // 按比例縮略/拉伸圖片
    $tmp_img = imagecreatetruecolor($pic_w, $pic_h);
    imagecopyresampled($tmp_img, $source_img, 0, 0, 0, 0, $pic_w, $pic_h, $owidth, $oheight);
 
    // 裁剪圖片
    $new_img = imagecreatetruecolor($this->_width, $this->_height);
    imagecopyresampled($new_img, $tmp_img, 0, 0, $offset_w, $offset_h, $this->_width, $this->_height, $this->_width, $this->_height);
 
    // 獲取目標圖片的類型
    $dest_ext = $this->get_file_ext($this->_dest);
 
    // 生成圖片
    switch($dest_ext){
      case 1: imagegif($new_img, $this->_dest, $this->_quality); break;
      case 2: imagejpeg($new_img, $this->_dest, $this->_quality); break;
      case 3: imagepng($new_img, $this->_dest, (int)(($this->_quality-1)/10)); break;
    }
 
    if(isset($source_img)){
      imagedestroy($source_img);
    }
 
    if(isset($tmp_img)){
      imagedestroy($tmp_img);
    }
 
    if(isset($new_img)){
      imagedestroy($new_img);
    }
 
    // 添加水印
    $this->add_watermark($this->_dest);
 
    return is_file($this->_dest)? true : false;
  }
 
  /** 獲取目標圖生成的size
  * @return Array $width, $height
  */
  private function get_size(){
    list($owidth, $oheight) = getimagesize($this->_source);
    $width = (int)($this->_width);
    $height = (int)($this->_height);
     
    switch($this->_type){
      case 'fit':
        $pic_w = $width;
        $pic_h = (int)($pic_w*$oheight/$owidth);
        if($pic_h>$height){
          $pic_h = $height;
          $pic_w = (int)($pic_h*$owidth/$oheight);
        }
        break;
      case 'crop':
        if($owidth>$oheight){
          $pic_h = $height;
          $pic_w = (int)($pic_h*$owidth/$oheight);
        }else{
          $pic_w = $width;
          $pic_h = (int)($pic_w*$oheight/$owidth);
        }
        break;
    }
    return array($pic_w, $pic_h);
  }
 
  /** 獲取截圖的偏移量
  * @param int $pic_w 圖寬度
  * @param int $pic_h 圖高度
  * @return Array $offset_w, $offset_h
  */
  private function get_crop_offset($pic_w, $pic_h){
    $offset_w = 0;
    $offset_h = 0;
     
    switch(strtoupper($this->_croppos)){
      case 'TL':
        $offset_w = 0;
        $offset_h = 0;
        break;
 
      case 'TM':
        $offset_w = (int)(($pic_w-$this->_width)/2);
        $offset_h = 0;
        break;
 
      case 'TR':
        $offset_w = (int)($pic_w-$this->_width);
        $offset_h = 0;
        break;
 
      case 'ML':
        $offset_w = 0;
        $offset_h = (int)(($pic_h-$this->_height)/2);
        break;
 
      case 'MM':
        $offset_w = (int)(($pic_w-$this->_width)/2);
        $offset_h = (int)(($pic_h-$this->_height)/2);
        break;
 
      case 'MR':
        $offset_w = (int)($pic_w-$this->_width);
        $offset_h = (int)(($pic_h-$this->_height)/2);
        break;
 
      case 'BL':
        $offset_w = 0;
        $offset_h = (int)($pic_h-$this->_height);
        break;
 
      case 'BM':
        $offset_w = (int)(($pic_w-$this->_width)/2);
        $offset_h = (int)($pic_h-$this->_height);
        break;
 
      case 'BR':
        $offset_w = (int)($pic_w-$this->_width);
        $offset_h = (int)($pic_h-$this->_height);
        break;
    }
    return array($offset_w, $offset_h);
  }
 
  /** 添加水印
  * @param String $dest 圖片路徑
  */
  private function add_watermark($dest){
    if($this->_watermark!=null && file_exists($this->_watermark) && file_exists($dest)){
      list($owidth, $oheight, $otype) = getimagesize($dest);
      list($w, $h, $wtype) = getimagesize($this->_watermark);
 
      // 水印圖比原圖要小才加水印
      if($w<=$owidth && $h<=$oheight){
 
        if($this->_handler=='imagemagick'){ // imagemagick 添加水印
 
          $cmd = sprintf("composite -gravity %s -geometry %s -dissolve %s '%s' %s %s", $this->_gravity, $this->_geometry, $this->_opacity, $this->_watermark, $dest, $dest);
 
          $this->to_log($cmd);
 
          exec($cmd);
 
        }else{ // gd 添加水印
 
          switch($wtype){
            case 1: $water_img = imagecreatefromgif($this->_watermark); break;
            case 2: $water_img = imagecreatefromjpeg($this->_watermark); break;
            case 3: $water_img = imagecreatefrompng($this->_watermark); break;
            default: return false;
          }
 
          switch($otype){
            case 1: $dest_img = imagecreatefromgif($dest); break;
            case 2: $dest_img = imagecreatefromjpeg($dest); break;
            case 3: $dest_img = imagecreatefrompng($dest); break;
            default: return false;
          }
 
          // 水印位置
          switch(strtolower($this->_gravity)){
            case 'northwest':
              $posX = 0;
              $posY = 0;
              break;
            case 'north':
              $posX = ($owidth - $w) / 2;
              $posY = 0;
              break;
            case 'northeast':
              $posX = $owidth - $w;
              $posY = 0;
              break;
            case 'west':
              $posX = 0;
              $posY = ($oheight - $h) / 2;
              break;
            case 'center':
              $posX = ($owidth - $w) / 2;
              $posY = ($oheight - $h) / 2;
              break;
            case 'east':
              $posX = $owidth - $w;
              $posY = ($oheight - $h) / 2;
              break;
            case 'southwest':
              $posX = 0;
              $posY = $oheight - $h;
              break;
            case 'south':
              $posX = ($owidth - $w) / 2;
              $posY = $oheight - $h;
              break;
            case 'southeast':
              $posX = $owidth - $w;
              $posY = $oheight - $h;
              break;
          }
 
          imagealphablending($dest_img, true);
          imagecopy($dest_img, $water_img, $posX, $posY, 0, 0, $w, $h);
 
          switch($otype){
            case 1:imagegif($dest_img, $dest, $this->_quality); break;
            case 2:imagejpeg($dest_img, $dest, $this->_quality); break;
            case 3:imagepng($dest_img, $dest, (int)(($this->_quality-1)/10)); break;
          }
 
          if(isset($water_img)){
            imagedestroy($water_img);
          }
 
          if(isset($dest_img)){
            imagedestroy($dest_img);
          }
        }
      }
    }
  }
 
  /** 判斷處理程序是否已安裝
  * @return boolean
  */
  private function check_handler(){
 
    $handler = $this->_handler;
 
    if(!in_array($handler, array('imagemagick', 'gd', null))){
      return false;
    }
 
    // 檢查是否有安裝imagemagick
    $imagemagick_installed = strstr(shell_exec('convert -version'),'Version: ImageMagick')!=''? true : false;
 
    // 檢查是否有安裝gd庫
    $gd_installed = function_exists('gd_info')? true : false;
 
    switch($handler){
      case 'imagemagick':
        return $imagemagick_installed;
        break;
 
      case 'gd':
        return $gd_installed;
        break;
 
      case null:
        if($imagemagick_installed){
          $this->_handler = 'imagemagick';
          return true;
        }
 
        if($gd_installed){
          $this->_handler = 'gd';
          return true;
        }
        break;
    }
    return false;
  }
 
  /** 創建圖片目錄
  * @param String $path
  * @return boolean
  */
  private function create_dirs($dest){
    if(!is_dir(dirname($dest))){
      return mkdir(dirname($dest), 0777, true);
    }
    return true;
  }
 
  /** 判斷參數是否存在
  * @param Array  $obj 數組對象
  * @param String $key 要查找的key
  * @return boolean
  */
  private function exists($obj,$key=''){
    if($key==''){
      return isset($obj) && !empty($obj);
    }else{
      $keys = explode('.',$key);
      for($i=0,$max=count($keys); $i<$max; $i++){
        if(isset($obj[$keys[$i]])){
          $obj = $obj[$keys[$i]];
        }else{
          return false;
        }
      }
      return isset($obj) && !empty($obj);
    }
  }
 
  /** 記錄log
  * @param String $msg 要記錄的log訊息
  */
  private function to_log($msg){
    if($this->_log){
      $msg = '['.date('Y-m-d H:i:s').']'.$msg."\r\n";
      file_put_contents($this->_log, $msg, FILE_APPEND);
    }
  }
 
  /** hex顏色轉rgb顏色
  * @param String $color hex顏色
  * @return Array
  */
  private function hex2rgb($hexcolor){
    $color = str_replace('#', '', $hexcolor);
    if (strlen($color) > 3) {
      $rgb = array(
        'r' => hexdec(substr($color, 0, 2)),
        'g' => hexdec(substr($color, 2, 2)),
        'b' => hexdec(substr($color, 4, 2))
      );
    } else {
      $r = substr($color, 0, 1) . substr($color, 0, 1);
      $g = substr($color, 1, 1) . substr($color, 1, 1);
      $b = substr($color, 2, 1) . substr($color, 2, 1);
      $rgb = array(
        'r' => hexdec($r),
        'g' => hexdec($g),
        'b' => hexdec($b)
      );
    }
    return $rgb;
  }
 
  /** 獲取圖片類型
  * @param String $file 圖片路徑
  * @return int
  */
  private function get_file_ext($file){
    $filename = basename($file);
    list($name, $ext)= explode('.', $filename);
 
    $ext_type = 0;
 
    switch(strtolower($ext)){
      case 'jpg':
      case 'jpeg':
        $ext_type = 2;
        break;
      case 'gif':
        $ext_type = 1;
        break;
      case 'png':
        $ext_type = 3;
        break;
    }
    return $ext_type;
  }
 
} // class end
?>

demo示例代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
define('ROOT', dirname(__FILE__));
 
require(ROOT."/PicThumb.class.php");
 
$logfile = ROOT.'/PicThumb.log';
$source1 = ROOT.'/pic/source.jpg';
$dest1 = ROOT.'/pic/1.jpg';
$dest2 = ROOT.'/pic/2.gif';
$dest3 = ROOT.'/pic/3.png';
 
$source2 = ROOT.'/pic/source_cmyk.jpg';
$dest4 = ROOT.'/pic/4.jpg';
$dest5 = ROOT.'/pic/5.gif';
$dest6 = ROOT.'/pic/6.png';
 
$watermark = ROOT.'/pic/watermark.png';
 
// 按比例生成縮略圖
$param = array(
  'type' => 'fit',
  'width' => 100,
  'height' => 100,
);
 
$obj = new PicThumb($logfile);
$obj->set_config($param);
$flag = $obj->create_thumb($source1, $dest1);
 
if($flag){
  echo '<img src="pic/'.basename($dest1).'">';
}else{
  echo 'create thumb fail';
}
 
// 按比例生成縮略圖,不足部分用#FF0000填充
$param = array(
  'type' => 'fit',
  'width' => 100,
  'height' => 100,
  'bgcolor' => '#FFFF00'
);
 
$obj = new PicThumb($logfile);
$obj->set_config($param);
$flag = $obj->create_thumb($source1, $dest2);
 
if($flag){
  echo '<img src="pic/'.basename($dest2).'">';
}else{
  echo 'create thumb fail';
}
 
// 裁剪250x250的縮略圖,裁剪位置是底部中間,水印位置西南,透明度50
$param = array(
  'type' => 'crop',
  'croppos' => 'BM',
  'width' => 250,
  'height' => 250,
  'watermark' => $watermark,
  'opacity' => 50,
  'gravity' => 'SouthWest'
);
 
$obj = new PicThumb($logfile);
$obj->set_config($param);
$flag = $obj->create_thumb($source1, $dest3);
 
if($flag){
  echo '<img src="pic/'.basename($dest3).'">';
}else{
  echo 'create thumb fail';
}
 
// 按比例生成縮略圖 CMYK格式
$param = array(
  'type' => 'fit',
  'width' => 100,
  'height' => 100,
);
 
$obj = new PicThumb($logfile);
$obj->set_config($param);
$flag = $obj->create_thumb($source2, $dest4);
 
if($flag){
  echo '<img src="pic/'.basename($dest4).'">';
}else{
  echo 'create thumb fail';
}
 
// 按比例生成縮略圖,不足部分用#FF0000填充 CMYK格式
$param = array(
  'type' => 'fit',
  'width' => 100,
  'height' => 100,
  'bgcolor' => '#FFFF00'
);
 
$obj = new PicThumb($logfile);
$obj->set_config($param);
$flag = $obj->create_thumb($source2, $dest5);
 
if($flag){
  echo '<img src="pic/'.basename($dest5).'">';
}else{
  echo 'create thumb fail';
}
 
// 裁剪250x250的縮略圖,裁剪位置是底部中間,水印位置西南,透明度50 CMYK格式
$param = array(
  'type' => 'crop',
  'croppos' => 'BM',
  'width' => 250,
  'height' => 250,
  'watermark' => $watermark,
  'opacity' => 50,
  'gravity' => 'SouthWest'
);
 
$obj = new PicThumb($logfile);
$obj->set_config($param);
$flag = $obj->create_thumb($source2, $dest6);
 
if($flag){
  echo '<img src="pic/'.basename($dest6).'">';
}else{
  echo 'create thumb fail';
}
?>

本文完整實例代碼點擊此處本站下載

希望本文所述對大家的PHP程序設計有所幫助

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲在线 | 亚洲一区精品在线 | 在线国产精品一区 | 夜夜av | 欲色视频| 国产精品久久久久久久久久久久久 | 国产亚洲精品久久久久动 | 日韩中文字幕在线免费观看 | 欧洲一区二区三区 | 国产91精品一区二区绿帽 | 视频一区二区中文字幕 | 日韩精品无码一区二区三区 | 国产精品视频导航 | 精品国产欧美 | 日韩无在线 | 香蕉久久一区二区不卡无毒影院 | 天堂成人av | 天堂中文视频在线观看 | 成人精品一区二区三区中文字幕 | 成人午夜在线 | 九色在线观看 | 亚洲国产婷婷香蕉久久久久久99 | 亚洲成人久久久久 | 看av的网址 | 欧美午夜一区二区三区免费大片 | 亚洲在线中文字幕 | 麻豆产精国品免费入口 | 日韩一区二区三区在线视频 | 91精品在线播放 | 久久五月天婷婷 | 国外成人在线视频网站 | 欧美福利视频 | 亚洲精品一区二区三区四区高清 | 精品无人乱码一区二区三区的优势 | 精品专区 | 久久99精品国产麻豆婷婷洗澡 | 精品免费av| 欧美一区二区在线播放 | 亚洲精品久久久久久久久久久久久 | 久久懂色精品99综一区合 | 99久久婷婷国产综合精品电影 |