-----------------------------------GD库基础1----------------
<?php
$img = imagecreatetruecolor(200,200);
$red = imagecolorallocate($img,255,0,0);
imagefill($img,0,0,$red);
header("Content-Type:image/png");
imagepng($img);
imagedestroy($img);
-----------------------------------------------------------
<?php
$img = imagecreatetruecolor(200,200);
$red = imagecolorallocate($img,255,0,0);
imagefill($img,0,0,$red);
header("Content-Type:image/png");
imagepng($img);
imagedestroy($img);
---------------------------在画布上画随机的点-----------------
<?php
$img = imagecreatetruecolor(200,200);
$green = imagecolorallocate($img,0,255,0);
$red = imagecolorallocate($img,255,0,0);
imagefill($img,0,0,$red);
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
$img = imagecreatetruecolor(200,200);
$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
$img = imagecreatetruecolor(200,200);
$green = imagecolorallocate($img,0,255,0);
$blue = imagecolorallocate($img,0,0,255);
imagefill($img,0,0,$green);
imagerectangle($img,50,50,100,100,$blue);
imagefilledrectangle($img,100,100,150,150,
$blue);
header("Content-Type:image/png");
imagepng($img);
imagedestroy($img);
---------------------------------画一个实心圆----------------
<?php
$img = imagecreatetruecolor(300,300);
$red = imagecolorallocate($img,255,0,0);
$blue = imagecolorallocate($img,0,0,255);
imagefill($img,0,0,$red);
imageellipse($img,150,150,100,100,$blue);
header("Content-Type:image/png");
imagepng($img);
imagedestroy($img);
----------------------------------在画布上输出文字------------
<?php
$img = imagecreatetruecolor(200,200);
$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
$img = imagecreatetruecolor(200,200);
$blue = imagecolorallocate($img,0,0,255);
$green = imagecolorallocate($img,0,255,0);
imagefill($img,0,0,$blue);
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);
}
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);
}
header("Content-Type:image/png");
imagepng($img);
imagedestroy($img);
-----------------------------------------------------------
<?php
$filename = "t.jpg";
$img = imagecreatefromjpeg($filename);
$red = imagecolorallocate($img,255,0,0);
imagestring($img,5,20,20,"hello",$red);
header("Content-Type:image/png");
imagepng($img);
imagedestroy($img);
-------------------------------------GD库基础2--------------
<?php
$filename = "t.jpg";
$img = imagecreatefromjpeg($filename);
$red = imagecolorallocate($img,255,0,0);
imagestring($img,5,20,20,"hello",$red);
header("Content-Type:image/png");
imagepng($img);
imagedestroy($img);
-----------------------------------------
<?php
$filename = "t.jpg";
$fileinfo = getimagesize($filename);
$width = $fileinfo[0];
$height = $fileinfo[1];
$img = imagecreatefromjpeg($filename);
$red = imagecolorallocate($img,255,0,0);
$str_w = imagefontwidth(5)*strlen("hello");
$x = $width-($str_w+10);
$str_h = imagefontheight(5);
$y = $height-($str_h+10);
imagestring($img,5,$x,$y,"hello",$red);
header("Content-Type:image/png");
imagepng($img);
imagedestroy($img);
------在现有图片上输出文字 hello------------
<?php
$filename = "t.jpg";
$fileinfo = getimagesize($filename);
$width = $fileinfo[0];
$height = $fileinfo[1];
$img = imagecreatefromjpeg($filename);
$red = imagecolorallocate($img,255,0,0);
$str_w = imagefontwidth(5)*strlen("hello");
$x = $width-($str_w+10);
$str_h = imagefontheight(5);
$y = $height-($str_h+10);
imagestring($img,5,$x,$y,"hello",$red);
header("Content-Type:image/png");
imagepng($img);
imagedestroy($img);
-------------------------将两张图片合成一张图片---------------
<?php
$filename = "dog.jpg";
$des_img = imagecreatefromjpeg($filename);
$watermark_file = "qq.png";
$src_img = imagecreatefrompng($watermark_file);
$des_x = 300;
$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];
$des_x = $width-($w_width+10);
$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();
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);
}
--------------将两张图片合成一张图片(封装成自定义函数-整理版)------
<?php
waterMark();
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";
$big_type_arr=$type_arr[$big_type];
$big_function=$func_str.$big_type_arr;
$big_img=$big_function($filename);
$small_type_arr=$type_arr[$small_type];
$small_function=$func_str.$small_type_arr;
$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);
$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
$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
$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
$filename="qq.png";
fz($filename);
function fz($filename){
$file_info=getimagesize($filename);
$width=$file_info[0];
$height=$file_info[1];
$type=$file_info[2];
$type_arr=array(1=>'gif',2=>'jpeg',3=>'png');
$src_type=$type_arr[$type];
$function="imagecreatefrom".$src_type;
$src_img=$function($filename);
$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
$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);
}