码迷,mamicode.com
首页 > Web开发 > 详细

php操作图像(GD函数库)

时间:2015-01-22 15:22:48      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

PHP.ini里修改;extension=php_gd2.dll

在PHP中创建一个图像应该完成这样四个步骤:

•创建背景图像
•在背景上绘制图形或输入文本
•输出最终图形
•清除所有资源

<?php

$width = 200; // 图像宽度

$height = 400; // 图像高度

$img = ImageCreateTrueColor( $width, $height );

?> 

通过ImageCreateFromPNG()、ImageCreateJPEG()或ImageCreateFromGIF()来读取一个现有图像文件,然后对其进行过滤,再在它上面添加其他图像。 

<?php

$imgTemp = ImageCreateFromPNG(‘abc.png‘);

?> 

<?php

$width = 200; // 图像宽度

$height = 400; // 图像高度

$img = ImageCreateTrueColor( $width, $height );

$white = ImageColorAllocate($img, 255, 255, 255); //RGB值

$blue = ImageColorAllocate($img, 0, 0, 64);

ImageFill($img, 0, 0, $blue);  //往背景图里填充$blue

ImageLine($img, 0, 0, $width, $height, $white); /*这里出现了2对坐标,“0, 0”是起始点的坐标,而$width, $height是终点的坐标,最后一个参数就是颜色。*/

ImageString($img, 3, 100, 150, ‘abc‘, $white);//图像上书写字符串

?> 

<?php

$width = 200; // 图像宽度

$height = 400; // 图像高度

$img = ImageCreateTrueColor( $width, $height );

$white = ImageColorAllocate($img, 255, 255, 255); //RGB值

$blue = ImageColorAllocate($img, 0, 0, 64);

ImageFill($img, 0, 0, $blue);  //往背景图里填充$blue

ImageLine($img, 0, 0, $width, $height, $white); /*这里出现了2对坐标,“0, 0”是起始点的坐标,而$width, $height是终点的坐标,最后一个参数就是颜色。*/

ImageString($img, 3, 100, 150, ‘abc’, $white);//图像上书写字符串

header( ‘Content-type: image/png‘ ); //指定图像的MIME类型

imagepng($img);  //输出图像数据

?> 

同样的,如果你希望最终输出JPEG图像,可以使用ImageJPEG()来输出,同时还要将header()函数参数同步替换成image/jpeg。

  综上所述,我们是将图像直接发送到浏览器,如果我们希望将自动创建的图像生成文件,可以将在imagepng()函数中加上第二个参数,这个参数就是你希望生成的文件名,如logo.png。注意,文件后缀名要保持格式一直,而且文件名要用引号引起来。

-------------------------

释放并销毁资源

<?php

$width = 200; // 图像宽度

$height = 400; // 图像高度

$img = ImageCreateTrueColor( $width, $height );

$white = ImageColorAllocate($img, 255, 255, 255); //RGB值

$blue = ImageColorAllocate($img, 0, 0, 64);

ImageFill($img, 0, 0, $blue);  //往背景图里填充$blue

ImageLine($img, 0, 0, $width, $height, $white); /*这里出现了2对坐标,“0, 0”是起始点的坐标,而$width, $height是终点的坐标,最后一个参数就是颜色。*/

ImageString($img, 3, 100, 150, ‘abc’, $white);//图像上书写字符串

header( ‘Content-type: image/png‘ ); //指定图像的MIME类型

imagepng($img);  //输出图像数据

ImageDestroy($img); //释放并销毁

?> 

调用自动生成的图像

<img src="image.php" height="400" width="200" /> 

总结:

创建背景图像:ImageCreateTrueColor()

 在背景上绘制图形或输入文本:ImageColorAllocate()、ImageFill()、ImageLine()、ImageString()

输出最终图形:header()、 imagepng()

清楚所有资源:imagedestroy()

php操作图像(GD函数库)

标签:

原文地址:http://www.cnblogs.com/csharphuang/p/4241546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!