PHP-GD库开发手记
创建带有alpha通道的背景
$png = imagecreatetruecolor(800, 600);
imagesavealpha($png, true);
$trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127);
imagefill($png, 0, 0, $trans_colour);
输出图像
$png = imagecreatetruecolor(800, 600);
imagesavealpha($png, true);
$trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127);
imagefill($png, 0, 0, $trans_colour);
根据不同的图片格式,选择不同的函数
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
画中文字体
用ttftext函数即可,如果不是UTF8的编码,记得用iconv函数转码一次
imagettftext ( $img, 20, 0, 0, 20, imagecolorallocate ( $img, 255, 255, 255 ),'g:/msyhbd.ttf', '啊啊啊啊啊啊啊啊啊啊啊啊啊');
获取长宽
imagesx($tx);
imagesy($tx);
等比例缩放图片,也可以用于裁切
$tx=imagecreatefromjpeg("G:/test.jpg" );
$scaletx=imagecreatetruecolor(450, 450);
//意思表示将tx图片的从0,0坐标长sx,宽sy的区域,重新采样改变大小复制到stx的0,0坐标,长宽为450的区域。
imagecopyresampled($scaletx, $tx, 0, 0, 0, 0, 450, 450, imagesx($tx), imagesy($tx));
private function resizeImage($src, $dest, $thumbWidth)
{
ini_set('memory_limit', '2048M');
if (!file_exists($src)) {
return;
}
$image = imagecreatefrompng($src);
$srcWidth = imagesx($image); // 原始宽高
$srcHeight = imagesy($image);
$thumbHeight = ($srcHeight / $srcWidth) * $thumbWidth; // 处理后的高
imagesavealpha($image, true);//这里很重要;
$new = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagealphablending($new, false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
imagesavealpha($new, true);//这里很重要,意思是不要丢了$thumb图像的透明色;
imagecopyresampled($new, $image, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $srcWidth, $srcHeight);
imagepng($new, $dest);
imagedestroy($new);
}
实例代码
$img = imagecreatetruecolor ( 720, 1280 );
imagesavealpha ( $img, true );//保留通道,不然不透明
$trans_colour = imagecolorallocatealpha ( $img, 33, 0, 0, 33 );
imagefill ( $img, 0, 0, $trans_colour );
$tx = imagecreatefromjpeg ( "G:/Lotus.jpg" );
// 设置头像居中
$width = 450;
$height = 450;
$w = imagesx ( $tx );
$h = imagesy ( $tx );
// 原图尺寸小于缩略图尺寸则不进行缩略
if ($w > $width || $h > $height) {
// 计算缩放比例,选择优先让小的边放大到指定等比宽度,大的边随意,为了充满屏幕
$scale = max ( $width / $w, $height / $h );
$width = $w * $scale;
$height = $h * $scale;
}
//缩放
$scaletx = imagecreatetruecolor ( $width, $height );
imagecopyresampled ( $scaletx, $tx, 0, 0, 0, 0, $width, $height, $w, $h );
// 计算居中位置
$cx = (720 - $width) / 2;
$cy = (1280 - $height) / 2 - 170;
//覆盖
imagecopy ( $img, $scaletx, $cx, $cy, 0, 0, $width, $height );
$bg = imagecreatefrompng ( "G:/test.png" );
imagesavealpha ( $bg, true );
imagecopy ( $img, $bg, 0, 0, 0, 0, imagesx ( $bg ), imagesy ( $bg ) );
// 写名字
$name = '李昕';
$name_len = mb_strlen ( $name, 'utf8' );// 计算名字长度
$name_y =510-(45*$name_len/2);//居中
for($i = 0; $i < $name_len; $i ++) {
imagettftext ( $img, 30, 0, 630, $name_y, imagecolorallocate ( $img, 255, 255, 255 ), 'msyhbd.ttf', mb_substr ( $name, $i, 1,'utf-8' ));
$name_y += 45;
}
//写编号
$vcode='01002';
imagettftext ( $img, 20, 0, 560, 1050, imagecolorallocate ( $img, 232, 255, 0 ), 'msyhbd.ttf', $vcode);
header ( "content-type: image/jpg" );
imagejpeg ( $img, null, 100 );
imagesx($tx);
imagesy($tx);
$tx=imagecreatefromjpeg("G:/test.jpg" );
$scaletx=imagecreatetruecolor(450, 450);
//意思表示将tx图片的从0,0坐标长sx,宽sy的区域,重新采样改变大小复制到stx的0,0坐标,长宽为450的区域。
imagecopyresampled($scaletx, $tx, 0, 0, 0, 0, 450, 450, imagesx($tx), imagesy($tx));
private function resizeImage($src, $dest, $thumbWidth)
{
ini_set('memory_limit', '2048M');
if (!file_exists($src)) {
return;
}
$image = imagecreatefrompng($src);
$srcWidth = imagesx($image); // 原始宽高
$srcHeight = imagesy($image);
$thumbHeight = ($srcHeight / $srcWidth) * $thumbWidth; // 处理后的高
imagesavealpha($image, true);//这里很重要;
$new = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagealphablending($new, false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
imagesavealpha($new, true);//这里很重要,意思是不要丢了$thumb图像的透明色;
imagecopyresampled($new, $image, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $srcWidth, $srcHeight);
imagepng($new, $dest);
imagedestroy($new);
}
实例代码
$img = imagecreatetruecolor ( 720, 1280 );
imagesavealpha ( $img, true );//保留通道,不然不透明
$trans_colour = imagecolorallocatealpha ( $img, 33, 0, 0, 33 );
imagefill ( $img, 0, 0, $trans_colour );
$tx = imagecreatefromjpeg ( "G:/Lotus.jpg" );
// 设置头像居中
$width = 450;
$height = 450;
$w = imagesx ( $tx );
$h = imagesy ( $tx );
// 原图尺寸小于缩略图尺寸则不进行缩略
if ($w > $width || $h > $height) {
// 计算缩放比例,选择优先让小的边放大到指定等比宽度,大的边随意,为了充满屏幕
$scale = max ( $width / $w, $height / $h );
$width = $w * $scale;
$height = $h * $scale;
}
//缩放
$scaletx = imagecreatetruecolor ( $width, $height );
imagecopyresampled ( $scaletx, $tx, 0, 0, 0, 0, $width, $height, $w, $h );
// 计算居中位置
$cx = (720 - $width) / 2;
$cy = (1280 - $height) / 2 - 170;
//覆盖
imagecopy ( $img, $scaletx, $cx, $cy, 0, 0, $width, $height );
$bg = imagecreatefrompng ( "G:/test.png" );
imagesavealpha ( $bg, true );
imagecopy ( $img, $bg, 0, 0, 0, 0, imagesx ( $bg ), imagesy ( $bg ) );
// 写名字
$name = '李昕';
$name_len = mb_strlen ( $name, 'utf8' );// 计算名字长度
$name_y =510-(45*$name_len/2);//居中
for($i = 0; $i < $name_len; $i ++) {
imagettftext ( $img, 30, 0, 630, $name_y, imagecolorallocate ( $img, 255, 255, 255 ), 'msyhbd.ttf', mb_substr ( $name, $i, 1,'utf-8' ));
$name_y += 45;
}
//写编号
$vcode='01002';
imagettftext ( $img, 20, 0, 560, 1050, imagecolorallocate ( $img, 232, 255, 0 ), 'msyhbd.ttf', $vcode);
header ( "content-type: image/jpg" );
imagejpeg ( $img, null, 100 );
$img = imagecreatetruecolor ( 720, 1280 );
imagesavealpha ( $img, true );//保留通道,不然不透明
$trans_colour = imagecolorallocatealpha ( $img, 33, 0, 0, 33 );
imagefill ( $img, 0, 0, $trans_colour );
$tx = imagecreatefromjpeg ( "G:/Lotus.jpg" );
// 设置头像居中
$width = 450;
$height = 450;
$w = imagesx ( $tx );
$h = imagesy ( $tx );
// 原图尺寸小于缩略图尺寸则不进行缩略
if ($w > $width || $h > $height) {
// 计算缩放比例,选择优先让小的边放大到指定等比宽度,大的边随意,为了充满屏幕
$scale = max ( $width / $w, $height / $h );
$width = $w * $scale;
$height = $h * $scale;
}
//缩放
$scaletx = imagecreatetruecolor ( $width, $height );
imagecopyresampled ( $scaletx, $tx, 0, 0, 0, 0, $width, $height, $w, $h );
// 计算居中位置
$cx = (720 - $width) / 2;
$cy = (1280 - $height) / 2 - 170;
//覆盖
imagecopy ( $img, $scaletx, $cx, $cy, 0, 0, $width, $height );
$bg = imagecreatefrompng ( "G:/test.png" );
imagesavealpha ( $bg, true );
imagecopy ( $img, $bg, 0, 0, 0, 0, imagesx ( $bg ), imagesy ( $bg ) );
// 写名字
$name = '李昕';
$name_len = mb_strlen ( $name, 'utf8' );// 计算名字长度
$name_y =510-(45*$name_len/2);//居中
for($i = 0; $i < $name_len; $i ++) {
imagettftext ( $img, 30, 0, 630, $name_y, imagecolorallocate ( $img, 255, 255, 255 ), 'msyhbd.ttf', mb_substr ( $name, $i, 1,'utf-8' ));
$name_y += 45;
}
//写编号
$vcode='01002';
imagettftext ( $img, 20, 0, 560, 1050, imagecolorallocate ( $img, 232, 255, 0 ), 'msyhbd.ttf', $vcode);
header ( "content-type: image/jpg" );
imagejpeg ( $img, null, 100 );
PHP-GD库开发手记的更多相关文章
- (转)php中GD库的配置,解决dedecms安装中GD不支持问题
了解gd库 在php中,使用gd库来对图像进行操作,gd库是一个开放的动态创建的图像的源代码公开的函数库,可以从官方网站http://www.boutell.com/gd处下载.目前,gd库支持gif ...
- (转)本地搭建环境wamp下提示不支持GD库的解决方法
转自:http://www.zzdp.net/local-wamp-gd GD库是什么?GD库,是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片. ...
- cenos 上的php 支持GD库问题
---恢复内容开始--- thinkphp 开发的项目verify类无法引用,原因是没有开启gd库 环境:CentOS 6.4,php-5.3.3需求:php支持GD库解决方案:GD是Linux下的一 ...
- [转]Nodejs开发框架Express4.x开发手记
Express: ?web application framework for?Node.js? Express 是一个简洁.灵活的 node.js Web 应用开发框架, 它提供一系列强大的特性,帮 ...
- 一起学习PHP中GD库的使用(三)
上篇文章我们已经学习了一个 GD 库的应用,那就是非常常用的制作验证码的功能.不过在现实的业务开发中,这种简单的验证码已经使用得不多了,大家会制作出更加复杂的验证码来使用.毕竟现在的各种外挂软件已经能 ...
- Doris开发手记4:倍速性能提升,向量化导入的性能调优实践
最近居家中,对自己之前做的一些工作进行总结.正好有Doris社区的小伙伴吐槽向量化的导入性能表现并不是很理想,就借这个机会对之前开发的向量化导入的工作进行了性能调优,取得了不错的优化效果.借用本篇手记 ...
- PHP的GD库
GD库 PHP通过GD库,可以对JPG.PNG.GIF.SWF等图片进行处理.GD库常用在图片加水印,验证码生成等方面. 绘制线条 要对图形进行操作,首先要新建一个画布,通过imagecreatetr ...
- gd库
1.开启GD库扩展 去掉注释: extension=php_gd2.dll extension_dir='ext目录所在位置' 2.检测GD库是否开启 phpinfo(); //检测扩展是够开启 ex ...
- 已安装php 编译安装 gd库拓展模块
参考资料:http://wenku.baidu.com/link?url=EgXFShYxeJOZSYNQ_7RCBC-6X8OcRRCqVm4qCv49uBk57d6vLBoUpfYdQ-KqJRs ...
随机推荐
- 自定义beans.xml文件实现Spring框架
经过一天的补习,学习文件加载,java反射,JDom等知识,到了晚上终于能够搭出一个基于配置文件的简单spring框架实现! 首先我们先看看这个问题: 下面是两副图左边是项目结构图,右边是UML图: ...
- Set List Map
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 2016-2017-2 20155223 实验二 《Java面向对象程序设计》
2016-2017-2 苏黄永郦 实验二 实验报告 前期准备工作--程序安装 -问题一 开始的时候我就在老师博客的指导下安装IDEA插件JUnit Generator V2.0.当然我的IDEA肯定没 ...
- java中父类的静态方法不能被重写
Java中父类的静态方法确实不能被重写的,但是有的人可能去做实验发现在子类中去重写父类static方法时,并没什么问题.这里我来具体解释下. public class Parent { public ...
- ASP.NET Core2读写InfluxDB时序数据库
在我们很多应用中会遇到有一种基于一系列时间的数据需要处理,通过时间的顺序可以将这些数据点连成线,再通过数据统计后可以做成多纬度的报表,也可通过机器学习来实现数据的预测告警.而时序数据库就是用于存放管理 ...
- IPA文件的自动化生成和无线分发
1. IPA的无线分发 iOS应用开发测试过程中,通过无线网络进行IPA包的分发将是非常便捷的,于是也就有了类似testflightapp之类的平台.对于这一功能,我们也可以自己实现,只需要一个简单的 ...
- Linux Redis 开机启动
通过初始化脚本启动Redis 在Redis源代码目录的utils文件夹中有一个名为redis_init_script的初始化脚本文件.需要配置Redis的运行方式和持久化文件.日志文件的存储位置.步骤 ...
- solrconfig.xml配置详解
solrconfig.xml配置文件主要定义了SOLR的一些处理规则,包括索引数据的存放位置,更新,删除,查询的一些规则配置. 可以在tomcat的安装路径下找到这个文件C:\Program File ...
- svn自动更新服务器最新代码
1.很简单打开dos界面 cd到svn exe目录下,运行 cd C:\Program Files\TortoiseSVN\bin --svn安装目录(作者使用时TortoiseSVN客户端,其 ...
- 不写代码也能爬虫Web Scraper
https://www.jianshu.com/p/d0a730464e0c web scraper中文网 http://www.iwebscraper.com/category/%E6%95%99% ...