php学习笔记4--php中GD2扩展库的学习总结

    关于php GD2扩展库,首先你要确定php环境已配置完成,才能进行接下来的操作,以下是我试验成功的几种题型:

GD2创建真彩图像:

<?php
header("Content-type:image/png");
$height=300;
$width=300;
$im =imagecreatetruecolor($width,$height);//创建真彩色的图像
$white =imagecolorallocate($im ,255,255,255);
$blue =imagecolorallocate($im ,0,0,64);
imagefill($im,0,0,$blue);//浅蓝色的背景
imageline($im,0,0,$width,$height,$white);//在图像上面画一条白线
imagestring($im,4,80,150,"PHP",$white);//写出白色的"PHP"

imagepng($im);
imagedestroy($im);
?>

(1)GD2画图之八卦的画法:

<?php
header("content-type:image/png");

$width=400;
$height=400;
$image=imagecreatetruecolor($width,$height);
$bgcolor=imagecolorallocate($image,255,153,0);
$black=imagecolorallocate($image,0,0,0);
$green=imagecolorallocate($image,0,255,0);
$white=imagecolorallocate($image,255,255,255);

imagefilledellipse($image,200,200,299,299,$black);
imagefilledarc($image,200,200,300,300,90,-90,$white,IMG_ARC_PIE);
imagefilledellipse($image,200,125,150,150,$black);
imagefilledellipse($image,200,275,150,150,$white);

imagefilledellipse($image,200,125,75,75,$white);
imagefilledellipse($image,200,275,75,75,$black);

imagestring($image,5,125,380,"http://www.baidu.com",$white);
imagepng($image);
imagedestroy($image);

?>

(2)GD2画图之滤镜效果:

<?php
header('content-type:image/jpeg');

$logo = imagecreatefromjpeg('i.jpg');
$ww = imagesx($logo);
$hh = imagesy($logo);


//imagefilter($logo,IMG_FILTER_GRAYSCALE);
//imagejpeg($logo);
//imagedestroy($logo);

$i = imagecreatetruecolor($ww*2,$hh);
imagecopy($i,$logo,0,0,0,0,$ww,$hh);

imagefilter($logo,IMG_FILTER_COLORIZE,255,0,255);
//imagefilter($logo,IMG_FILTER_NEGATE);  //将图像中所有颜色反转。
//imagefilter($logo,IMG_FILTER_MEAN_REMOVAL); //用平均移除法来达到轮廓效果。
//imagefilter($logo,IMG_FILTER_EDGEDETECT); //用边缘检测来突出图像的边缘。
//imagefilter($logo,IMG_FILTER_GRAYSCALE); //调节灰度,将图像转换为灰度的。
//imagefilter($logo,IMG_FILTER_EMBOSS);      //使图像浮雕化。
//imagefilter($logo,IMG_FILTER_SMOOTH,255);    //使图像更柔滑。用 arg1 设定柔滑级别。
imagecopy($i,$logo,$ww,0,0,0,$ww,$hh);

imagejpeg($i);
imagedestroy($i);

?>

(3)GD2画图之“logo图片加到另一张图片上”:

<?php
header("Content-type:image/png");
$image =imagecreatefromjpeg("img/s.jpg");
$watermark =imagecreatefrompng("img/2.png");
$width =imagesx($watermark);
$height = imagesy($watermark);
//将水印加到图片左上角
imagecopyresampled($image,$watermark,0,0,0,0,$width,$height,$width,$height);
imagejpeg($image,"images/s_water.jpg",100);
imagejpeg($image);
imagedestroy($image);
?>

(4)GD2画图之“将一群图片打上文字水印并另外生成一群不重名的图片”:


<?php
$files = scandir("img/a/");
foreach($files as $v){
    if($v=='.' || $v=='..'){
        continue;    
    }
    $i = imagecreatefromjpeg('img/a/'.$v);
    $c = imagecolorallocate($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    imagettftext($i,15,0,10,20,$c,'Fonts/h.ttf','教育培训版权所有');
    imagejpeg($i,'img/a/t_'.$v);
    imagedestroy($i);
}

?>

(5)GD2画图之“将汉字水印添加到图片上”:

<?php
header("Content-type: image/png");
$image =imagecreatefromjpeg("images/i.jpg");
$pink = imagecolorallocate($image,255,255,255);
//$font_file 字体的路径,视操作系统而定,可以是simhei.ttf(黑体)
//SIMKAI.TTF(楷体),SIMFANG.TTF(仿宋),SIMSUN.TTC(宋体&新宋体)等GD支持的中文字体
$font_file ="C:\WINDOWS\Fonts\msyhbd.ttf";
$str ="hello!中国 >_< ";
//$str =mb_convert_encoding($str,"GBK","UTF-8");
imagettftext($image,25,10,100,200,$pink,$font_file,$str);//设置字体颜色
imagejpeg($image,"images/i_text.jpg",100);//将带有文字的图片保存到文件夹
imagejpeg($image);
imagedestroy($image);
?>

(6)GD2画图之“缩略图--宽度一定、高度按比例缩放”:

<?php
header('content-type:image/jpeg');

$logo = imagecreatefromjpeg('i.jpg');
$ww = imagesx($logo);
$hh = imagesy($logo);

$w = 150;
$h = $w/$ww * $hh;
$i = imagecreatetruecolor($w,$h);

imagecopyresampled($i,$logo,0,0,0,0,$w,$h,$ww,$hh);
imagejpeg($i,'t_i.jpg');
imagejpeg($i);
imagedestroy($i);

?>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值