GD库基础

来源:互联网 发布:淘宝个人中心在哪里找 编辑:程序博客网 时间:2024/06/01 10:14
-----------------------------------GD库基础1----------------<?php//1、创建画布$img = imagecreatetruecolor(200,200);//var_dump($img);//2、画// 创建颜色$red = imagecolorallocate($img,255,0,0); //填充画布imagefill($img,0,0,$red);//3、展示/保存//通知浏览器输出图片header("Content-Type:image/png");//输出图片imagepng($img);//4、释放资源imagedestroy($img);-----------------------------------------------------------<?php//画一张200*200的绿色画布//1、创建画布$img = imagecreatetruecolor(200,200);//var_dump($img);//2、画// 创建颜色$red = imagecolorallocate($img,255,0,0); //填充画布imagefill($img,0,0,$red);//3、展示/保存//通知浏览器输出图片header("Content-Type:image/png");//输出图片imagepng($img);//4、释放资源imagedestroy($img);---------------------------在画布上画随机的点-----------------<?php//画点//1、创建画布$img = imagecreatetruecolor(200,200);//2、画$green = imagecolorallocate($img,0,255,0);$red = imagecolorallocate($img,255,0,0);//填充画布imagefill($img,0,0,$red);// 画点//2、在上一题的画布上,画一个红色的点。for($i=1;$i<100;$i++){    imagesetpixel($img,rand(1,199),                  rand(1,199),$red);}//输出图片header("Content-Type:image/gif");imagegif($img);//释放资源imagedestroy($img);------------------------------画一个蓝色的十字---------------------------------<?php//3、在上一题的画布上,画一个蓝色的正十字。//1、创建画布$img = imagecreatetruecolor(200,200);//2、画$green = imagecolorallocate($img,0,255,0);$blue = imagecolorallocate($img,0,0,255);//填充画布imagefill($img,0,0,$green);//画线imageline($img,100,0,100,200,$blue);imageline($img,0,100,200,100,$blue);//展示header("Content-Type:image/png");imagepng($img);//释放资源imagedestroy($img);-----------------------------画一个蓝色的实心矩形--------------<?php//矩形//1、创建画布$img = imagecreatetruecolor(200,200);//2、画$green = imagecolorallocate($img,0,255,0);$blue = imagecolorallocate($img,0,0,255);//填充画布imagefill($img,0,0,$green);//4、在上一题的画布上,画一个蓝色的实心矩形。//画矩形imagerectangle($img,50,50,100,100,$blue);//画实心矩形imagefilledrectangle($img,100,100,150,150,                     $blue);//展示header("Content-Type:image/png");imagepng($img);//释放资源imagedestroy($img);---------------------------------画一个实心圆----------------<?php//圆//1、创建画布$img = imagecreatetruecolor(300,300);//2、画//创建颜色$red = imagecolorallocate($img,255,0,0);$blue = imagecolorallocate($img,0,0,255);//填充画布imagefill($img,0,0,$red);//5、在上一题的画布上,画一个实心圆。//圆imageellipse($img,150,150,100,100,$blue);//展示header("Content-Type:image/png");imagepng($img);//释放资源imagedestroy($img);----------------------------------在画布上输出文字------------<?php/* * 6、创建一个200*200的蓝色背景画布,在画布上输出文字   "have a nice day" *///1、创建画布$img = imagecreatetruecolor(200,200);//2、画$blue = imagecolorallocate($img,0,0,255);$green = imagecolorallocate($img,0,255,0);//填充画布imagefill($img,0,0,$blue);//输出文字imagestring($img,5,10,50,"have a nice day",$green);//展示header("Content-Type:image/png");imagepng($img);//释放资源imagedestroy($img);----------------画布上输出文字,可以用不同的字体显示文字----------<?php//1、创建画布$img = imagecreatetruecolor(200,200);//2、画$blue = imagecolorallocate($img,0,0,255);$green = imagecolorallocate($img,0,255,0);//填充画布imagefill($img,0,0,$blue);//7、在上一题的画布上输出文字,可以用不同的字体显示文字。//输出文字imagettftext($img,30,30,50,50,$green,"JOKERMAN.TTF","hello");//展示header("Content-Type:image/png");imagepng($img);//释放资源imagedestroy($img);------------------------------------验证码------------------<?php//验证码/* *  - 画布(浅色、随机)    - 干扰(随机出现的线,随机出现的点)    - 输出文字    - 展示    - 释放资源*/ //画布(浅色、随机)$img = imagecreatetruecolor(80,30);//创建画布的背景色$bgcolor = imagecolorallocate($img,                      rand(200,255),                      rand(200,255),                      rand(200,255));//填充画布imagefill($img,0,0,$bgcolor);//- 干扰(随机出现的线,随机出现的点)for($i=0;$i<50;$i++){    $color = imagecolorallocate($img,                                rand(100,200),                                rand(100,200),                                rand(100,200));    imagesetpixel($img,rand(1,79),                       rand(1,29),$color);}//随机10条线for($i=0;$i<10;$i++){    $color = imagecolorallocate($img,            rand(100,200),            rand(100,200),            rand(100,200));    imageline($img,rand(1,79),rand(1,29),                   rand(1,79),rand(1,29),$color);}//- 输出文字$codes = "0123456789abcdefghijklmnopqrstuvwxyz";//规定验证码的长度$length = 4;//从 库字符串 随机截取 每次截取一个字符for($i=0;$i<$length;$i++){    $color = imagecolorallocate($img,                                rand(0,100),                                rand(0,100),                                rand(0,100));    $str = substr($codes,rand(0,strlen($codes)-1),1);    $x = (80/$length)*$i+5;    $y = rand(5,10);    imagestring($img,5,$x,$y,$str,$color);//font字体的大小最大是5}//- 展示header("Content-Type:image/png");imagepng($img);//- 释放资源imagedestroy($img);-----------------------------------------------------------<?php //1、在现有图片上输出文字 hello//1、获取图片资源$filename = "t.jpg";$img = imagecreatefromjpeg($filename);//2、画$red = imagecolorallocate($img,255,0,0);//输出文字imagestring($img,5,20,20,"hello",$red);//3、展示header("Content-Type:image/png");imagepng($img);//4、释放资源imagedestroy($img);-------------------------------------GD库基础2--------------<?php //1、在现有图片上输出文字 hello//1、获取图片资源$filename = "t.jpg";$img = imagecreatefromjpeg($filename);//2、画$red = imagecolorallocate($img,255,0,0);//输出文字imagestring($img,5,20,20,"hello",$red);//3、展示header("Content-Type:image/png");imagepng($img);//4、释放资源imagedestroy($img);-----------------------------------------<?php //1、在现有图片上输出文字 hello//1、获取图片资源$filename = "t.jpg";//获取图片的信息$fileinfo = getimagesize($filename);//图片的宽$width = $fileinfo[0];//图片的高$height = $fileinfo[1];$img = imagecreatefromjpeg($filename);//2、画$red = imagecolorallocate($img,255,0,0);//确定水印文字的位置//x = 原图片的宽度-(文字的宽度+希望空开的距离)//文字的宽度=每个字符的宽度*字符串长度$str_w = imagefontwidth(5)*strlen("hello");$x = $width-($str_w+10);//y= 原图片的高度-(文字的高度+希望空开的距离)//文字的高度$str_h = imagefontheight(5);$y = $height-($str_h+10);imagestring($img,5,$x,$y,"hello",$red);//3、展示header("Content-Type:image/png");imagepng($img);//4、释放资源imagedestroy($img);------在现有图片上输出文字 hello------------<?php //1、在现有图片上输出文字 hello//1、获取图片资源$filename = "t.jpg";//获取图片的信息$fileinfo = getimagesize($filename);//图片的宽$width = $fileinfo[0];//图片的高$height = $fileinfo[1];$img = imagecreatefromjpeg($filename);//2、画$red = imagecolorallocate($img,255,0,0);//确定水印文字的位置//x = 原图片的宽度-(文字的宽度+希望空开的距离)//文字的宽度=每个字符的宽度*字符串长度$str_w = imagefontwidth(5)*strlen("hello");$x = $width-($str_w+10);//y= 原图片的高度-(文字的高度+希望空开的距离)//文字的高度$str_h = imagefontheight(5);$y = $height-($str_h+10);imagestring($img,5,$x,$y,"hello",$red);//3、展示header("Content-Type:image/png");imagepng($img);//4、释放资源imagedestroy($img);-------------------------将两张图片合成一张图片---------------<?php //图片水印//获取大图片的资源$filename = "dog.jpg";$des_img = imagecreatefromjpeg($filename);//获取小图片资源$watermark_file = "qq.png";$src_img = imagecreatefrompng($watermark_file);//水印图片在大图中出现的x轴位置$des_x = 300;//水印图片在大图中出现的y轴位置$des_y = 180;$src_x = 0;$src_y = 0;//水印图片的宽和高$src_w = 22;$src_h = 25;imagecopy($des_img,$src_img,          $des_x,$des_y,          $src_x,$src_y,          $src_w,$src_h);//展示header("Content-Type:image/png");imagepng($des_img);//释放资源imagedestroy($des_img);imagedestroy($src_img);----------------将两张图片合成一张图片(动态)--------------<?php //图片水印//获取大图片的资源$filename = "dog.jpg";$des_img = imagecreatefromjpeg($filename);//获取大图片的信息$fileinfo = getimagesize($filename);//大图片的宽$width = $fileinfo[0];//大图片的高$height = $fileinfo[1];//获取小图片资源$watermark_file = "qq.png";$src_img = imagecreatefrompng($watermark_file);//获取小图片的信息$w_fileinfo = getimagesize($watermark_file);//小图片宽$w_width = $w_fileinfo[0];//小图片高$w_height = $w_fileinfo[1];//水印图片在大图片上出现的位置(x)=大图片的宽-//(小图片的宽+希望空开的距离)$des_x = $width-($w_width+10);//水印图片在大图片上出现的位置(y)=//大图片的高-(小图片的高+希望空开的距离)$des_y = $height-($w_height+10);imagecopy($des_img,$src_img,          $des_x,$des_y,          0,0,          $w_width,$w_height);//展示header("Content-Type:image/png");imagepng($des_img);//释放资源imagedestroy($des_img);imagedestroy($src_img);-----------将两张图片合成一张图片(封装成自定义函数)----------<?php //图片水印-自定义函数//小驼峰 第一个单词首字母小写后面每个单词首字母大写//例如: waterMark() //大驼峰 每个单词首字母大写//例如:WaterMark()waterMark();function waterMark(){    //获取大图片的资源    $filename = "t.jpg";    $des_img = imagecreatefromjpeg($filename);    //获取大图片的宽和高    $fileinfo = getimagesize($filename);    $width = $fileinfo[0];    $height = $fileinfo[1];    //水印图片资源    $watermark_file = "qq.png";    $src_img = imagecreatefrompng($watermark_file);    //获取水印图片的宽和高    $w_fileinfo = getimagesize($watermark_file);    $w_width = $w_fileinfo[0];    $w_height = $w_fileinfo[1];    $des_x = $width-($w_width+10);    $des_y = $height-($w_height+10);    $src_x = 0;    $src_y = 0;    //函数体    imagecopy($des_img,$src_img,              $des_x,$des_y,              $src_x,$src_y,              $w_width,$w_height);    //展示    header("Content-Type:image/png");    imagepng($des_img);    //释放资源    imagedestroy($des_img);    imagedestroy($src_img);}--------------将两张图片合成一张图片(封装成自定义函数-整理版)------<?phpwaterMark();function waterMark(){    $filename="dog.jpg";    $big_img=imagecreatefromjpeg($filename);    //水印图片资源    $watermark_file="qq.png";    $small_img=imagecreatefrompng($watermark_file);    //获取大图片的宽和高    $big_info=getimagesize($filename);    $big_width=$big_info[0];    $big_height=$big_info[1];    //获取小图片的宽和高    $small_info=getimagesize($watermark_file);    $small_width=$small_info[0];    $small_height=$small_info[1];    //获取实际的水印的左边位置    $des_width=$big_width-($small_width+10);    $des_height=$big_height-($small_height+10);    //函数体    imagecopy($big_img,$small_img,$des_width,$des_height,0,0,$small_width,$small_height);    //展示    header("Content-Type:image/png");    imagepng($big_img);    //两张图片都要释放    imagedestroy($big_img);    imagedestroy($small_img);}-----------将两张图片合成一张图片(封装成自定义函数-优化拼接版)--<?php$filename="dog.jpg";$watermark_file="qq.png";waterMark($filename,$watermark_file);function waterMark($filename,$watermark_file){    //获取大图片的资源属性    $big_info=getimagesize($filename);    $big_width=$big_info[0];    $big_height=$big_info[1];    $big_type=$big_info[2];    //获取小图片的资源属性    $small_info=getimagesize($watermark_file);    $small_width=$small_info[0];    $small_height=$small_info[1];    $small_type=$small_info[2];    //类型和图片格式字符串之间的对应关系    $type_arr=array(1=>'gif',2=>'jpeg',3=>'png');    $func_str="imagecreatefrom";//后面具体是什么图片格式需要拼装    //1 实际获取到的大图片格式    $big_type_arr=$type_arr[$big_type];    //2 拼接大图片函数    $big_function=$func_str.$big_type_arr;    //3 调用拼接函数获取大图片的资源    $big_img=$big_function($filename);    //1 实际获取到的小图片格式    $small_type_arr=$type_arr[$small_type];    //2 拼接小图片函数    $small_function=$func_str.$small_type_arr;    //3 调用拼接函数获取大图片的资源    $small_img=$small_function($watermark_file);    //获取实际的水印的左边位置    $des_width=$big_width-($small_width+10);    $des_height=$big_height-($small_height+10);    //函数体    imagecopy($big_img,$small_img,$des_width,$des_height,0,0,$small_width,$small_height);    //展示    header("Content-Type:image/png");    imagepng($big_img);    //两张图片都要释放    imagedestroy($big_img);    imagedestroy($small_img);}------------图片文件可以任意指定,缩放尺寸可以任意指定------------<?php //图片的缩放的自定义函数//图片文件可以任意指定,缩放尺寸可以任意指定$filename = "t.jpg";$des_w = 100;$des_h = 100;thumb($filename,$des_w,$des_h);function thumb($filename,$des_w,$des_h){    //获取原图片的资源    //获取原图片的信息    $fileinfo = getimagesize($filename);    $width = $fileinfo[0];    $height = $fileinfo[1];    $type = $fileinfo[2]; //整数    $type_array = array(1=>"gif",2=>"jpeg",3=>"png");    $type_str = $type_array[$type];    //拼装函数    $fun_str = "imagecreatefrom";    $function = $fun_str.$type_str;    //原图片的资源    $src_img = $function($filename);    //目标图片资源 缩放后的图片资源    $des_img = imagecreatetruecolor($des_w,$des_h);    imagecopyresampled($des_img,$src_img,                       0,0,                       0,0,                       $des_w,$des_h,                       $width,$height);    //展示    header("Content-Type:image/png");    imagepng($des_img);    //释放资源    imagedestroy($des_img);    imagedestroy($src_img);}---------------------------------图片的旋转------------------<?php //图片的旋转//获取要旋转的图片资源$filename = "dog.jpg";$img = imagecreatefromjpeg($filename);//3、把图片旋转的功能封装到自定义函数中。//要求图片文件可以任意指定, 图片旋转角度可以任意指定。//旋转角度$angle = 30;//背景色$bgd_color = imagecolorallocate($img,0,255,0);$des_img = imagerotate($img,$angle,$bgd_color);//展示header("Content-Type:image/png");imagepng($des_img);//释放资源imagedestroy($des_img);imagedestroy($img);-----------------------把图片旋转的功能封装到自定义函数中--------<?php //图片的旋转/* * 3、把图片旋转的功能封装到自定义函数中。 * 要求图片文件可以任意指定, 图片旋转角度可以任意指定。 */$filename = "dog.jpg";$angle = 60;rotate($filename,$angle);function rotate($filename,$angle){    //获取图片的信息    $fileinfo = getimagesize($filename);    $type = $fileinfo[2];//整数    $type_array = array(1=>"gif",2=>"jpeg",3=>"png");    $type_str = $type_array[$type];    //拼装函数    $fun_str = "imagecreatefrom";    $function = $fun_str.$type_str;    //要旋转图片的资源    $img = $function($filename);    //创建背景色    $bgd_color =     imagecolorallocate($img,0,255,0);    $des_img = imagerotate($img,$angle,$bgd_color);    //展示    header("Content-Type:image/png");    imagepng($des_img);    //释放资源    imagedestroy($des_img);    imagedestroy($img);}----------------------------图片沿X轴翻转--------------------<?php //图片沿X轴翻转$filename = "dog.jpg";$src_img = imagecreatefromjpeg($filename);//获取原图片的信息$fileinfo = getimagesize($filename);$width = $fileinfo[0];$height = $fileinfo[1];//创建目标图片资源$des_img = imagecreatetruecolor($width,$height);for($i=0;$i<$width;$i++){    $des_x = $width-$i-1;    $des_y = 0;    $src_x = $i;    $src_y = 0;    $src_w = 1;    $src_h = $height;    imagecopy($des_img,$src_img,              $des_x,$des_y,              $src_x,$src_y,              $src_w,$src_h);}//展示header("Content-Type:image/png");imagepng($des_img);//释放资源imagedestroy($des_img);imagedestroy($src_img);-------------------------图片沿X轴翻转-自定义函数--------------<?php//图片沿X轴翻转-自定义函数$filename="qq.png";fz($filename);function fz($filename){    //获取图片的信息    $file_info=getimagesize($filename);    $width=$file_info[0];    $height=$file_info[1];    $type=$file_info[2];//返回的是对应的图片格式整数    //1 创建原图片资源      //开始拼接函数     $type_arr=array(1=>'gif',2=>'jpeg',3=>'png');     $src_type=$type_arr[$type];     $function="imagecreatefrom".$src_type;     //调用拼接的函数     $src_img=$function($filename);    //2 创建翻转的目标资源     $des_img=imagecreatetruecolor($width,$height);     for($i=0;$i<$width;$i++){         $des_x=$width-$i-1;         $des_y=0;         $src_x=$i;         $src_y=0;         $src_w=1;         $src_h=$height;         imagecopy($des_img,$src_img,             $des_x,$des_y,             $src_x,$src_y,             $src_w,$src_h);     }    //展示目标图片    header("Content-Type:image/png");    imagepng($des_img);    //释放两个资源文件    imagedestroy($src_img);    imagedestroy($des_img);}-------------------------图片沿y轴翻转-自定义函数--------------<?php //图片沿y轴翻转-自定义函数$filename = "t.jpg";fz($filename);function fz($filename){    //获取原图片的信息    list($width,$height,$type) = getimagesize($filename);    $type_array = array(1=>"gif",2=>"jpeg",3=>"png");    $type_str = $type_array[$type];    //拼装函数    $fun_str = "imagecreatefrom";    $function = $fun_str.$type_str;    //获取原图片资源    $src_img = $function($filename);    //获取目标图片资源    $des_img = imagecreatetruecolor($width,$height);    for($i=0;$i<$height;$i++){        $des_x = 0;        $des_y = $height-$i-1;        $src_x = 0;        $src_y = $i;        $src_w = $width;        $src_h = 1;        imagecopy($des_img,$src_img,                  $des_x,$des_y,                  $src_x,$src_y,                  $src_w,$src_h);    }    //展示    header("Content-Type:image/png");    imagepng($des_img);    //释放资源    imagedestroy($des_img);    imagedestroy($src_img);}
0 0
原创粉丝点击