您的位置:首页 > 其它

GD库函数学习【笔记】

2015-06-25 19:43 204 查看
<?php

// 知识点

1. header("content-type:image/jpeg");                                       //表明请求页面的内容是jpeg格式的图像。
2. $img = imagecreatetruecolor(200,200)                                        //新建一个真彩色图像返回resourse
imagecreate(width,height);                                            //创建图片    默认用第一种
3. $img = imagecreatefromjpeg("完整路径");                                         //图片载入
4. $size = getimagesize("完整路径");                                        //获得图片的大小,以及信息,返回一个【数组】
5. imagejpeg($img[,图片质量(1~100)]);                                       //图片输出到浏览器,以指定格式jpeg,gif,png,bmp...
6. imagedestroy($img);                                                     //销毁图片

7. $color = imagecolorallocate($img,x,y,z) ;                                //为一幅图分配颜色,返回的是RBG格式颜色
8. imagefill ($img, x ,y, RBGcolor)                                         //区域填充,背景颜色,x,y坐标相邻的颜色【一样的】一起填充

9. imagettftext ($img,font-size,jiaodu,x,y,RGBcolor,"font-path","$text");   // 绘字函数 用truetype字体输入文本字体
imagestring($img,font ,x, y,"content",RBGcolor);                        //绘字函数 ,水平的划一行字符串,

10. imageline($img,x1, y1 ,x2, y2, $color);                                  //画线函数,从(x1,y1)-->(x2,y2)话一条线
11. imagesetpixel($img , x ,y ,$color);                                      //画点函数

12. imagecopy($img1[底],$img2[表图],x1 ,y1, x2, y2, 2_width, 2_height)   ;
//【截取图片水印】表层按一定坐标高宽,截取到底层图片上
//(x1,y1)控制img2在img1的位置,
//(x2,y2)控制img2开始截取的坐标
13. imagecopyresampled($img1[底],$img2[表图],x1 ,y1, x2, y2, 2_width, 2_height, 1_width, 1_height);
//【缩略图片水印】表层图片按一定坐标高宽,缩略到底层图片上,并且可以调整底层图片的高宽
//(x1,y1)控制img2在img1的位置,
//(x2,y2)控制img2开始缩略的坐标
//imagejpeg();
imagecopyresized();                              //按百分比缩放,默认用第一种*/

两种create图片的函数:$img=imagecreatetruecolor();				$img=imagecreatefromjpeg();
读取信息函数       getimagesize($img);
输出函数             imagejpeg($img);
销毁函数            imagedestroy($img);
分配颜色函数      $color=imagecolorallocate($img,x,y,z)
填充颜色函数      imagefill($img,x,y,$color);
绘符函数            imageline($img,x1,y1,x2,y2,$color);          imagesetpixel($img,x,y,$color);
!绘字函数           imagettftext($img,font-size,angle,x,y,$color,"ttf_path","$text");
!绘图函数           imagecopy();            imagecopyresampled();

// 举例应用
// header("content-type:image/jpeg");

// $img = imagecreatetruecolor(200,200);                       //新建一个真彩图,返回resource
// $img = imagecreatefronjpeg("1.jpg");

//  destroy ($img);                                                //销毁一个图片

// $bgcolor = imagecolorallocate($img,205,100,200);              //分配一个RGB颜色
// $color2 = imagecolorallocate($img,000,100,200);
// $color3 = imagecolorallocate($img,100,100,200);
// imagefill($img,100,100,$bgcolor);                            //填充颜色,

// imageline($img,10,10,50,50,$color2);                         //绘符函数,画线
// imagesetpixel($img,199,190,$color2);                         //绘符函数,画点

// imagestring($img,5,50,100,"content",200);                   //绘字函数,水平化一行字,
// imagettftext($img,50,10,50,80,$color3,"simhei.ttf","hehe");  //绘字函数,按一定参数写入文本,utf8

// echo imagejpeg($img);

// $a =getimagesize("1.jpg");
// print_r($a);

/*// 12
$img1 = imagecreatefromjpeg("1.jpg");
$img2 = imagecreatefromjpeg("2.jpg");
imagecopy($img1,$img2,10,10,100,150,200,200);
header("content-type:image/jpeg");
imagejpeg($img1);
*/
// 13.
/*  $img1 = imagecreatefromjpeg("1.jpg");
$img2 = imagecreatefromjpeg("2.jpg");
// $img2 = imagecreate(300,100);
imagecopyresampled($img1,$img2,10,10,100,50,500,100,1024,768);
header("content-type:image/jpeg");
imagejpeg($img1);*/

// 验证码
$str = "abcdefghijklmn123467890";
$rand ="";
for($i=0;$i<4;$i++){
$rand .=$str[rand(0,strlen("$str")-1)];
}
// echo $rand;exit;
header("content-type:image/jpeg");
$code = imagecreatetruecolor(120,40);
$code_color = imagecolorallocate($code, 110, 110, 110);
$bg_color = imagecolorallocate($code,255, 255, 255);
imagefill($code,1,1,$bg_color);
imagettftext($code,12,10,10,20,$code_color,"simhei.ttf",$rand); //绘字函数,按一定参数写入文本,utf8
// imagettftext($code,20,20,3,3,$code_color,"simhei.ttf","haha");
imagejpeg($code);
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: