GD库笔记

^.GD简介
PHP 不仅限于只产生 HTML 的输出,还可以创建及操作多种不同格式的图像文件。
PHP提供了一些内置的图像信息函数,也可以使用GD函数库创建新图像或处理已有的图像。
目前GD2库支持GIF、JPEG、PNG和WBMP等格式。此外还支持一些FreeType、Type1等字体库。
JPEG 是一种压缩标准的名字,通常是用来存储照片或者存储具有丰富色彩和色彩层次的图像。这种格式使用了有损压缩。
PNG 是可移植的网络图像,对图像采用了无损压缩标准。
GIF 原义是“图像互换格式”,是一种基于LZW算法的连续色调的无损压缩格式 。
使用前,先检查是否开启GD库这个模块
phpinfo();
没有GD库,去PHP.INI中查找
extension=php_gd2.dll

^.使用gd基础
画布管理
imagecreate() 创建基于调色板的画布
imagecreatetruecolor() 创建基于真彩的画布
imagedestroy() 销毁图像的资源

设置颜色
imagecolorallocate() 为图像分配颜色

生成图像 把图像输出到浏览器上或者图像另存为
header("Content-type:image/jpeg")
imagegif() 把图像输出或另存为gif格式
imagepng()  把图像输出或另存为png格式
imagejpeg() 把图像输出或另存为jpeg格式
如果两个参数 就是另存
如果一个参数 就是输出

^.4个步骤
//1.创建画布
$img = imagecreatetruecolor(300,300);

//2.绘制图像
//分配颜色
$blue = imagecolorallocate($img,0,0,255);
$red = imagecolorallocate($img,255,0,0);

//3.输出图像
header('content-type:image/jpg');
imagejpeg($img); //不另存为
imagejpeg($img,'./image/'.time().'.jpg'); //另存为

//4.销毁资源
imagedestroy($img);

^.绘制图像
// 填充颜色
imagefill($img,0,0,$blue);

// 画线
for($i=0; $i<10; $i++) {
imageline($img, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $white);
}

// 画像素点
for($i=0; $i<1000; $i++){
// 随机绘制像素点
imagesetpixel($img,mt_rand(0,400),mt_rand(0,400),$white);
}

// 矩形
imagerectangle();
imagefilledrectangle($img, 150, 150, 250, 250, $white);

// 多边形
$gon = array(
$width / 2, 0,
$width , $height /2 ,
$width / 2, $height,
0,$height / 2,
$width / 2, 0);
imagepolygon($img,$gon,5,$white);
imagefilledpolygon($img,$gon,5,$white);

//画圆
imageellipse($img, 150, 75, 50, 50, $red);
imagefilledellipse($img, 150,150, 100, 100, $red);

//画弧线
imagearc($img,150,150, 100,100, 0,360,$red);
imagefilledarc($img,150,150, 100,100, 0,-30,$red,0);

//绘制文字
imagestring() //一串字符x轴
imagestringup() //一串字符y轴
imagechar() //一个字符x轴
imagecharup() //一个字符y轴

//资源, 字体大小, 角度, 坐标x, 坐标y, 颜色, 字体文件, 输出的字符串
imagettftext($img,30,-45, 80,150,$white,'./MSYH.TTF','范德萨范德萨');

^.php图像处理(缩放、裁剪、水印、旋转和翻转)
基础
imagecreatefrompng($filename); //打开资源
imagecreatefromjpeg()
imagecreatefromgif()
getimagesize($filename); //获取图片信息,返回数组
imagesx($img); //图宽
imagesy($img); //图高

图片缩放和裁剪
imagecopyresampled() //拷贝部分图像并调整大小
imagecopyresized()

添加图片水印
imagecopy()
imagecopymerge()

图片旋转和翻转
imagerotate()

^.剪裁图像
//源文件
$filename = './image/1.jpg';

//新图的宽高
$width = 300;
$height = 300;

//新图
$imgdes = imagecreatetruecolor($width, $height);

//裁剪图
$imgsrc = imagecreatefromjpeg($filename);

//进行裁剪
//新图, 剪裁对象, 新图x,新图y, 剪裁图x,裁剪图y,新图w,新图h,裁剪图w,裁剪图h
imagecopyresampled($imgdes, $imgsrc, 0, 0, 300, 100, $width, $height, $width, $height);

//输出
header('Content-type:image/jpeg');
imagejpeg($imgdes);

//销毁资源
imagedestroy($imgdes);
imagedestroy($imgsrc);

^.水印文字
//源文件
$filename = './image/1.jpg';
$img = imagecreatefromjpeg($filename);

//字体颜色
$color = imagecolorallocate($img,255,255,0);

//加上文字
imagettftext($img, 30, 0, 250, 500, $color, './STHUPO.TTF','梦中情人');

header('Content-Type:image/jpeg');
imagejpeg($img);

imagedestroy($img);

^.失真图片缩放
$filename = './image/1.jpg';
$img = imagecreatefromjpeg($filename);
$width = 300;
$height = 300;
$bg = imagecreatetruecolor($width, $height);

//进行失真缩放
//画布, 美女图, 画布x,画布y,美女图x,美女图y,画布w,画布h,美女图w,美女图h
imagecopyresampled($bg,$img,0,0,0,0,$width,$height,imagesx($img),imagesy($img));

header('Content-Type:image/jpeg');
imagejpeg($bg);

imagedestroy($bg);
imagedestroy($img);

^.无透明水印图片
$filename = './image/1.jpg';
$bgpic = imagecreatefromjpeg($filename);

//引入水印图片
$water = imagecreatefrompng('./image/logo.png');

//进行水印覆盖
//美女图,水印图,水印图x,水印图y,美女图x,美女图y,水印图w,水印图h
imagecopy($bgpic,$water,300,100,0,0,imagesx($water),imagesy($water));

header('Content-Type:image/jpeg');
imagejpeg($bgpic);
imagedestroy($bgpic);
imagedestroy($water);

^.透明水印
$filename = './image/1.jpg';
$bgpic = imagecreatefromjpeg($filename);

//引入水印图片
$water = imagecreatefromjpeg('./image/taiji.jpg');

//进行透明水印覆盖
//美女图,水印图,水印图x,水印图y,美女图x,美女图y,水印图w,水印图h,透明度
imagecopymerge($bgpic,$water,200,400,0,0,imagesx($water),imagesy($water),50);

header('Content-Type:image/jpeg');
imagejpeg($bgpic);
imagedestroy($bgpic);
imagedestroy($water);

^.旋转图片
$filename = './image/1.jpg';
$img = imagecreatefromjpeg($filename);

$color = imagecolorallocate($img,255,255,0);

//画布,角度,颜色
$img = imagerotate($img,-30,$color); //返回资源

header('Content-Type:image/jpeg');
imagejpeg($img);
imagedestroy($img);

^.等比例缩放
$filename = './image/1.jpg';
$img = imagecreatefromjpeg($filename);

$width = imagesx($img);
$height = imagesy($img);
$size = 300;

if($width > $height){
$bw = $size;
$bh = ($height / $width) * $bw;
}else{
$bh = $size;
$bw = ($width / $height) * $bh;
}

$bg = imagecreatetruecolor($bw,$bh);

imagecopyresampled($bg,$img,0,0,0,0,$bw,$bh,$width,$height);

header('Content-Type:image/jpeg');
imagejpeg($bg);

imagedestroy($bg);
imagedestroy($img);

posted on 2016-06-05 14:44  方休  阅读(208)  评论(0编辑  收藏  举报

导航