gd库提供一套处理图片的api,默认安装php没有加载gd库,在php.ini里面打开
把上面的; 去掉,保存,重启Apache服务即可(ps 这是window下的)
图像信息:
imagesx(resource $image) 获取图片宽度
imagesy(resource $image) 获取图片高度
新建/载入图像:
imagecreate(int $x_size, int $y_size) 创建一个基于调色板的图像资源(2个参数分别是 宽度 高度)
imagecreatefromjepg(string $filename) 从 JPEG 文件或 URL 新建一图像 于此类似的还有
imagecreatefromgd2 — 从 GD2 文件或 URL 新建一图像
imagecreatefromgd2part — 从给定的 GD2 文件或 URL 中的部分新建一图像
imagecreatefromgd — 从 GD 文件或 URL 新建一图像
imagecreatefromgif — 从 GIF 文件或 URL 新建一图像
imagecreatefrompng — 从 PNG 文件或 URL 新建一图像
imagecreatefromstring — 从字符串中的图像流新建一图像
imagecreatefromwbmp — 从 WBMP 文件或 URL 新建一图像
imagecreatefromxbm — 从 XBM 文件或 URL 新建一图像
imagecreatefromxpm — 从 XPM 文件或 URL 新建一图像
imagecreatetruecolor — 新建一个真彩色图像
新建/载入图像:
imagegif(resource $image[,string $filename]) 参数可是一个图片资源,也可以是以个url 通过header函数发送Content-type: image/gif 可以直接向浏览器输出图片。于此类似的还有
image2wbmp 以 WBMP 格式将图像输出到浏览器或文件
imagegd2 将 GD2 图像输出到浏览器或文件
imagegd 将 GD 图像输出到浏览器或文件
imagewbmp 以 WBMP 格式将图像输出到浏览器或文件
imagexbm 将 XBM 图像输出到浏览器或文件
imagejpeg 以 JPEG 格式将图像输出到浏览器或文件
imagepng 以 PNG 格式将图像输出到浏览器或文件
一个简单的例子
$filename = "1.jpg";
$img = imagecreatefromjpeg($filename); //将一个图片路径 创建该图片
echo imagesx($im)."<br>"; //输出改图片宽度
echo imagesy($im)."<br>"; //输出改图片高度
生成一个图片 并返回到浏览器
html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<div style="width:960px; margin:10px auto;">
<img src="createimg.php">
</div>
</body>
</html>
createimg.php
header("Content-type:image/png"); //发送一个响应http头 告诉浏览器返回的是图片
$im = imagecreate(150,30);
$bg = imagecolorallocate($im, 0, 0, 0); //第一次调用 imagecolorallocate会给调色板背景设置
$textcolor = imagecolorallocate($im, 255, 0, 0);
imagestring($im, 10,10, 6, "Hello world!",$textcolor); //在新建的图片上画出一个文字 $textcolor字体设置成红色
imagepng($im);
常用的吧 哎
imagecolorallocate ( resource image, int red, int green, int blue )返回一个标识符,代表了由给定的 RGB 成分组成的颜色。image 参数是 imagecreatetruecolor() 函数的返回值。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。imagecolorallocate() 必须被调用以创建每一种用在 image 所代表的图像中的颜色。 注: 第一次对 imagecolorallocate() 的调用会填充背景色。
imagesetpixel ( resource image, int x, int y, int color )在 image 图像中用 color 颜色在 x, y 坐标(图像左上角为 0, 0)上画一个点。
imageline ( resource image, int x1, int y1, int x2, int y2, int color )用 color 颜色从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条直线。
imagestring ( resource image, int font, int x, int y, string s, int col )用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。
imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text ) 这个可以设置字体的大小哟
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 )
一个验证码的简单例子
用的 imagettftext 因为可以设置字体大小 还有字体的形式
验证码主要就是session 把值赋给$_SESSION["code"] = $allNum; 然后提交的时候去验证就行了
2个文件 一个是1.php 一个是img.php
1.php
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<style type="text/css">
img{ border:1px solid #CCC;}
</style>
<body>
<?php
session_start();
if(!empty($_POST)){
$code = $_POST["code"];
if($_SESSION["code"] == $code ){
echo "ok";
}else{
echo "fail";
}
echo "<br><a href=\"1.php\">返回</a>";
exit;
}
?>
<div style="width:960px; margin:10px auto;">
<img id="c" src="img.php"><br>
<a href="javascript:;" onClick="change()">换一张</a>
<form action="" method="post">
<input type="text" name="code"/>
<br>
<input type="submit" />
</form>
</div>
<script type="text/javascript">
window.onload = function(){
var img = document.getElementById("c"),
uuid = 1;
this.change = function(){
img.src="img.php?"+(++uuid);
}
}
</script>
</body>
</html>
img.php
session_start();
header("Content-type:image/png");
$im = imagecreate(200,60);
$all_num = "";
$ychar = array(0,1,2,3,4,5,6,7,8,9,"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
for($i=0;$i<5;$i++){
$all_num.=$ychar[rand(0,35)];
}
$_SESSION["code"] = $all_num;
imagecolorallocate($im, 255, 255, 255); //设置白色背景
$textcolor = imagecolorallocate($im, 0, 0, 0);
$left = 60;
$top = 45;
$font = "arial.ttf"; //字体 xp 在 c:/window/Fonts下 我拷贝了一个出来
//imagestring($im, 5, $left, $top, $all_num, $textcolor); 这个没有办法设置字体的大小
imagettftext($im,24,0,$left,$top,$textcolor,$font,$all_num); //
$dot_color = imagecolorallocate($im, 99,99,99);
$line_color = imagecolorallocate($im, 33,33,33);
for($i=0;$i<100;$i++){
$dot_left = $left-10;
$dot_top = $top-30;
imagesetpixel($im,$dot_left+rand(0,120),$dot_top+rand(0,45),$dot_color);
//画点
if(($i+1)%10 == 0){
$line_left = $left-20 +rand(0,10);
$line_top = $top -30;
imageline($im,$line_left,$line_top+rand(0,45),$line_left+rand(0,120),$line_top+rand(0,20),$line_color);
}
//画线
}
imagepng($im);
工作中需要做缩率图,然后就试着写了下,发现bmp的图片处理不了,png的图片缩率后边黑了, 郁闷.....
在网上找了下处理png变黑的问题, 看不懂 ,但是代码还是很少的, 处理bmp图片的 代码量就大了 哎
一个例子 缩放图片 (不支持bmp,而且可能其他没怎么常见的图片格式也不支持)
<?php
function resize($src_img="",$tofile="",$maxheight=55,$maxwidth=200,$qualit=75){
if($src_img == "" || !file_exists($src_img) ){
return false;
}
$img_combine = array("1"=>"gif","2"=>"jpg","3"=>"png");
list($width,$height,$type) = getimagesize($src_img);
if(isset($img_combine[$type])){
$type = $img_combine[$type];
$type == "jpg"&&($type = "jpeg");
}else{
return false;
}
$img_content = file_get_contents($src_img);
$im = imagecreatefromstring($img_content);
//$createimage = "imagecreatefrom$type";
//$im = $createimage($src_img);
$width>$maxwidth || $height>$maxheight
? $scale = min($maxwidth/$width,$maxheight/$height)
: $scale = 1;
if($scale == 1 && $tofile=="" ){
return $im; //如果没有保存路径 并且缩放比例为1,则不经行缩放
}
$newwidth = $width*$scale;
$newheight = $height*$scale;
$newim = imagecreatetruecolor($newwidth, $newheight);
if($type == "png"){
imagealphablending($newim, false);
imagecolortransparent($newim, imagecolorallocatealpha($newim, 0, 0, 0,0));
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagecopyresampled($newim, $im, 200, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagecopyresampled($newim, $im, 400, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagecopyresampled($newim, $im, 600, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagecopyresampled($newim, $im, 800, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagecopyresampled($newim, $im, 1000, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
$saveimg = "image$type";
$tofile = $tofile == "" ? $src_img : $tofile; //$tofile为空 替换本文件
imagedestroy($im);
if($type == "png"){
return $saveimg($newim,$tofile);
}else{
return $saveimg($newim,$tofile,$qualit);
}
}
$filename = "new1.jpeg";
$new_filename = "xx/new1.jpeg";
if(resize($filename,$new_filename)){
echo "生成缩略图成功!!!";
}else{
echo "生成缩略图失败!!!";
}
?>