PHP->利用GD库新建图像
1.确认php中GD库是否开启
在PHP配置文件php.ini中查找extension=php_gd2.dll,去掉前边的(分号) ';' 即可,一般php是默认开启的

2.绘画步骤
- 创建一个画布(画板)、画笔、色彩。
- *开始绘画(重点,难点)
- 输出图像(复制型)
- 销毁图像资源(释放内存)
3.示例
为了方便演示,先了解后两步
输出图像:
header("Content-Type: image/jpeg");//设置响应头信息为一个jpeg的图片
imagejpeg($im);//输出一个jpeg图片
header("Content-Type: image/png");//设置响应头信息为一个png的图片
imagepng($im);//输出一个png图片
7 //输出到指定文件中(另存为)
imagepng($im,"**.png");
销毁图像(解除占用的资源):
bool imagedestroy ( resource $image )//销毁一图像
创建一个画布:
//创建一个基于256色的画布
$img=imagecreate(400,400);
echo $img;
显示效果:
//新建一个真彩色图像
$img=imagecreatetruecolor(400,400);
echo $img;
显示效果:

其他的方式:
//还有基于某个图片的画布
imagecreatefromgif( string filename )
imagecreatefrompng( string filename )
imagecreatefromjpeg( string filename )
真彩和256色画布的显示区别-----真彩的会默认填充黑色的画布
<?php
//创建一个基于256色的画布
$img1=imagecreate(400,400);
$img2=imagecreatetruecolor(400,400);
// 输出图像
header('content-type:image/png');
// imagepng($img1);
imagepng($img2);
// 销毁图像,解除占用的资源
// imagedestroy($img1);
imagedestroy($img2);
?>
显示效果:

*开始绘画
<?php
//分配定义颜色
$red = imagecolorallocate($im,255,0,0); //分配一个颜色 //填充背景
bool imagefill(resource image,int x,int y, int color ); //填充背景 //画点
bool imagesetpixel(resource image,int x,int y,int color );//画一个像素点 //画一个线段的函数
bool imageline ( resource image, int x1, int y1, int x2, int y2, int color ) //画矩形框(不填充)
bool imagerectangle ( resource image, int x1, int y1, int x2, int y2, int color )
//画矩形框(填充)
bool imagefilledrectangle ( resource image, int x1, int y1, int x2, int y2, int color ) //绘制多边形
bool imagepolygon ( resource image, array points, int num_points, int color )
bool imagefilledpolygon ( resource image, array points, int num_points, int color ) //绘制椭圆(正圆)
imageellipse ( resource image, int cx, int cy, int w, int h, int color )
imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color ) //绘制圆弧(可填充)
imagearc ( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )
imagefilledarc ( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style ) //绘制字串
bool imagestring ( resource image, int font, int x, int y, string s, int col )
bool imagestringup ( resource image, int font, int x, int y, string s, int col ) //绘制字符
imagechar //绘制文本:
*array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text ) //当上面的字体出现乱码时,使用下面的函数转换编码
string iconv ( string in_charset, string out_charset, string str ) $name="张三";
$str = iconv("ISO8859-1","UTF-8",$name);
$str = iconv("GBK","UTF-8",$name);
$str = iconv("GB2312","UTF-8",$name); //图片的裁剪、合成、缩放
**bool imagecopyresampled ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h ) * imagesx — 取得图像宽度
* imagesy — 取得图像高度
* array getimagesize ( string $filename [, array &$imageinfo ] ) 取得图像大小
?>
综合代码示例:
<?php
// 创建画布
$img=imagecreatetruecolor(1000,1000);
// 创建颜色
$red=imagecolorallocate($img,255,0,0);
$blue=imagecolorallocate($img, 0, 0, 255);
$green=imagecolorallocate($img, 0, 255, 0);
$gray=imagecolorallocate($img, 200, 200, 200); //填充颜色
imagefill($img,100,0,$gray);
//创建一个点
imagesetpixel($img, 20, 20, $red);
// 随机创建1000个点
for($i=0;$i<1000;$i++){
imagesetpixel($img, rand(0,1000), rand(0,1000), $red);
}
//输出线段
imageline($img, 0, 0, 200, 200, $blue);
//输出矩形
imagerectangle($img, 200, 200, 400, 400, $blue);//不填充
imagefilledrectangle($img, 200, 400, 400, 600, $blue);//填充
//绘制多边形 imagefilledpolygon----填充
imagepolygon($img, array(0,0,100,200,300,200), 3, $blue);
//绘制椭圆(正圆)
imageellipse($img, 600, 600, 200, 200, $blue);
imagefilledellipse($img, 800, 600, 200, 200, $blue);
// 绘制字符串
imagestring($img, 15, 600, 200, "string", $blue);//水平
imagestringup($img, 15, 600, 200, "string", $blue);//垂直
// 绘制字符
imagechar($img, 5, 800, 200, 'H', $blue);
imagecharup($img, 5, 800, 200, 'H', $blue);
//绘制文本
imagettftext($img, 20, 0, 500, 500, $blue, 'bb.ttc', 'this is string!');
/*当上面的字体出现乱码时,使用下面的函数转换编码
string iconv ( string in_charset, string out_charset, string str )*/
//imagecopyresampled($img, "dd.jpg", 200, 200, 200, 200, 200, 200, 200, 200);
//输出图像
header('content-type:image/png');
imagepng($img);
//销毁图像
imagedestroy($img);
?>
难重点:
PHP->利用GD库新建图像的更多相关文章
- php学习笔记:利用gd库生成图片,并实现随机验证码
说明:一些基本的代码我都进行了注释,这里实现的验证码位数.需要用的字符串都可以再设置.有我的注释,大家应该很容易能看得懂. 基本思路: 1.用mt_rand()随机生成数字确定需要获取的字符串,对字符 ...
- PHP 使用GD 库绘制图像,无法显示的问题
根据官方GD 库绘制图像文档样式 原基本样式 $width = 120; $height = 50; $img = @imagecreatetruecolor($width, $height) or ...
- 【PHP】使用GD库实现 图像生成、缩放、logo水印和简单验证码
gd库是php最常用的图片处理库之一(另外一个是imagemagick),可以生成图片.验证码.水印.缩略图等等.要使用gd库首先需要开启gd库扩展, windows系统下需要在php.ini中将ex ...
- GD库处理图像
在PHP5中,动态图象的处理要比以前容易得多.PHP5在php.ini文件中包含了GD扩展包,只需去掉GD扩展包的相应注释就可以正常使用了.PHP5包含的GD库正是升级的GD2库,其中包含支持真彩图像 ...
- PHP利用GD库绘图和生成验证码图片
首先得确定php.ini设置有没有打开GD扩展功能,測试例如以下 print_r(gd_info()); 假设有打印出内容例如以下,则说明GD功能有打开: Array ( [GD Version] = ...
- 利用pil库处理图像
1关于PIL PIL(Python Image Library)是python的第三方图像处理库,但是由于其强大的功能与众多的使用人数,几乎已经被认为是python官方图像处理库了. 2PIL的主要功 ...
- PHP学习笔记:利用gd库给图片打图片水印
<?php $dst_path = '1.jpg';//目标图片 $src_path = 'logo1.png';//水印图片 //创建图片的实例 $dst = imagecreatefroms ...
- PHP利用GD库画曲线
效果: PHP代码 <?php Header('Content-type: image/png;Charset:utf-8'); //声明图片 $im = imagecreate(400,200 ...
- php 利用Gd库添加文字水印乱码的问题及解决方案
最近一个项目进行了服务器迁移,部署后发现 ,其中一个为图片添加水印文字的功能出现了乱码问题,确认功能代码不存在问题,同时项目代码都是使用UTF-8编码,不存在编码问题,也检查排除了字体文件出现问题的可 ...
随机推荐
- js常用函数陆续总结
1.each() 方法规定为每个匹配元素规定运行的函数. $.each(data,function(index,item){ sb.append(item.answerNum); } $(" ...
- 封装原生Ajax
var Chef = { createAjax:function() { var xhr = null; try { //IE系列浏览器 xhr = new ActiveXObject("m ...
- 简单验证码识别(matlab)
简单验证码识别(matlab) 验证码识别, matlab 昨天晚上一个朋友给我发了一些验证码的图片,希望能有一个自动识别的程序. 1474529971027.jpg 我看了看这些样本,发现都是很规则 ...
- Eclipse Java注释模板设置详解,更改 ${user}和${date}
修改MyEclipse eclipse 注释的作者名字 转自:http://www.oschina.net/question/158170_31311 在eclipse/myeclipse中,当我们去 ...
- 20145304 刘钦令 Java程序设计第一周学习总结
20145304<Java程序设计>第1周学习总结 教材学习内容总结 1995年5月23日,是公认的Java的诞生日,Java正式由Oak改名为Java. Java的三大平台是:Java ...
- 【POJ】2891 Strange Way to Express Integers
http://poj.org/problem?id=2891 题意:求最小的$x$使得$x \equiv r_i \pmod{ a_i }$. #include <cstdio> #inc ...
- This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.
一,经历 <1> 使用SDWebImage下载 成功图片后,将图片设置给 self.imageView.image,提示如题所示的错误提示. <2>第一反应就是慢慢注释掉代码进 ...
- nmap的script参数列表
在新的nmap版本中,添加了script功能的使用.在nmap的安装目录的share/nmap/scripts中,已经有将61个写好的脚本提供. 具体的用法可以参考:http://nmap.org/b ...
- python 获取进程pid号
#-*- encoding:UTF-8 -*- import os import sys import string import psutil import re def get_pid(name) ...
- java 反取字符串
public class demo2 { /** * 2 : 将字符串反取出来 新中国好 好国中新 */ public static void main(String[] args) { String ...