GD库添加图片水印和缩略图
2016-03-16 13:50
295 查看
在平时,我们经常遇到给图片加水印的问题,例如给图片加水印做版权保护,我们可以通过GD库来解决这个问题。
首先就是开启GD库
在php.ini文件里面可以开启GD库。
建立操作图像类image.class.php
测试test.php(测试字体水印和缩略图)
总结:1.这篇写的太草率了;
2.掌握了面向对象的思想在编程中真的很有用。
首先就是开启GD库
在php.ini文件里面可以开启GD库。
建立操作图像类image.class.php
<?php class Image { /*图片基本信息*/ private $info; /*打开图片,读取到内存中*/ public function __construct($src) { $info = getimagesize($src); $this->info = array( 'width' => $info[0], 'height' => $info[1], 'type' => image_type_to_extension($info[2],false), 'mime' => $info['mime'] ); $fun = "imagecreatefrom{$this->info['type']}"; $this->image = $fun($src); } /*压缩图片*/ public function thumb($width, $height) { $image_thumb = imagecreatetruecolor($width, $height); imagecopyresampled($image_thumb, $this->image, 0, 0, 0, 0, $width, $height, $this->info['width'], $this->info['height']); imagedestroy($this->image); $this->image = $image_thumb; } /*操作图片(添加文字水印)*/ public function fontMark($content, $font_url, $size, $color, $local, $angle) { $col = imagecolorallocatealpha($this->image, $color[0], $color[1], $color[2]); imagettftext($this->image, $size, $angle, $local['x'] 4000 , $local['y'], $col, $font_url, $content); } /*操作图片(添加图片水印)*/ public function imageMark($source, $local, $alpha) { $info = getimagesize($source); $type = image_type_to_extension($info[2],false); $fun = "imagecreatefrom{$type}"; $water = $fun($source); imagecopymerge($this->image, $water, $local['x'], $local['y'], 0, 0, $info[0], $info[1], $alpha); imagedestroy($water); } /*在浏览器中输出图片*/ public function show() { header("Content-type:".$this->info['mime']); $funs = "image{$this->info['type']}"; $funs($this->image); } /*把图片保存到硬盘中*/ public function save($newname) { $funs = "image{$this->info['type']}"; $funs($this->image, $newname.".".$this->info['type']); } /*销毁图片*/ public function __destruct() { imagedestroy($this->image); } } ?>
测试test.php(测试字体水印和缩略图)
require 'image.class.php'; $src = "image1.jpg"; $content = "Hello World!"; $font_url = "msyh.ttf"; $size = 20; $color = array( 0 => 255, 1 => 255, 2 => 255, 3 => 20 ); $local = array( 'x' => 20, 'y' => 70 ); $angle = 10; $image = new Image($src); $image->fontMark($content, $font_url, $size, $color, $local, $angle); $image->show(); $image->save("fontMark");
总结:1.这篇写的太草率了;
2.掌握了面向对象的思想在编程中真的很有用。
相关文章推荐
- Lua编程示例(二):面向对象、metatable对表进行扩展
- C#中面向对象编程机制之多态学习笔记
- 浅谈Lua的面向对象特性
- Lua面向对象之类和继承浅析
- JavaScript面向对象的两种书写方法以及差别
- 浅谈c# 面向对象之类与对象
- C#面向对象特征的具体实现及作用详解
- C# 面向对象的基本原则
- 浅谈对c# 面向对象的理解
- Ruby面向对象编程详解
- C#图片处理3种高级应用
- php学习 面向对象 课件第1/2页
- PHP程序61条面向对象分析设计的经验小结
- 收集学习asp.net比较完整的面向对象开发流程
- javascript 面向对象编程 万物皆对象
- 不错的JavaScript面向对象的简单入门介绍第1/2页
- [推荐]javascript 面向对象技术基础教程
- javascript 面向对象的JavaScript类
- JavaScript中的面向对象介绍
- javascript实现面向对象类的功能书写技巧