php 图片添加文字水印并添加文字阴影
GD库,是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片。 在网站上GD库通常用来生成缩略图,或者用来对图片加水印,或者用来生成汉字验证码,或者对网站数据生成报表等。
生成效果图:
GD库的安装什么的网上都有,现在很多虚拟空间也都支持,这里就不再赘述。下面通过我实际应用代码的实例和相关的注释为大家介绍一下GD库的使用方法。
代码如下 | 复制代码 |
$str="北京"; $str2= "空气质量:轻度污染"; // 通过图片生成一个对象$im $im = imagecreatefromjpeg("images/3.jpg"); //载入字体zt.ttf $fnt = "zt.ttf"; //创建颜色,用于文字字体的白和阴影的黑 $white=imagecolorallocate($im,222,229,207); $black=imagecolorallocate($im,50,50,50); //创建关于相对图片位置的函数,方便调用 $top=100; $left=60; $top2=170; //在图片中添加文字,imagettftext (image,size,angle, x, y,color,fontfile,text) imagettftext($im,41, 0, $left+1, $top+1, $black, $fnt, $str); imagettftext($im,41, 0, $left, $top, $white, $fnt, $str); imagettftext($im,43, 0, $left+1,$top2+1 , $black, $fnt, $str2); imagettftext($im,43, 0, $left,$top2, $white, $fnt, $str2); //将$im输出 ImageJpeg($im); //销毁$im对象 ImageDestroy($im); |
接下来详细解释一下:
代码如下 | 复制代码 |
imagettftext (image,size,angle, x, y,color,fontfile,text) |
imagettftext() 是将字符串 text画到 image所代表的图像上,从坐标 x,y(左上角为 0, 0)开始,角度为 angle,颜色为 color,使用 fontfile 所指定的 TrueType 字体文件。
由 x,y 所表示的坐标定义了第一个字符的基本点大概在字符的左下角。
angle 以角度表示,0 度为从左向右阅读文本,更高的值表示逆时针方向(即如果值为 90 则表示从下向上阅读文本)。
fontfile 是想要使用的 TrueType 字体的文件名。
text 是文本字符串,可以包含 UTF-8 字符序列。
color 是颜色的索引值。
一个可以供PHP调用ImageMagick功能的PHP扩展。使用这个扩展可以使PHP具备和ImageMagick相同的功能。
ImageMagick是一套功能强大、稳定而且免费的工具集和开发包,可以用来读、写和处理超过185种基本格式的图片文件,包括流行的TIFF, JPEG, GIF, PNG, PDF以及PhotoCD等格式。利用ImageMagick,你可以根据web应用程序的需要动态生成图片, 还可以对一个(或一组)图片进行改变大小、旋转、锐化、减色或增加特效等操作,并将操作的结果以相同格式或其它格式保存。
php_imagick是PHP对图片处理的一个扩展包,可以完成对图片改变大小、旋转、锐化、减色或增加特效等操作。
一、windows下安装Imagick扩展:
1、下载 ImageMagick并安装
http://image_magick.veidrodis.com/image_magick/binaries/ImageMagick-6.6.2-10-Q16-windows-dll.exe
2、下载php_imagick.dll
http://valokuva.org/outside-blog-content/imagick-windows-builds/php53/imagick-2.3.0-dev/vc9_nts/php_imagick.dll
如果你用的是线程安全的php,请下载
http://valokuva.org/outside-blog-content/imagick- windows-builds/php53/imagick-2.3.0-dev/vc9_zts/php_imagick.dll
3、设置
在php.ini中添加
extension=php_imagick.dll ,重启web server
二、linux下安装Imagick扩展:
1.yum安装ImageMagick
yum install ImageMagick ImageMagick-devel
2.测试是否安装成功
convert -version
3.安装imagick扩展
01.wget http://pecl.php.net/get/imagick-3.1.0RC2.tgz02.tar xzvf imagick-3.1.0RC2.tgz03.cd imagick-3.1.0RC204.phpize05../configure06.make07.make install
4.编辑php.ini文件,在文件末尾添加如下代码
extension=imagick.so
5. 重新启动apache服务器
service httpd restart
三、案例
1. 边框处理
代码如下 | 复制代码 |
//by www.111cn.net header('Content-type: image/jpeg'); $image = new Imagick('test.jpg'); $color=new ImagickPixel(); $color->setColor("rgb(220,220,220)"); $image->borderImage($color,5,4); $image->blurImage(5,5,imagick::CHANNEL_GREEN); echo $image; |
我们先来看个简单的实例
php_imagick程序示例
1.创建一个缩略图并显示出来
代码如下 | 复制代码 |
<?php header('Content-type: image/jpeg'); $image = new Imagick('image.jpg'); // If 0 is provided as a width or height parameter,// aspect ratio is maintained $image->thumbnailImage(100, 0); echo $image; ?> |
2.创建一个目录下的缩略图,并保存
代码如下 | 复制代码 |
<?php |
3.缩略GIF动画图片
代码如下 | 复制代码 |
<?php |
现在我们进入正题吧,
例1
裁切/生成缩略图/添加水印, 自动检测和处理 GIF
调用方式:
代码如下 | 复制代码 |
include 'imagick.class.php'; $image = new lib_image_imagick(); $image->open('a.gif'); |
imagick.class.php
代码如下 | 复制代码 |
<?php class lib_image_imagick // 构造函数
// 载入图像 public function crop($x=0, $y=0, $width=null, $height=null) $canvas->addImage( $img ); /* $canvas->addImage( $img ); $canvas->addImage( $img ); if($src_width*$height > $src_height*$width) $image = $this->image; $draw = new ImagickDraw(); $img = new Imagick(); $canvas->addImage( $img );
// 添加水印图片 if($this->type=='gif') // 输出图像 // 设置图像类型, 默认与源类型一致 // 获取源图像类型
/* public function add_noise($type=null){$this->image->addNoiseImage($type==null?imagick::NOISE_IMPULSE:$type);} // 添加噪点 public function charcoal($radius, $sigma){$this->image->charcoalImage($radius, $sigma);} // 素描 } |
实现水印功能主要就是靠这些函数功能操作
1.imagecreatefromjpeg // 打开JPG图片 2.imagecreatefromgif // 打开GIF图片
3.imagecreatefrompng // 打开PNG图片
4.imagecreatefromwbmp // 打开WBMP图片(比较少用)
5.getimagesize // 获取图片大小信息
6.imagecopymerge // 把多张图片整合(添加水印的主要函数)
7.imagejpeg // 保存JPG图片
8.imagegif // 保存GIF图片
9.imagepng // 保存PNG图片
代码如下 | 复制代码 |
<?php |
目前支持jpg、gif、png等图片格式。
用法举例:
代码如下 | 复制代码 |
if($pic = watermark('./image.jpg','./watermark.png')) |
下面演示一个完整全水印增加函数
代码如下 | 复制代码 |
<?php 参数说明: 使用说明: //上传文件类型列表 $max_file_size=2000000; //上传文件大小限制, 单位BYTE <body> $file = $_FILES["upfile"]; if(!in_array($file["type"], $uptypes)) if(!file_exists($destination_folder)) $filename=$file["tmp_name"]; if(!move_uploaded_file ($filename, $destination)) $pinfo=pathinfo($destination); if($watermark==1) imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); switch($watertype) switch ($iinfo[2]) //覆盖原上传文件 if($imgpreview==1) |
我们先来检查一下自己的php是不是打开了gd库。
代码如下 | 复制代码 |
<?php |
如果有返回信息就正确可以常用使用了
例1
代码如下 | 复制代码 |
<?php function vCode($num = 4, $size = 20, $width = 0, $height = 0) { ?> |
例2
使用PHP,结合session和GD库扩展开发的一个生成验证码的例子(w3c推荐),可以很方便的用于项目中。而且样式美观//首先开启session
session_start();
//定义前台显示验证码长&宽
$image_width = 120;
$image_height = 40;
$characters_on_image = 6;
$font = './monofont.ttf';
//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 10;
$random_lines = 30;
$captcha_text_color="0x142864";
$captcha_noice_color = "0x142864";
//定义要生成验证码的字符串
$code = '';
$i = 0;
while ($i < $characters_on_image) {
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}
$font_size = $image_height * 0.75;
$image = @imagecreate($image_width, $image_height);
/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 255, 255, 255);
$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color['red'],
$arr_text_color['green'], $arr_text_color['blue']);
$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],
$arr_noice_color['green'], $arr_noice_color['blue']);
/* generating the dots randomly in background */
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
mt_rand(0,$image_height), 2, 3, $image_noise_color);
}
/* generating lines randomly in background of image */
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}
/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code);
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
/* Show captcha image in the page html page */
header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
//设置session,做验证
$_SESSION['6_letters_code'] = $code;
function hexrgb ($hexstr)
{
$int = hexdec($hexstr);
return array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
}
个人推荐推荐第二个生成验证码程序代码,各位同学可尝试参考对比哦,最后一个是W3C标准生成的也是利用了php gd库。
本文章来给各同学总结了一些常用的图像处理函数,包括有缩放、剪裁、缩放、翻转、旋转、透明、锐化功能,大家可参考参考。
注意事项:如果要使用php gd处理我们需要开启gd库
Windows下开启PHP的GD库支持
找到php.ini,打开内容,找到:
;extension=php_gd2.dll
把最前面的分号“;”去掉,再保存即可,如果本来就没有分号,那就是已经开启了。
linux开启GD库
##检测GD库是否安装命令
php5 -m | grep -i gd
或者
php -i | grep -i --color gd
##如未安装GD库,则为服务器安装,方法如下
### 如果是源码安装,则加入参数
--with-gd
### 如果是debian系的linux系统,用apt-get安装,如下
apt-get install php5-gd
### 如果是CentOS系的系统,用yum安装,如下
yum install php-gd
### 如果是suse系的linux系统,用yast安装,如下
yast -i php5_gd
一、创建图片资源
imagecreatetruecolor(width,height);
imagecreatefromgif(图片名称);
imagecreatefrompng(图片名称);
imagecreatefromjpeg(图片名称);
画出各种图像
imagegif(图片资源,保存路径);
imagepng()
imagejpeg();
二、获取图片属性
imagesx(res//宽度
imagesy(res//高度
getimagesize(文件路径)
返回一个具有四个单元的数组。索引 0 包含图像宽度的像素值,索引 1 包含图像高度的像素值。索引 2 是图像类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。这些标记与 PHP 4.3.0 新加的 IMAGETYPE 常量对应。索引 3 是文本字符串,内容为“height="yyy" width="xxx"”,可直接用于 IMG 标记。销毁图像资源
imagedestroy(图片资源);
三、透明处理
PNG、jpeg透明色都正常,只有gif不正常
imagecolortransparent(resource image [,int color])//将某个颜色设置成透明色
imagecolorstotal()
imagecolorforindex();
四、图片的裁剪
imagecopyresized()
imagecopyresampled();
五、加水印(文字、图片)
字符串编码转换string iconv ( string $in_charset , string $out_charset , string $str )
六、图片旋转
imagerotate();//制定角度的图片翻转
七、图片的翻转
沿X轴 沿Y轴翻转
八、锐化
imagecolorsforindex()
imagecolorat()
在图片上画图形
$img=imagecreatefromgif("./images/map.gif");
$red= imagecolorallocate($img, 255, 0, 0);
imageline($img, 0, 0, 100, 100, $red);
imageellipse($img, 200, 100, 100, 100, $red);
imagegif($img, "./images/map2.gif");
imagedestroy($img);图片普通缩放
$filename="./images/hee.jpg";
$per=0.3;
list($width, $height)=getimagesize($filename);
$n_w=$width*$per;
$n_h=$width*$per;
$new=imagecreatetruecolor($n_w, $n_h);
$img=imagecreatefromjpeg($filename);
//拷贝部分图像并调整
imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height);
//图像输出新图片、另存为
imagejpeg($new, "./images/hee2.jpg");
imagedestroy($new);
imagedestroy($img);图片等比例缩放、没处理透明色
function thumn($background, $width, $height, $newfile) {
list($s_w, $s_h)=getimagesize($background);//获取原图片高度、宽度
if ($width && ($s_w < $s_h)) {
$width = ($height / $s_h) * $s_w;
} else {
$height = ($width / $s_w) * $s_h;
}
$new=imagecreatetruecolor($width, $height);
$img=imagecreatefromjpeg($background);
imagecopyresampled($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);
imagejpeg($new, $newfile);
imagedestroy($new);
imagedestroy($img);
}
thumn("images/hee.jpg", 200, 200, "./images/hee3.jpg");gif透明色处理
function thumn($background, $width, $height, $newfile) {
list($s_w, $s_h)=getimagesize($background);
if ($width && ($s_w < $s_h)) {
$width = ($height / $s_h) * $s_w;
} else {
$height = ($width / $s_w) * $s_h;
}
$new=imagecreatetruecolor($width, $height);
$img=imagecreatefromgif($background);
$otsc=imagecolortransparent($img);
if($otsc >=0 && $otst < imagecolorstotal($img)){//判断索引色
$tran=imagecolorsforindex($img, $otsc);//索引颜色值
$newt=imagecolorallocate($new, $tran["red"], $tran["green"], $tran["blue"]);
imagefill($new, 0, 0, $newt);
imagecolortransparent($new, $newt);
}
imagecopyresized($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);
imagegif($new, $newfile);
imagedestroy($new);
imagedestroy($img);
}
thumn("images/map.gif", 200, 200, "./images/map3.gif");图片裁剪
function cut($background, $cut_x, $cut_y, $cut_width, $cut_height, $location){
$back=imagecreatefromjpeg($background);
$new=imagecreatetruecolor($cut_width, $cut_height);
imagecopyresampled($new, $back, 0, 0, $cut_x, $cut_y, $cut_width, $cut_height,$cut_width,$cut_height);
imagejpeg($new, $location);
imagedestroy($new);
imagedestroy($back);
}
cut("./images/hee.jpg", 440, 140, 117, 112, "./images/hee5.jpg");图片加水印
文字水印
function mark_text($background, $text, $x, $y){
$back=imagecreatefromjpeg($background);
$color=imagecolorallocate($back, 0, 255, 0);
imagettftext($back, 20, 0, $x, $y, $color, "simkai.ttf", $text);
imagejpeg($back, "./images/hee7.jpg");
imagedestroy($back);
}
mark_text("./images/hee.jpg", "细说PHP", 150, 250);
//图片水印
function mark_pic($background, $waterpic, $x, $y){
$back=imagecreatefromjpeg($background);
$water=imagecreatefromgif($waterpic);
$w_w=imagesx($water);
$w_h=imagesy($water);
imagecopy($back, $water, $x, $y, 0, 0, $w_w, $w_h);
imagejpeg($back,"./images/hee8.jpg");
imagedestroy($back);
imagedestroy($water);
}
mark_pic("./images/hee.jpg", "./images/gaolf.gif", 50, 200);图片旋转
$back=imagecreatefromjpeg("./images/hee.jpg");
$new=imagerotate($back, 45, 0);
imagejpeg($new, "./images/hee9.jpg");图片水平翻转垂直翻转
function turn_y($background, $newfile){
$back=imagecreatefromjpeg($background);
$width=imagesx($back);
$height=imagesy($back);
$new=imagecreatetruecolor($width, $height);
for($x=0; $x < $width; $x++){
imagecopy($new, $back, $width-$x-1, 0, $x, 0, 1, $height);
}
imagejpeg($new, $newfile);
imagedestroy($back);
imagedestroy($new);
}
function turn_x($background, $newfile){
$back=imagecreatefromjpeg($background);
$width=imagesx($back);
$height=imagesy($back);
$new=imagecreatetruecolor($width, $height);
for($y=0; $y < $height; $y++){
imagecopy($new, $back,0, $height-$y-1, 0, $y, $width, 1);
}
imagejpeg($new, $newfile);
imagedestroy($back);
imagedestroy($new);
}
turn_y("./images/hee.jpg", "./images/hee11.jpg");
turn_x("./images/hee.jpg", "./images/hee12.jpg"); 图片锐化
function sharp($background, $degree, $save){
$back=imagecreatefromjpeg($background);
$b_x=imagesx($back);
$b_y=imagesy($back);
$dst=imagecreatefromjpeg($background);
for($i=0; $i<$b_x; $i++){
for($j=0; $j<$b_y; $j++){
$b_clr1=imagecolorsforindex($back, imagecolorat($back, $i-1, $j-1));\前一个像素颜色数组
$b_clr2=imagecolorsforindex($back, imagecolorat($back, $i, $j));\取出当前颜色数组
$r=intval($b_clr2["red"]+$degree*($b_clr2["red"]-$b_clr1["red"]));\加深
$g=intval($b_clr2["green"]+$degree*($b_clr2["green"]-$b_clr1["green"]));
$b=intval($b_clr2["blue"]+$degree*($b_clr2["blue"]-$b_clr1["blue"]));
$r=min(255, max($r, 0));//限制r范围在0-255之间
$g=min(255, max($g, 0));
$b=min(255, max($b, 0));
if(($d_clr=imagecolorexact($dst, $r, $g, $b))==-1){//等于1不在颜色范围内
$d_clr=Imagecolorallocate($dst, $r, $g, $b);//创建一个颜色
}
imagesetpixel($dst, $i, $j, $d_clr);
}
}
imagejpeg($dst, $save);
imagedestroy($back);
imagedestroy($dst);
}
sharp("./images/hee.jpg", 20, "./images/hee13.jpg");十月 26th, 2011No comments yet真诚待 php设计实现和应用验证码类
validationcode.class.php
/* 开发一个验证码类
*
* 1. 什么是验证码
*
* 2. 验证码的作用
*
* 3. 编写验证码类(PHP图像处理)
*
* 4. 使用验证码
*
*
*/<?php
class ValidationCode {
private $width;
private $height;
private $codeNum;
private $image; //图像资源
private $disturbColorNum;
private $checkCode;
function __construct($width=80, $height=20, $codeNum=4){
$this->width=$width;
$this->height=$height;
$this->codeNum=$codeNum;
$this->checkCode=$this->createCheckCode();
$number=floor($width*$height/15);
if($number > 240-$codeNum){
$this->disturbColorNum= 240-$codeNum;
}else{
$this->disturbColorNum=$number;
}
}
//通过访问该方法向浏览器中输出图像
function showImage($fontFace=""){
//第一步:创建图像背景
$this->createImage();
//第二步:设置干扰元素
$this->setDisturbColor();
//第三步:向图像中随机画出文本
$this->outputText($fontFace);
//第四步:输出图像
$this->outputImage();
}
//通过调用该方法获取随机创建的验证码字符串
function getCheckCode(){
return $this->checkCode;
}
private function createImage(){
//创建图像资源
$this->image=imagecreatetruecolor($this->width, $this->height);
//随机背景色
$backColor=imagecolorallocate($this->image, rand(225, 255), rand(225,255), rand(225, 255));
//为背景添充颜色
imagefill($this->image, 0, 0, $backColor);
//设置边框颜色
$border=imagecolorallocate($this->image, 0, 0, 0);
//画出矩形边框
imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $border);
}
private function setDisturbColor(){
for($i=0; $i<$this->disturbColorNum; $i++){
$color=imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($this->image, rand(1, $this->width-2), rand(1, $this->height-2), $color);
}
for($i=0; $i<10; $i++){
$color=imagecolorallocate($this->image, rand(200, 255), rand(200, 255), rand(200, 255));
imagearc($this->image, rand(-10, $this->width), rand(-10, $this->height), rand(30, 300), rand(20, 200), 55, 44, $color);
}
}
private function createCheckCode(){
$code="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
$string='';
for($i=0; $i < $this->codeNum; $i++){
$char=$code{rand(0, strlen($code)-1)};
$string.=$char;
}
return $string;
}
private function outputText($fontFace=""){
for($i=0; $i<$this->codeNum; $i++){
$fontcolor=imagecolorallocate($this->image, rand(0, 128), rand(0, 128), rand(0, 128));
if($fontFace==""){
$fontsize=rand(3, 5);
$x=floor($this->width/$this->codeNum)*$i+3;
$y=rand(0, $this->height-15);
imagechar($this->image,$fontsize, $x, $y, $this->checkCode{$i},$fontcolor);
}else{
$fontsize=rand(12, 16);
$x=floor(($this->width-8)/$this->codeNum)*$i+8;
$y=rand($fontSize+5, $this->height);
imagettftext($this->image,$fontsize,rand(-30, 30),$x,$y ,$fontcolor, $fontFace, $this->checkCode{$i});
}
}
}
private function outputImage() {
if(imagetypes() & IMG_GIF){
header("Content-Type:image/gif");
imagepng($this->image);
}else if(imagetypes() & IMG_JPG){
header("Content-Type:image/jpeg");
imagepng($this->image);
}else if(imagetypes() & IMG_PNG){
header("Content-Type:image/png");
imagepng($this->image);
}else if(imagetypes() & IMG_WBMP){
header("Content-Type:image/vnd.wap.wbmp");
imagepng($this->image);
}else{
die("PHP不支持图像创建");
}
}
function __destruct(){
imagedestroy($this->image);
}
}code.php
<?php
session_start();
include "validationcode.class.php";
$code=new ValidationCode(80, 20, 4);
$code->showImage(); //输出到页面中供 注册或登录使用
$_SESSION["code"]=$code->getCheckCode(); //将验证码保存到服务器中demo.php
<?php
session_start();
echo $_POST["code"]."<br>";
echo $_SESSION["code"]."<br>";
if(strtoupper($_POST["code"])==strtoupper($_SESSION["code"])){
echo "ok";
}else{
echo "error";
}
?>
<body>
<form action="login.php" method="post">
user:<input type="text" name="usenrame"><br>
pass:<input type="passowrd" name="pass"><br>
code: <input type="text" name="code" onkeyup="if(this.value!=this.value.toUpperCase()) this.value=this.value.toUpperCase()"> <img src="code.php" onclick="this.src='code.php?'+Math.random()"><br>
<input type="submit" name="sub" value="login"><br>
</form>
</body>
1、画图
验证码,统计图
安装GD库(图片处理库)
■创建一个画布—-创建资源类型——–高度、宽度
■绘制图像
1、制定各种颜色
2、矩形、园、点、线段、扇形、画字(字符、字符串、freetype字体),每一个图像对应一个函数
■输出图像/保存处理好的图像
1、输出各种类型(gif、png、jpeg)
imagegif();
imagepng();
imagegpeg();
■释放资源
<?php
//step 1创建图片资源
$img=imagecreatetruecolor(200,200);
$red=imagecolorallocate($img,255,0,0);
$yellow=imagecolorallocate($img,255,255,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
imagefill($img,0,0,$yellow);//颜色填充
//step2画各种图形
imagefilledrectangle($img,10,10,80,80,$red);//画矩形并填充
imagerectangle($img,10,10,80,80,$red));//画矩形不填充,颜色填充
//线段
imageline($img,0, 0, 200, 200 ,$blue);
imageline($img,200, 0, 0, 200, $blue);
//点
imagesetpixel($img,50, 50 ,$red);
imagesetpixel($img,55, 50 ,$red);
imagesetpixel($img,59, 50 ,$red);
imagesetpixel($img,64, 50 ,$red);
imagesetpixel($img,72, 50 ,$red);
//圆
imageellipse($img, 100, 100, 100, 100,$green);
//圆
imagefilledellipse($img, 100, 100, 10, 10,$blue);
边框
//输出图像或保存图像
header("Content-Type:img/gif");
imagegif($img);
//释放资源
imagedestory($img);
画饼状图
//step 1创建图片资源
$img=imagecreatetruecolor(200, 200);
// $img=imagecreate(200, 200);
$white=imagecolorallocate($img, 255, 255, 255);
$gray=imagecolorallocate($img, 0xC0, 0xC0, 0xC0);
$darkgray=imagecolorallocate($img, 0x90, 0x90, 0x90);
$navy=imagecolorallocate($img, 0, 0, 0x80);
$darknavy=imagecolorallocate($img, 0, 0, 0x50);
$red= imagecolorallocate($img, 0xFF, 0, 0);
$darkred=imagecolorallocate($img, 0x90, 0, 0);
imagefill($img, 0, 0, $white);
//3D效果
for($i=60; $i>50; $i--){
imagefilledarc($img, 50, $i,100, 50, -160, 40, $darkgray, IMG_ARC_PIE);
imagefilledarc($img, 50, $i,100, 50, 40, 75, $darknavy, IMG_ARC_PIE);
imagefilledarc($img, 50, $i,100, 50, 75, 200, $darkred, IMG_ARC_PIE);
}
imagefilledarc($img, 50, $i,100, 50, -160, 40, $gray, IMG_ARC_PIE);
imagefilledarc($img, 50, $i,100, 50, 40, 75, $navy, IMG_ARC_PIE);
imagefilledarc($img, 50, $i,100, 50, 75, 200, $red, IMG_ARC_PIE);
header("Content-Type:image/gif");
imagegif($img);
//释放资源
imagedestroy($img);画文字
<?php
//step 1创建图片资源
$img=imagecreatetruecolor(200, 200);
// $img=imagecreate(200, 200);
$white=imagecolorallocate($img, 255, 255, 255);
$gray=imagecolorallocate($img, 0xC0, 0xC0, 0xC0);
$darkgray=imagecolorallocate($img, 0x90, 0x90, 0x90);
$navy=imagecolorallocate($img, 0, 0, 0x80);
$darknavy=imagecolorallocate($img, 0, 0, 0x50);
$red= imagecolorallocate($img, 0xFF, 0, 0);
$darkred=imagecolorallocate($img, 0x90, 0, 0);
imagefill($img, 0, 0, $gray);
imagechar($img, 5, 100, 100, "A", $red);
imagechar($img, 5, 120, 120, "B", $red);
imagecharup($img, 5, 60, 60, "C", $red);
imagecharup($img, 5, 80, 80, "D", $red);
imagestring($img, 3, 10, 10, "Hello", $navy);
imagestringup($img, 3, 10, 80, "Hello", $navy);
imagettftext($img, 25, 60, 150, 150, $red, "simkai.ttf", "##");
imagettftext($img, 12, -60, 50, 150, $red, "simli.ttf", "##");
header("Content-Type:image/gif");
imagegif($img);
//释放资源
imagedestroy($img);2、处理原有的图像
相关文章
使用PHP+JavaScript将HTML页面转换为图片的实例分享
这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
- 今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
- php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13- 下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
- Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
- 图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04
- 这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
- 共享一段使用PHP下载CSS文件中的图片的代码 复制代码 代码如下: <?php //note 设置PHP超时时间 set_time_limit(0); //note 取得样式文件内容 $styleFileContent = file_get_contents('images/style.css'); //not...2013-10-04
- PHP代码如下:复制代码 代码如下:if (isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) { $upload_file = $_FILES['Filedata']; $fil...2013-10-04
C#中图片旋转和翻转(RotateFlipType)用法分析
这篇文章主要介绍了C#中图片旋转和翻转(RotateFlipType)用法,实例分析了C#图片旋转及翻转Image.RotateFlip方法属性的常用设置技巧,需要的朋友可以参考下...2020-06-25- 这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
- ps软件是现在很多人比较喜欢的,有着非常不错的使用效果,这次文章就给大家介绍下ps怎么制作图片阴影效果,还不知道制作方法的赶紧来看看。 ps图片阴影效果怎么做方法/...2017-07-06
- 这篇文章主要介绍了OpenCV如何去除图片中的阴影的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-29
- 本文主要介绍用C#实现图片转换成字节流,字节流转换成图片,并根据图片路径返回图片的字节流,有需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了JavaScript 如何禁止用户保存图片,帮助大家完成需求,更好的理解和使用JavaScript,感兴趣的朋友可以了解下...2020-11-19
- 我们在php中上传文件就必须使用#_FILE变量了,这个自动全局变量 $_FILES 从 PHP 4.1.0 版本开始被支持。在这之前,从 4.0.0 版本开始,PHP 支持 $HTTP_POST_FILES 数组。这...2016-11-25
- SwiftUI是一种使用Swift语言在苹果设备上构建用户界面的创新且简单的方式,下面这篇文章主要给大家介绍了关于SwiftUI图片缩放、拼图等处理的相关资料,需要的朋友可以参考下...2021-08-23