PHP语言 缩略图生成类,支持imagemagick及gd库两种处理
小标 2018-07-12 来源 : 阅读 867 评论 0

摘要:本文主要向大家介绍了PHP语言 缩略图生成类,支持imagemagick及gd库两种处理,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

本文主要向大家介绍了PHP语言 缩略图生成类,支持imagemagick及gd库两种处理,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

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

使用imagemagick/GD库实现,imagemagick地址:www.imagemagick.org
需要安装imagemagick,安装方法如下:点击查看

PicThumb.class.php

[php] view plain copy

1. <?php  

2. /** 缩略图生成类,支持imagemagick及gd库两种处理 

3. *   Date:   2013-07-15 

4. *   Author: fdipzone 

5. *   Ver:    1.2 

6. * 

7. *   Func: 

8. *   public  set_config: 设置参数 

9. *   public  create_thumb: 生成缩略图 

10. *   private fit: 缩略图片 

11. *   private crop: 裁剪图片 

12. *   private gd_fit: GD库缩略图片 

13. *   private gd_crop: GD库裁剪图片 

14. *   private get_size: 获取要转换的size 

15. *   private get_crop_offset: 获取裁图的偏移量 

16. *   private add_watermark: 添加水印 

17. *   private check_handler: 判断处理程序是否已安装 

18. *   private create_dirs: 创建目录 

19. *   private exists: 判断参数是否存在 

20. *   private to_log: 记录log 

21. *   private hex2rgb: hex颜色转rgb颜色 

22. *   private get_file_ext: 获取图片类型 

23. * 

24. *   ver:    1.1 增加GD库处理 

25. *   ver:    1.2 增加width,height错误参数处理 

26. *               增加当图片colorspace不为RGB时作转RGB处理 

27. *               修正使用crop保存为gif时出现透明无效区域问题,使用+repage参数,删除透明无效区域即可 

28. * 

29. *   tips:建议使用imagemagick 

30. *        GD库不支持透明度水印,如果必须使用透明水印,请将水印图片做成有透明度。 

31. *        GD库输出gif如加透明水印,会有问题。 

32. */  

33.   

34. class PicThumb{ // class start  

35.   

36.     private $_log = null;            // log file  

37.     private $_handler = null;        // 进行图片处理的程序,imagemagick/gd库  

38.     private $_type = 'fit';          // fit or crop  

39.     private $_source = null;         // 原图路径  

40.     private $_dest = null;           // 缩略图路径  

41.     private $_watermark = null;      // 水印图片  

42.     private $_opacity = 75;          // 水印圖片透明度,gd库不支持  

43.     private $_gravity = 'SouthEast'; // 水印摆放位置 NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast  

44.     private $_geometry = '+10+10';   // 水印定位,gd库不支持  

45.     private $_croppos = 'TL';        // 截图的位置 TL TM TR ML MM MR BL BM BR  

46.     private $_bgcolor = null;        // 填充的背景色  

47.     private $_quality = 90;          // 生成的图片质量  

48.     private $_width = null;          // 指定区域宽度  

49.     private $_height = null;         // 指定区域高度  

50.   

51.   

52.     // 初始化  

53.     public function __construct($logfile=''){  

54.         if($logfile!=''){  

55.             $this->_log = $logfile;  

56.         }  

57.     }  

58.   

59.   

60.     // 设置参数  

61.     public function set_config($param=array()){  

62.         $this->_handler = $this->exists($param, 'handler')? strtolower($param['handler']) : null;  

63.         $this->_type = $this->exists($param, 'type')? strtolower($param['type']) : 'fit';  

64.         $this->_watermark = $this->exists($param, 'watermark')? $param['watermark'] : null;  

65.         $this->_opacity = $this->exists($param, 'opacity')? $param['opacity'] : 75;  

66.         $this->_gravity = $this->exists($param, 'gravity')? $param['gravity'] : 'SouthEast';  

67.         $this->_geometry = $this->exists($param, 'geometry')? $param['geometry'] : '+10+10';  

68.         $this->_croppos = $this->exists($param, 'croppos')? $param['croppos'] : 'TL';  

69.         $this->_bgcolor = $this->exists($param, 'bgcolor')? $param['bgcolor'] : null;  

70.         $this->_quality = $this->exists($param, 'quality')? $param['quality'] : 90;  

71.         $this->_width = $this->exists($param, 'width')? $param['width'] : null;  

72.         $this->_height = $this->exists($param, 'height')? $param['height'] : null;  

73.     }  

74.   

75.   

76.     /** 创建缩略图 

77.     * @param String $source 原图 

78.     * @param String $dest   目标图 

79.     * @return boolean 

80.     */  

81.     public function create_thumb($source, $dest){  

82.   

83.         // 检查使用的handler是否已安装  

84.         if(!$this->check_handler()){  

85.             $this->to_log('handler not installed');  

86.             return false;  

87.         }  

88.   

89.         // 判断区域宽高是否正确  

90.         if(!is_numeric($this->_width) || !is_numeric($this->_height) || $this->_width<=0 || $this->_height<=0){  

91.             $this->to_log('width or height invalid');  

92.             return false;  

93.         }  

94.   

95.         // 判断源文件是否存在  

96.         if(!file_exists($source)){  

97.             $this->to_log($source.' not exists');  

98.             return false;  

99.         }  

100.   

101.         // 创建目标文件路径  

102.         if(!$this->create_dirs($dest)){  

103.             $this->to_log(dirname($dest).' create fail');  

104.             return false;  

105.         }  

106.   

107.         $this->_source = $source;   // 源文件  

108.         $this->_dest = $dest;       // 目标文件  

109.   

110.         // 处理图片  

111.         switch($this->_type){  

112.             case 'fit':  

113.                 if($this->_handler=='imagemagick'){  

114.                     return $this->fit();  

115.                 }else{  

116.                     return $this->gd_fit();  

117.                 }  

118.                 break;  

119.   

120.             case 'crop':  

121.                 if($this->_handler=='imagemagick'){  

122.                     return $this->crop();  

123.                 }else{  

124.                     return $this->gd_crop();  

125.                 }  

126.                 break;  

127.   

128.             default:  

129.                 $this->to_log($this->_type.' not fit and crop');  

130.                 return false;  

131.         }  

132.   

133.     }  

134.   

135.   

136.     /** 按比例压缩或拉伸图片 

137.     * @return boolean 

138.     */  

139.     private function fit(){  

140.   

141.         // 判断是否填充背景  

142.         $bgcolor = ($this->_bgcolor!=null)?   

143.         sprintf(" -background '%s' -gravity center -extent '%sx%s' ", $this->_bgcolor, $this->_width, $this->_height) : "";  

144.   

145.         // 判断是否要转为RGB  

146.         $source_info = getimagesize($this->_source);  

147.         $colorspace = (!isset($source_info['channels']) || $source_info['channels']!=3)? ' -colorspace RGB ' : '';  

148.   

149.         // 命令行  

150.         $cmd = sprintf("convert -resize '%sx%s' '%s' %s -quality %s %s '%s'", $this->_width, $this->_height, $this->_source, $bgcolor, $this->_quality, $colorspace, $this->_dest);  

151.   

152.         // 记录执行的命令  

153.         $this->to_log($cmd);  

154.   

155.         // 执行命令  

156.         exec($cmd);  

157.   

158.         // 添加水印  

159.         $this->add_watermark($this->_dest);  

160.   

161.         return is_file($this->_dest)? true : false;  

162.   

163.     }  

164.   

165.   

166.     /** 裁剪图片 

167.     * @return boolean 

168.     */  

169.     private function crop(){  

170.   

171.         // 获取生成的图片尺寸  

172.         list($pic_w, $pic_h) = $this->get_size();  

173.   

174.         // 获取截图的偏移量  

175.         list($offset_w, $offset_h) = $this->get_crop_offset($pic_w, $pic_h);  

176.   

177.         // 判断是否要转为RGB  

178.         $source_info = getimagesize($this->_source);  

179.         $colorspace = (!isset($source_info['channels']) || $source_info['channels']!=3)? ' -colorspace RGB ' : '';  

180.   

181.         // 命令行  

182.         $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);  

183.   

184.         // 记录执行的命令  

185.         $this->to_log($cmd);  

186.   

187.         // 执行命令  

188.         exec($cmd);  

189.   

190.         // 添加水印  

191.         $this->add_watermark($this->_dest);  

192.   

193.         return is_file($this->_dest)? true : false;  

194.   

195.     }  

196.   

197.   

198.     /** GD库按比例压缩或拉伸图片 

199.     * @return boolean 

200.     */  

201.     private function gd_fit(){  

202.   

203.         // 获取生成的图片尺寸  

204.         list($pic_w, $pic_h) = $this->get_size();  

205.   

206.         list($owidth, $oheight, $otype) = getimagesize($this->_source);  

207.   

208.         switch($otype){  

209.             case 1: $source_img = imagecreatefromgif($this->_source); break;  

210.             case 2: $source_img = imagecreatefromjpeg($this->_source); break;  

211.             case 3: $source_img = imagecreatefrompng($this->_source); break;  

212.             default: return false;  

213.         }  

214.   

215.         // 按比例缩略/拉伸图片  

216.         $new_img = imagecreatetruecolor($pic_w, $pic_h);  

217.         imagecopyresampled($new_img, $source_img, 0, 0, 0, 0, $pic_w, $pic_h, $owidth, $oheight);  

218.   

219.         // 判断是否填充背景  

220.         if($this->_bgcolor!=null){  

221.             $bg_img = imagecreatetruecolor($this->_width, $this->_height);  

222.             $rgb = $this->hex2rgb($this->_bgcolor);  

223.             $bgcolor =imagecolorallocate($bg_img, $rgb['r'], $rgb['g'], $rgb['b']);  

224.             imagefill($bg_img, 0, 0, $bgcolor);  

225.             imagecopy($bg_img, $new_img, (int)(($this->_width-$pic_w)/2), (int)(($this->_height-$pic_h)/2), 0, 0, $pic_w, $pic_h);  

226.             $new_img = $bg_img;  

227.         }  

228.   

229.         // 获取目标图片的类型  

230.         $dest_ext = $this->get_file_ext($this->_dest);  

231.   

232.         // 生成图片  

233.         switch($dest_ext){  

234.             case 1: imagegif($new_img, $this->_dest, $this->_quality); break;  

235.             case 2: imagejpeg($new_img, $this->_dest, $this->_quality); break;  

236.             case 3: imagepng($new_img, $this->_dest, (int)(($this->_quality-1)/10)); break;  

237.         }  

238.   

239.         if(isset($source_img)){  

240.             imagedestroy($source_img);  

241.         }  

242.   

243.         if(isset($new_img)){  

244.             imagedestroy($new_img);  

245.         }  

246.   

247.         // 添加水印  

248.         $this->add_watermark($this->_dest);  

249.   

250.         return is_file($this->_dest)? true : false;  

251.   

252.     }  

253.   

254.   

255.     /** GD库裁剪图片 

256.     * @return boolean 

257.     */  

258.     private function gd_crop(){  

259.   

260.         // 获取生成的图片尺寸  

261.         list($pic_w, $pic_h) = $this->get_size();  

262.   

263.         // 获取截图的偏移量  

264.         list($offset_w, $offset_h) = $this->get_crop_offset($pic_w, $pic_h);  

265.   

266.         list($owidth, $oheight, $otype) = getimagesize($this->_source);  

267.   

268.         switch($otype){  

269.             case 1: $source_img = imagecreatefromgif($this->_source); break;  

270.             case 2: $source_img = imagecreatefromjpeg($this->_source); break;  

271.             case 3: $source_img = imagecreatefrompng($this->_source); break;  

272.             default: return false;  

273.         }  

274.   

275.         // 按比例缩略/拉伸图片  

276.         $tmp_img = imagecreatetruecolor($pic_w, $pic_h);  

277.         imagecopyresampled($tmp_img, $source_img, 0, 0, 0, 0, $pic_w, $pic_h, $owidth, $oheight);  

278.   

279.         // 裁剪图片  

280.         $new_img = imagecreatetruecolor($this->_width, $this->_height);  

281.         imagecopyresampled($new_img, $tmp_img, 0, 0, $offset_w, $offset_h, $this->_width, $this->_height, $this->_width, $this->_height);  

282.   

283.         // 获取目标图片的类型  

284.         $dest_ext = $this->get_file_ext($this->_dest);  

285.   

286.         // 生成图片  

287.         switch($dest_ext){  

288.             case 1: imagegif($new_img, $this->_dest, $this->_quality); break;  

289.             case 2: imagejpeg($new_img, $this->_dest, $this->_quality); break;  

290.             case 3: imagepng($new_img, $this->_dest, (int)(($this->_quality-1)/10)); break;  

291.         }  

292.   

293.         if(isset($source_img)){  

294.             imagedestroy($source_img);  

295.         }  

296.   

297.         if(isset($tmp_img)){  

298.             imagedestroy($tmp_img);  

299.         }  

300.   

301.         if(isset($new_img)){  

302.             imagedestroy($new_img);  

303.         }  

304.   

305.         // 添加水印  

306.         $this->add_watermark($this->_dest);  

307.   

308.         return is_file($this->_dest)? true : false;  

309.   

310.     }  

311.   

312.   

313.     /** 获取目标图生成的size 

314.     * @return Array $width, $height 

315.     */  

316.     private function get_size(){  

317.         list($owidth, $oheight) = getimagesize($this->_source);  

318.         $width = (int)($this->_width);  

319.         $height = (int)($this->_height);  

320.           

321.         switch($this->_type){  

322.             case 'fit':  

323.                 $pic_w = $width;  

324.                 $pic_h = (int)($pic_w*$oheight/$owidth);  

325.                 if($pic_h>$height){  

326.                     $pic_h = $height;  

327.                     $pic_w = (int)($pic_h*$owidth/$oheight);  

328.                 }  

329.                 break;  

330.             case 'crop':  

331.                 $pic_w = $width;  

332.                 $pic_h = (int)($pic_w*$oheight/$owidth);  

333.                 if($pic_h<$height){  

334.                     $pic_h = $height;  

335.                     $pic_w = (int)($pic_h*$owidth/$oheight);  

336.                 }  

337.                 break;  

338.         }  

339.   

340.         return array($pic_w, $pic_h);  

341.     }  

342.   

343.   

344.     /** 获取截图的偏移量 

345.     * @param int $pic_w 图宽度 

346.     * @param int $pic_h 图高度 

347.     * @return Array $offset_w, $offset_h 

348.     */  

349.     private function get_crop_offset($pic_w, $pic_h){  

350.         $offset_w = 0;  

351.         $offset_h = 0;  

352.           

353.         switch(strtoupper($this->_croppos)){  

354.             case 'TL':  

355.                 $offset_w = 0;  

356.                 $offset_h = 0;  

357.                 break;  

358.   

359.             case 'TM':  

360.                 $offset_w = (int)(($pic_w-$this->_width)/2);  

361.                 $offset_h = 0;  

362.                 break;  

363.   

364.             case 'TR':  

365.                 $offset_w = (int)($pic_w-$this->_width);  

366.                 $offset_h = 0;  

367.                 break;  

368.   

369.             case 'ML':  

370.                 $offset_w = 0;  

371.                 $offset_h = (int)(($pic_h-$this->_height)/2);  

372.                 break;  

373.   

374.             case 'MM':  

375.                 $offset_w = (int)(($pic_w-$this->_width)/2);  

376.                 $offset_h = (int)(($pic_h-$this->_height)/2);  

377.                 break;  

378.   

379.             case 'MR':  

380.                 $offset_w = (int)($pic_w-$this->_width);  

381.                 $offset_h = (int)(($pic_h-$this->_height)/2);  

382.                 break;  

383.   

384.             case 'BL':  

385.                 $offset_w = 0;  

386.                 $offset_h = (int)($pic_h-$this->_height);  

387.                 break;  

388.   

389.             case 'BM':  

390.                 $offset_w = (int)(($pic_w-$this->_width)/2);  

391.                 $offset_h = (int)($pic_h-$this->_height);  

392.                 break;  

393.   

394.             case 'BR':  

395.                 $offset_w = (int)($pic_w-$this->_width);  

396.                 $offset_h = (int)($pic_h-$this->_height);  

397.                 break;  

398.         }  

399.   

400.         return array($offset_w, $offset_h);  

401.     }  

402.   

403.   

404.     /** 添加水印 

405.     * @param String $dest 图片路径 

406.     */  

407.     private function add_watermark($dest){  

408.         if($this->_watermark!=null && file_exists($this->_watermark) && file_exists($dest)){  

409.             list($owidth, $oheight, $otype) = getimagesize($dest);  

410.             list($w, $h, $wtype) = getimagesize($this->_watermark);  

411.   

412.             // 水印图比原图要小才加水印  

413.             if($w<=$owidth && $h<=$oheight){  

414.   

415.                 if($this->_handler=='imagemagick'){ // imagemagick 添加水印  

416.   

417.                     $cmd = sprintf("composite -gravity %s -geometry %s -dissolve %s '%s' %s %s", $this->_gravity, $this->_geometry, $this->_opacity, $this->_watermark, $dest, $dest);  

418.   

419.                     $this->to_log($cmd);  

420.   

421.                     exec($cmd);  

422.   

423.                 }else{ // gd 添加水印  

424.   

425.                     switch($wtype){  

426.                         case 1: $water_img = imagecreatefromgif($this->_watermark); break;  

427.                         case 2: $water_img = imagecreatefromjpeg($this->_watermark); break;  

428.                         case 3: $water_img = imagecreatefrompng($this->_watermark); break;  

429.                         default: return false;  

430.                     }  

431.   

432.                     switch($otype){  

433.                         case 1: $dest_img = imagecreatefromgif($dest); break;  

434.                         case 2: $dest_img = imagecreatefromjpeg($dest); break;  

435.                         case 3: $dest_img = imagecreatefrompng($dest); break;  

436.                         default: return false;  

437.                     }  

438.   

439.                     // 水印位置  

440.                     switch(strtolower($this->_gravity)){  

441.                         case 'northwest':  

442.                             $posX = 0;  

443.                             $posY = 0;  

444.                             break;  

445.                         case 'north':  

446.                             $posX = ($owidth - $w) / 2;  

447.                             $posY = 0;  

448.                             break;  

449.                         case 'northeast':  

450.                             $posX = $owidth - $w;  

451.                             $posY = 0;  

452.                             break;  

453.                         case 'west':  

454.                             $posX = 0;  

455.                             $posY = ($oheight - $h) / 2;  

456.                             break;  

457.                         case 'center':  

458.                             $posX = ($owidth - $w) / 2;  

459.                             $posY = ($oheight - $h) / 2;  

460.                             break;  

461.                         case 'east':  

462.                             $posX = $owidth - $w;  

463.                             $posY = ($oheight - $h) / 2;  

464.                             break;  

465.                         case 'southwest':  

466.                             $posX = 0;  

467.                             $posY = $oheight - $h;  

468.                             break;  

469.                         case 'south':  

470.                             $posX = ($owidth - $w) / 2;  

471.                             $posY = $oheight - $h;  

472.                             break;  

473.                         case 'southeast':  

474.                             $posX = $owidth - $w;  

475.                             $posY = $oheight - $h;  

476.                             break;  

477.                     }  

478.   

479.                     imagealphablending($dest_img, true);  

480.                     imagecopy($dest_img, $water_img, $posX, $posY, 0, 0, $w, $h);  

481.   

482.                     switch($otype){  

483.                         case 1:imagegif($dest_img, $dest, $this->_quality); break;  

484.                         case 2:imagejpeg($dest_img, $dest, $this->_quality); break;  

485.                         case 3:imagepng($dest_img, $dest, (int)(($this->_quality-1)/10)); break;  

486.                     }  

487.   

488.                     if(isset($water_img)){  

489.                         imagedestroy($water_img);  

490.                     }  

491.   

492.                     if(isset($dest_img)){  

493.                         imagedestroy($dest_img);  

494.                     }  

495.   

496.                 }  

497.             }  

498.         }  

499.     }  

500.   

501.   

502.     /** 判断处理程序是否已安装 

503.     * @return boolean 

504.     */  

505.     private function check_handler(){  

506.   

507.         $handler = $this->_handler;  

508.   

509.         if(!in_array($handler, array('imagemagick', 'gd', null))){  

510.             return false;  

511.         }  

512.   

513.         // 检查是否有安装imagemagick  

514.         $imagemagick_installed = strstr(shell_exec('convert -version'),'Version: ImageMagick')!=''? true : false;  

515.   

516.         // 检查是否有安装gd库  

517.         $gd_installed = function_exists('gd_info')? true : false;  

518.   

519.         switch($handler){  

520.             case 'imagemagick':  

521.                 return $imagemagick_installed;  

522.                 break;  

523.   

524.             case 'gd':  

525.                 return $gd_installed;  

526.                 break;  

527.   

528.             case null:  

529.                 if($imagemagick_installed){  

530.                     $this->_handler = 'imagemagick';  

531.                     return true;  

532.                 }  

533.   

534.                 if($gd_installed){  

535.                     $this->_handler = 'gd';  

536.                     return true;  

537.                 }  

538.                 break;  

539.         }  

540.   

541.         return false;  

542.     }  

543.   

544.   

545.     /** 创建图片目录 

546.     * @param String $path 

547.     * @return boolean 

548.     */  

549.     private function create_dirs($dest){  

550.         if(!is_dir(dirname($dest))){  

551.             return mkdir(dirname($dest), 0777, true);  

552.         }  

553.         return true;  

554.     }  

555.   

556.   

557.     /** 判断参数是否存在 

558.     * @param  Array   $obj  数组对象 

559.     * @param  String  $key  要查找的key 

560.     * @return boolean 

561.     */  

562.     private function exists($obj,$key=''){  

563.         if($key==''){  

564.             return isset($obj) && !empty($obj);  

565.         }else{  

566.             $keys = explode('.',$key);  

567.             for($i=0,$max=count($keys); $i<$max; $i++){  

568.                 if(isset($obj[$keys[$i]])){  

569.                     $obj = $obj[$keys[$i]];  

570.                 }else{  

571.                     return false;  

572.                 }  

573.             }  

574.             return isset($obj) && !empty($obj);  

575.         }  

576.     }  

577.   

578.   

579.     /** 记录log 

580.     * @param String $msg 要记录的log讯息 

581.     */  

582.     private function to_log($msg){  

583.         if($this->_log){  

584.             $msg = '['.date('Y-m-d H:i:s').']'.$msg."\r\n";  

585.             file_put_contents($this->_log, $msg, FILE_APPEND);  

586.         }  

587.     }  

588.   

589.   

590.     /** hex颜色转rgb颜色 

591.     * @param  String $color hex颜色 

592.     * @return Array 

593.     */  

594.     private function hex2rgb($hexcolor){  

595.         $color = str_replace('#', '', $hexcolor);  

596.         if (strlen($color) > 3) {  

597.             $rgb = array(  

598.                 'r' => hexdec(substr($color, 0, 2)),  

599.                 'g' => hexdec(substr($color, 2, 2)),  

600.                 'b' => hexdec(substr($color, 4, 2))  

601.             );  

602.         } else {  

603.             $r = substr($color, 0, 1) . substr($color, 0, 1);  

604.             $g = substr($color, 1, 1) . substr($color, 1, 1);  

605.             $b = substr($color, 2, 1) . substr($color, 2, 1);  

606.             $rgb = array(  

607.                 'r' => hexdec($r),  

608.                 'g' => hexdec($g),  

609.                 'b' => hexdec($b)  

610.             );  

611.         }  

612.         return $rgb;  

613.     }  

614.   

615.   

616.     /** 获取图片类型 

617.     * @param  String $file 图片路径 

618.     * @return int 

619.     */  

620.     private function get_file_ext($file){  

621.         $filename = basename($file);  

622.         list($name, $ext)= explode('.', $filename);  

623.   

624.         $ext_type = 0;  

625.   

626.         switch(strtolower($ext)){  

627.             case 'jpg':  

628.             case 'jpeg':  

629.                 $ext_type = 2;  

630.                 break;  

631.             case 'gif':  

632.                 $ext_type = 1;  

633.                 break;  

634.             case 'png':  

635.                 $ext_type = 3;  

636.                 break;  

637.         }  

638.   

639.         return $ext_type;  

640.     }  

641.   

642. } // class end  

643.   

644. ?>  

demo:

[php] view plain copy

1. <?php  

2. define('ROOT', dirname(__FILE__));  

3.   

4. require(ROOT."/PicThumb.class.php");  

5.   

6. $logfile = ROOT.'/PicThumb.log';  

7. $source1 = ROOT.'/pic/source.jpg';  

8. $dest1 = ROOT.'/pic/1.jpg';  

9. $dest2 = ROOT.'/pic/2.gif';  

10. $dest3 = ROOT.'/pic/3.png';  

11.   

12. $source2 = ROOT.'/pic/source_cmyk.jpg';  

13. $dest4 = ROOT.'/pic/4.jpg';  

14. $dest5 = ROOT.'/pic/5.gif';  

15. $dest6 = ROOT.'/pic/6.png';  

16.   

17. $watermark = ROOT.'/pic/watermark.png';  

18.   

19. // 按比例生成缩略图  

20. $param = array(  

21.     'type' => 'fit',  

22.     'width' => 100,  

23.     'height' => 100,  

24. );  

25.   

26. $obj = new PicThumb($logfile);  

27. $obj->set_config($param);  

28. $flag = $obj->create_thumb($source1, $dest1);  

29.   

30. if($flag){  

31.     echo '<img src="pic/'.basename($dest1).'">';  

32. }else{  

33.     echo 'create thumb fail';  

34. }  

35.   

36.   

37. // 按比例生成缩略图,不足部分用#FF0000填充  

38. $param = array(  

39.     'type' => 'fit',  

40.     'width' => 100,  

41.     'height' => 100,  

42.     'bgcolor' => '#FFFF00'  

43. );  

44.   

45. $obj = new PicThumb($logfile);  

46. $obj->set_config($param);  

47. $flag = $obj->create_thumb($source1, $dest2);  

48.   

49. if($flag){  

50.     echo '<img src="pic/'.basename($dest2).'">';  

51. }else{  

52.     echo 'create thumb fail';  

53. }  

54.   

55.   

56. // 裁剪250x250的缩略图,裁剪位置是底部中间,水印位置西南,透明度50  

57. $param = array(  

58.     'type' => 'crop',  

59.     'croppos' => 'BM',  

60.     'width' => 250,  

61.     'height' => 250,  

62.     'watermark' => $watermark,  

63.     'opacity' => 50,  

64.     'gravity' => 'SouthWest'  

65. );  

66.   

67. $obj = new PicThumb($logfile);  

68. $obj->set_config($param);  

69. $flag = $obj->create_thumb($source1, $dest3);  

70.   

71. if($flag){  

72.     echo '<img src="pic/'.basename($dest3).'">';  

73. }else{  

74.     echo 'create thumb fail';  

75. }  

76.   

77.   

78. // 按比例生成缩略图 CMYK格式  

79. $param = array(  

80.     'type' => 'fit',  

81.     'width' => 100,  

82.     'height' => 100,  

83. );  

84.   

85. $obj = new PicThumb($logfile);  

86. $obj->set_config($param);  

87. $flag = $obj->create_thumb($source2, $dest4);  

88.   

89. if($flag){  

90.     echo '<img src="pic/'.basename($dest4).'">';  

91. }else{  

92.     echo 'create thumb fail';  

93. }  

94.   

95.   

96. // 按比例生成缩略图,不足部分用#FF0000填充 CMYK格式  

97. $param = array(  

98.     'type' => 'fit',  

99.     'width' => 100,  

100.     'height' => 100,  

101.     'bgcolor' => '#FFFF00'  

102. );  

103.   

104. $obj = new PicThumb($logfile);  

105. $obj->set_config($param);  

106. $flag = $obj->create_thumb($source2, $dest5);  

107.   

108. if($flag){  

109.     echo '<img src="pic/'.basename($dest5).'">';  

110. }else{  

111.     echo 'create thumb fail';  

112. }  

113.   

114.   

115. // 裁剪250x250的缩略图,裁剪位置是底部中间,水印位置西南,透明度50 CMYK格式  

116. $param = array(  

117.     'type' => 'crop',  

118.     'croppos' => 'BM',  

119.     'width' => 250,  

120.     'height' => 250,  

121.     'watermark' => $watermark,  

122.     'opacity' => 50,  

123.     'gravity' => 'SouthWest'  

124. );  

125.   

126. $obj = new PicThumb($logfile);  

127. $obj->set_config($param);  

128. $flag = $obj->create_thumb($source2, $dest6);  

129.   

130. if($flag){  

131.     echo '<img src="pic/'.basename($dest6).'">';  

132. }else{  

133.     echo 'create thumb fail';  

134. }  

135. ?>  

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言PHP频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程