php gd库 图像操作
PHP GD库
- 使用GD需要开启 PHP默认会开启
- 如果没有开启 则需要在php.ini 把
extension=php_gd2.dll前面的分号去掉 重启apache
-
画出一个红色的正方形
-
画线条
-
函数bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color ) -
imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。
<?php
//创建画布
$img = imagecreatetruecolor(100,100);
//为图像分配颜色
$red = imagecolorallocate($img,0xFF,0x00,0x00);
//画线条
imageline($img,0,0,100,100,$red);
//输出头信息
header('content-type:image/png');
//声明格式 imagepng ( resource $image [, string $filename ] )如果给了参数 $filename 则将图像保存至 $filename 中
imagepng($img);
//销毁图像 释放内存
imagedestroy($img);?>
-
绘制文字
-
imagestring() 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。
bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
-
小例子 验证码:
-
采用imagesetpixel函数来实现干扰
-
添加水印
<?php
//这里仅仅是为了案例需要准备一些素材图片
$url = 'http://www.iyi8.com/uploadfile/2014/0521/20140521105216901.jpg';
$content = file_get_contents($url);
$filename = 'tmp.jpg';
file_put_contents($filename, $content);
$url = 'http://wiki.ubuntu.org.cn/images/3/3b/Qref_Edubuntu_Logo.png';
file_put_contents('logo.png', file_get_contents($url));
//开始添加水印操作
$im = imagecreatefromjpeg($filename);
$logo = imagecreatefrompng('logo.png');
$size = getimagesize('logo.png');
imagecopy($im, $logo, 15, 15, 0, 0, $size[0], $size[1]);header("content-type: image/jpeg");
imagejpeg($im); -
以上代码参考
http://www.imooc.com/learn/26

浙公网安备 33010602011771号