php中gd库的使用
在PHP中创建一个图像应该完成如下所示的4个步骤:
1.创建一个背景图像(也叫画布),以后的操作都基于此背景图像。
2.在背景上绘制图像轮廓或输入文本。
3.输出最终图形
4.释放资源
一.创建画布
imagecreate – 新建一个基于调色板的图像
imagecreatetruecolor – 新建一个真彩色图像
1.用imagecreatetruecolor()创建图像(jpeg,png等,不支持gif)时,图片默认背景颜色是黑色,要想改变背景色,必须使用别的指令如 imagefill() 填充背景。步骤如下:
1.创建真彩图片
2.分配颜色
3.填充颜色。
header("Content-type:image/png");
$image=imagecreatetruecolor(120,20) or die(" can not");
$background_color=imagecolorallocate($image,0,255,0);
imagefill($image,0,0,$background_color);
$text_color=imagecolorallocate($image,255,0,0);
imagestring($image,1,5,5,"a simple text string",$text_color);
imagepng($image);
imagedestroy($image);
2.用Imagecreate()创建图像时,默认是空白,只要第一次分配颜色,所分配的颜色就是背景色了。
header ('Content-Type: image/png');
$im = imagecreate(120, 20) or die('Cannot Initialize new GD image stream');
$background=imagecolorallocate($im,255,0,255);
$text_color = imagecolorallocate($im, 255, 0, 0);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);
二.设置颜色
imagecolorallocate
$image = imagecreatetruecolor(100,100);//创建画布
$background_color=imagecolorallocate($image,255,255,255); //设置画布颜色
imagefill($image,0,0,$background_color); //给画布填充颜色
$red=imagecolorallocate($image,250,0,0);//设置线颜色
imageline($image,0,0,100,100,$red);//画线
header("Content-type:image/png");
imagepng($image);
imagedestroy($image);
三.生成图片
imagegif – 以 GIF 格式将图像输出到浏览器或文件
imagejpeg – 以 JPEG 格式将图像输出到浏览器或文件
imagepng – 以 PNG 格式将图像输出到浏览器或文件
imagewbmp – 以 WBMP 格式将图像输出到浏览器或文件
四.新建一张图片
imagecreatefrompng ( string filename ) –从 PNG 文件或 URL 新建一图像
imagecreatefromjpeg ( string filename )–从 JPEG 文件或 URL 新建一图像
imagecreatefromgif ( string filename ) –从 GIF 文件或 URL 新建一图像
imagecreatefromwbmp ( string filename )– 从 WBMP 文件或 URL 新建一图像
imagecreatefromstring():创建一块画布,并从字符串中的图像流新建一副图像
$img=imagecreatefrompng("./cx.png");
$color=imagecolorallocate($img,0,255,0);
imageline($img,0,0,200,200,$color);
imagepng($img,"ncx.png");
imagedestroy($img)
五.销毁图片
imagedestroy – 销毁一图像
六.图片缩放和裁剪
imagecopyresampled ( resource
dstimage,resource
d
s
t
i
m
a
g
e
,
r
e
s
o
u
r
c
e
src_image ,
dstx,
d
s
t
x
,
dst_y ,
srcx,
s
r
c
x
,
src_y ,
dstw,
d
s
t
w
,
dst_h ,
srcw,
s
r
c
w
,
src_h ) 成功返回true 失败返回false
参数:
dst_image目标图象连接资源
src_image源图象连接资源
dst_x目标 X 坐标点
dst_y目标 Y 坐标点
src_x源的 X 坐标点
src_y源的 Y 坐标点
dst_w目标宽度
dst_h目标高度
src_w源图象的宽度
src_h源图象的高度
/**
* 等比例切割图片
* @param $uid 用户id
* @param $target_width 切割后图片宽度
* @param $target_height 切割后图片高度
* @param $src_path 原图路径
* @return string
*/
public function croppedImg($uid,$target_width,$target_height,$src_path)
{
if(!$src_path){
return false;
}
$imgSize= @getimagesize($src_path);
$with = $imgSize[0];
$height = $imgSize[1];
$type = explode('/',$imgSize['mime']);
$type = $type[1];
$ratio = $with/$height;
//最大宽或最大高
if ($target_width / $target_height > $ratio){
$target_width = $target_height * $ratio;
}else{
$target_height = $target_width / $ratio;
}
switch ($type){
case 'gif':
$source_image = imagecreatefromgif($src_path);
break;
case 'jpeg':
$source_image = imagecreatefromjpeg($src_path);
break;
case 'png':
$source_image = imagecreatefrompng($src_path);
break;
default:
return false;
break;
}
$newPath = 'upload/';
$filename = $uid.'_'.date('YmdHis').".". $type;
$sliceBanner = $newPath.$filename;
if(!is_dir($newPath)){
mkdir($newPath, 777); // 使用最大权限0777创建文件
}
$target_image = imagecreatetruecolor($target_width,$target_height);
@imagecopyresampled($target_image,$source_image,0,0,0,0,$target_width,$target_height,$with,$height);
imagejpeg($target_image,$sliceBanner);
@imagedestroy($target_image);
@imagedestroy($source_image);
//删除已上传未裁切的图片
if(file_exists($src_path)) {
unlink($src_path);
}
return $sliceBanner;
}
七.给图片上水印
imagecopymerge (
dstim,
d
s
t
i
m
,
src_im ,
dstx,
d
s
t
x
,
dst_y ,
srcx,
s
r
c
x
,
src_y ,
srcw,
s
r
c
w
,
src_h , $pct )
参数:
dst_im源图
src_im水印图
dst_x源图起始x坐标
dst_y源图起始y坐标
src_x水印图起始坐标x
src_y水印图起始坐标y
src_w水印图宽
src_h水印图高
pct水印图透明度
public function imageWater($water_path,$src_path,$uid){
if(!$water_path || !$src_path){
return false;
}
//创建图片的实例
$dst = imagecreatefromstring(file_get_contents($src_path));
$src = imagecreatefromstring(file_get_contents($water_path));
//获取水印图片的宽高
list($water_w,$water_h) = getimagesize($water_path);
//将水印图片复制到目标图片上,最后个参数50是设置透明度
imagecopymerge($dst,$src,0,0,0,0,$water_w,$water_h, 50);
//获取原图类型
$imageSize = getimagesize($src_path);
$mime = explode('/',$imageSize['mime']);
$type = $mime[1];
$newPath = 'upload/';
$filename = $uid.'_'.date('YmdHis').".". 'png';
$sliceBanner = $newPath.$filename;
switch ($type) {
case 'gif':
imagegif($dst,$sliceBanner);
break;
case 'jpg':
imagejpeg($dst,$sliceBanner);
break;
case 'png':
imagepng($dst,$sliceBanner);
break;
default:
break;
}
imagedestroy($dst);
imagedestroy($src);
return $sliceBanner;
}