GD库常用实例
一、图片水印
1、实现步骤
- 打开原图(也叫操作的目标图片)
- 打开水印图(也叫水印来源图片)
- 使用 imagecopymerge 将小图合并至大图的指定位置
- 输出图片
- 销毁资源
2、实例代码
$dst_path = 'image/meinv.jpg';
$dst = imagecreatefromstring(file_get_contents($dst_path));
$logo_path = 'image/jack.jpg';
$logo = imagecreatefromstring(file_get_contents($logo_path));
list($dst_width,$dst_height) = getimagesize($dst_path);
list($logo_width,$logo_height) = getimagesize($logo_path);
$dst_x = $dst_width - $logo_width;
$dst_y = $dst_height - $logo_height;
//要将图片加在右下脚
imagecopymerge($dst, $logo, $dst_x, $dst_y, 0, 0, $logo_width, $logo_height, 50);
header('Content-type:image/png');
imagepng($dst);
imagedestroy($dst);
二、文字水印
/**
* imagecolorallocatealpha() 的行为和 imagecolorallocate() 相同,
* 但多了一个额外的透明度参数 alpha,其值从 0 到 127。0 表示完全不透明,127 表示完全透明。
*/
$filename = 'image/meinv.jpg';
$fileinfo = getimagesize($filename);
list( $width, $height ) = $fileinfo;
$mime = $fileinfo['mime'];
$createFrom = str_replace('/','createfrom',$mime);
$outFun = str_replace('/','',$mime);
$image = $createFrom($filename);
//设置字体
$font = 'C:\Windows\Fonts\msyh.ttf';//建议使用绝对路径
imagettftext($image, 30, 0, 0,ceil(($height-30)/2), $color, $font, 'Jack喜欢的妞');
header('content-type:'.$mime);
$outFun($image);
imagedestroy($image);
三、验证码
function createCheckCode( $codeNum ){ $code="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
$string='';
//从字符串中取出随机的字符
for($i=0; $i < $codeNum; $i++){
$char=$code{mt_rand(0, strlen($code)-1)};
$string.=$char;
}
//返回字符内容
return $string;
}
$width = 100;
$height = 30;
# 准备画布
$image = imagecreatetruecolor($width, $height);
# 准备颜色
$color_dan = imagecolorallocate($image, 200, 200, 200);
$color_shen = imagecolorallocate($image, 2, 2, 2);
# 填充背景颜色
imagefilledrectangle($image, 0, 0, $width, $height, $color_dan);
# 将字符写入画布
$str = createCheckCode(4);
for ($i = 0; $i < 4; $i++) {
$pj = 100/4;
$startx = $i*$pj + 10;
$starty = rand(0, 30-15);
imagechar($image, 7, $startx, $starty, $str{$i}, $color_shen);
}
for ($i = 0; $i < 100; $i++) {
imagesetpixel($image, mt_rand(0, 100), mt_rand(0, 30), $color_shen);
}
# 告诉浏览器你弄了一个啥
header('Content-Type:image/png');
# 以 PNG 格式将图像输出到浏览器或文件
imagepng($image);
# 销毁 节省内存
imagedestroy($image);
四、图片缩放
- 打开来源图片
- 设置图片缩放百分比(缩放)
- 获得来源图片,按比调整大小
- 新建一个指定大小的图片为目标图
- 将来源图调整后的大小放到目标中
- 销毁资源
$src_path = 'image/meinv.jpg';
$src_image = imagecreatefromstring(file_get_contents($src_path));
list( $src_w, $src_h ) = getimagesize($src_path);
$p = 0.3;
$new_width = $src_w * $p;
$new_height = $src_h * $p;
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $src_image, 0, 0, 0, 0, $new_width, $new_height, $src_w, $src_h);
header('content-type:image/jpeg');
imagejpeg($new_image);
五、验证码类
$char=array_merge(range('a','z'),range('A','Z'),range(1,9));
<?php
//验证码类
class ValidateCode {
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子
private $code;//验证码
private $codelen ;//验证码长度
private $width;//宽度
private $height;//高度
private $img;//图形资源句柄
private $font;//指定的字体格式
private $fontsize;//指定字体大小
private $fontcolor;//指定字体颜色
//构造方法初始化
public function __construct($codelen=4,$width=130,$height=50,$fontsize=20) {
//dirname()返回路径中的目录部分,__FILE__ 文件的完整路径和文件名
$this->codelen=$codelen;
$this->width=$width;
$this->height=$height;
$this->fontsize=$fontsize;
$this->font = dirname(__FILE__).'/simhei.ttf';//注意字体路径要写对,否则显示不了图片
}
//生成随机码
private function createCode() {
$_len = strlen($this->charset)-1;
for ($i=0;$i<$this->codelen;$i++) {
$this->code .= $this->charset[mt_rand(0,$_len)];
}
}
//生成背景
private function createBg() {
//新建一个真彩色图像
$this->img = imagecreatetruecolor($this->width, $this->height);
//为一幅图像分配颜色
$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
//画一矩形并填充
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
}
//生成文字
private function createFont() {
$_x = $this->width / $this->codelen;
for ($i=0;$i<$this->codelen;$i++) {
$this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontsize,mt_rand(-20,20),$_x*$i+mt_rand(10,20),mt_rand($this->fontsize+5,$this->height-5),$this->fontcolor,$this->font,$this->code[$i]);
}
}
//生成线条、雪花
private function createLine() {
//线条
for ($i=0;$i<6;$i++) {
$color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
//雪花
for ($i=0;$i<100;$i++) {
$color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}
//输出
private function outPut() {
header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
//对外生成
public function doimg() {
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
}
//获取验证码
public function getCode() {
return strtolower($this->code);
}
}
//用法
//$p=new ValidateCode(5,250,60,30);
//$p->doimg();
?>
来自为知笔记(Wiz)