PHP基于GD库的图像处理方法小结

yipeiwu_com6年前PHP代码库

本文实例讲述了PHP基于GD库的图像处理方法。分享给大家供大家参考,具体如下:

gd图像处理技术

extension=php_gd2.dll

创建画布

画布,一种资源型数据,可操作的图像资源

创建画布(新建)

imageCreate(width,height) //创建基于调色板的画布

imageCreateTrueColor(width,height) //创建真彩色的画布

基于图片创建画布(打开)

imageCreateFromJPEG( url)
imageCreateFromPNG(url)
imageCreateFromGIF(url)

操作画布

分配颜色:如果需要在画布上使用某种颜色,应该先将颜色分配到画布上。

(颜色标识 )= imageColorAllocate(img,r,g,b)

填充画布

imageFill(img,x,y,颜色标识)

输出画布

1. 输出到图片文件

2. 直接输出,需要告知浏览器输出为图片信息(header("Content-type:image/png;")

imagePNG(img[,url])
imageJPEG()
imageGIF()

销毁画布资源

imageDestroy(img)

<?php
header('content-type:image/png');
$img = imagecreate(300,300);
$color = imagecolorallocate($img,223,22,44);
imagefill($img,3,3,$color);
imagepng($img);
imagedestroy();
?>

运行效果图如下:

验证码实现

<?php
  header('content-type:image/png');
  $code = '123456789abcdefghijklmnpqrstuvwxvz';
  $length = strlen($code);
  $print = '';
  for($i=0; $i<4; $i++){
    $print.=$code[mt_rand(0,$length-1)];
  }
//  echo $print;
  $img = imagecreatefrompng('./str.png');
  $color = mt_rand(0,1)==1?imagecolorallocate($img,0,0,0):imagecolorallocate($img,255,255,255);
  //图片大小
  $img_width = imagesx($img);
  $img_height = imagesy($img);
  //字体大小
  $font = 5;
  $font_width = imagefontwidth($font);
  $font_height = imagefontheight($font);
  $fin_w = ($img_width-$font_width*4)/2;
  $fin_h = ($img_height-$font_height)/2;
  imagestring($img,$font,$fin_w,$fin_h,$print,$color);
  imagepng($img);
  imagedestroy($img);
?>
<image src="gd_string.php" onclick="this.src='gd_string.php?ra='+Math.random()"></image>

运行效果图如下:

 

<?php
session_start();
$im=imagecreatetruecolor(80,30);
$str="";
for ($i=0;$i<4;$i++){
  $str.=dechex(rand(0,15));
}
$_SESSION['code']=$str;
$white=imagecolorallocate($im,255,255,255);
imagestring($im,rand(2,5),rand(0,70),rand(0,10),$str,$white);
//imagettftext($im,rand(0,5),rand(0,180),rand(0,100),rand(0,10),$white,"simhei.ttf",$str);
for($i=0;$i<20;$i++){
$color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imageline($im,rand(0,90),rand(0,20),rand(0,100),rand(0,100),$color);
}
header("content-type:image/png");
imagepng($im);
imagedestroy($im);
?>

注意:图片输出前后不能有额外输出

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

PHP 数组学习排序全接触第1/2页

array_values($arr) array 返回数组所有元素 复制代码 代码如下: <?php $arr = array("a", "b", "c"); $arr = arr...

php短域名转换为实际域名函数

复制代码 代码如下: $url = "http://sinaurl.cn/hbdsU5"; echo unshorten($url); function unshorten($url)...

php 生成WML页面方法详解

由于静态的WAP页面在很多方面不能满足用户个性化的服务请求,因此通过WAP服务器端语言产生动态的WML页面,具有很广泛的应用价值和很高的商业价值。   WAP应用结构非常类似于Inter...

使用GD库生成带阴影文字的图片

使用GD库生成带阴影文字的图片

最近使用GD库来进行微信公共账号的图片生成,研究了一下GD库文字阴影效果的生成同时也发现了GD库的强大。 GD库,是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用G...

php简单实现短网址(短链)还原的方法(测试可用)

php简单实现短网址(短链)还原的方法(测试可用)

本文实例讲述了php简单实现短网址还原的方法。分享给大家供大家参考,具体如下: 这里以【宜配屋www.yipeiwu.com】//www.jb51.net的短网址http://t.cn/...