创建带有alpha通道的背景

  1. $png = imagecreatetruecolor(800, 600);
  2. imagesavealpha($png, true);
  3. $trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127);
  4. imagefill($png, 0, 0, $trans_colour);

输出图像

根据不同的图片格式,选择不同的函数

  1. header("Content-type: image/png");
  2. imagepng($img);
  3. imagedestroy($img);

画中文字体

用ttftext函数即可,如果不是UTF8的编码,记得用iconv函数转码一次

  1. imagettftext ( $img, 20, 0, 0, 20, imagecolorallocate ( $img, 255, 255, 255 ),'g:/msyhbd.ttf', '啊啊啊啊啊啊啊啊啊啊啊啊啊');

获取长宽

  1. imagesx($tx);
  2. imagesy($tx);

等比例缩放图片,也可以用于裁切

  1. $tx=imagecreatefromjpeg("G:/test.jpg" );
  2. $scaletx=imagecreatetruecolor(450, 450);
  3. //意思表示将tx图片的从0,0坐标长sx,宽sy的区域,重新采样改变大小复制到stx的0,0坐标,长宽为450的区域。
  4. imagecopyresampled($scaletx, $tx, 0, 0, 0, 0, 450, 450, imagesx($tx), imagesy($tx));
  1. private function resizeImage($src, $dest, $thumbWidth)
  2. {
  3. ini_set('memory_limit', '2048M');
  4. if (!file_exists($src)) {
  5. return;
  6. }
  7. $image = imagecreatefrompng($src);
  8. $srcWidth = imagesx($image); // 原始宽高
  9. $srcHeight = imagesy($image);
  10. $thumbHeight = ($srcHeight / $srcWidth) * $thumbWidth; // 处理后的高
  11. imagesavealpha($image, true);//这里很重要;
  12. $new = imagecreatetruecolor($thumbWidth, $thumbHeight);
  13. imagealphablending($new, false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
  14. imagesavealpha($new, true);//这里很重要,意思是不要丢了$thumb图像的透明色;
  15. imagecopyresampled($new, $image, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $srcWidth, $srcHeight);
  16. imagepng($new, $dest);
  17. imagedestroy($new);
  18. }

实例代码

  1. $img = imagecreatetruecolor ( 720, 1280 );
  2. imagesavealpha ( $img, true );//保留通道,不然不透明
  3. $trans_colour = imagecolorallocatealpha ( $img, 33, 0, 0, 33 );
  4. imagefill ( $img, 0, 0, $trans_colour );
  5. $tx = imagecreatefromjpeg ( "G:/Lotus.jpg" );
  6. // 设置头像居中
  7. $width = 450;
  8. $height = 450;
  9. $w = imagesx ( $tx );
  10. $h = imagesy ( $tx );
  11. // 原图尺寸小于缩略图尺寸则不进行缩略
  12. if ($w > $width || $h > $height) {
  13. // 计算缩放比例,选择优先让小的边放大到指定等比宽度,大的边随意,为了充满屏幕
  14. $scale = max ( $width / $w, $height / $h );
  15. $width = $w * $scale;
  16. $height = $h * $scale;
  17. }
  18. //缩放
  19. $scaletx = imagecreatetruecolor ( $width, $height );
  20. imagecopyresampled ( $scaletx, $tx, 0, 0, 0, 0, $width, $height, $w, $h );
  21. // 计算居中位置
  22. $cx = (720 - $width) / 2;
  23. $cy = (1280 - $height) / 2 - 170;
  24. //覆盖
  25. imagecopy ( $img, $scaletx, $cx, $cy, 0, 0, $width, $height );
  26. $bg = imagecreatefrompng ( "G:/test.png" );
  27. imagesavealpha ( $bg, true );
  28. imagecopy ( $img, $bg, 0, 0, 0, 0, imagesx ( $bg ), imagesy ( $bg ) );
  29. // 写名字
  30. $name = '李昕';
  31. $name_len = mb_strlen ( $name, 'utf8' );// 计算名字长度
  32. $name_y =510-(45*$name_len/2);//居中
  33. for($i = 0; $i < $name_len; $i ++) {
  34. imagettftext ( $img, 30, 0, 630, $name_y, imagecolorallocate ( $img, 255, 255, 255 ), 'msyhbd.ttf', mb_substr ( $name, $i, 1,'utf-8' ));
  35. $name_y += 45;
  36. }
  37. //写编号
  38. $vcode='01002';
  39. imagettftext ( $img, 20, 0, 560, 1050, imagecolorallocate ( $img, 232, 255, 0 ), 'msyhbd.ttf', $vcode);
  40. header ( "content-type: image/jpg" );
  41. imagejpeg ( $img, null, 100 );

PHP-GD库开发手记的更多相关文章

  1. (转)php中GD库的配置,解决dedecms安装中GD不支持问题

    了解gd库 在php中,使用gd库来对图像进行操作,gd库是一个开放的动态创建的图像的源代码公开的函数库,可以从官方网站http://www.boutell.com/gd处下载.目前,gd库支持gif ...

  2. (转)本地搭建环境wamp下提示不支持GD库的解决方法

    转自:http://www.zzdp.net/local-wamp-gd GD库是什么?GD库,是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片. ...

  3. cenos 上的php 支持GD库问题

    ---恢复内容开始--- thinkphp 开发的项目verify类无法引用,原因是没有开启gd库 环境:CentOS 6.4,php-5.3.3需求:php支持GD库解决方案:GD是Linux下的一 ...

  4. [转]Nodejs开发框架Express4.x开发手记

    Express: ?web application framework for?Node.js? Express 是一个简洁.灵活的 node.js Web 应用开发框架, 它提供一系列强大的特性,帮 ...

  5. 一起学习PHP中GD库的使用(三)

    上篇文章我们已经学习了一个 GD 库的应用,那就是非常常用的制作验证码的功能.不过在现实的业务开发中,这种简单的验证码已经使用得不多了,大家会制作出更加复杂的验证码来使用.毕竟现在的各种外挂软件已经能 ...

  6. Doris开发手记4:倍速性能提升,向量化导入的性能调优实践

    最近居家中,对自己之前做的一些工作进行总结.正好有Doris社区的小伙伴吐槽向量化的导入性能表现并不是很理想,就借这个机会对之前开发的向量化导入的工作进行了性能调优,取得了不错的优化效果.借用本篇手记 ...

  7. PHP的GD库

    GD库 PHP通过GD库,可以对JPG.PNG.GIF.SWF等图片进行处理.GD库常用在图片加水印,验证码生成等方面. 绘制线条 要对图形进行操作,首先要新建一个画布,通过imagecreatetr ...

  8. gd库

    1.开启GD库扩展 去掉注释: extension=php_gd2.dll extension_dir='ext目录所在位置' 2.检测GD库是否开启 phpinfo(); //检测扩展是够开启 ex ...

  9. 已安装php 编译安装 gd库拓展模块

    参考资料:http://wenku.baidu.com/link?url=EgXFShYxeJOZSYNQ_7RCBC-6X8OcRRCqVm4qCv49uBk57d6vLBoUpfYdQ-KqJRs ...

随机推荐

  1. 20155317 2016-2017-2 《Java程序设计》第7周学习总结

    20155317 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 1.在只有Lambda表达式的情况下,参数的类型必须写出来. 2.Lambda表达式本身是中 ...

  2. Excel应用程序如何创建数据透视表

    原文作者:andreww 原文链接: http://blogs.msdn.com/andreww/archive/2008/07/25/creating-a-pivottable-programmat ...

  3. oracle 11g 创建普通用户

    CREATE USER xiaoming IDENTIFIED by xm123123 GRANT CREATE SESSION TO xiaoming; GRANT RESOURCE TO xiao ...

  4. VS2015安装失败

    [16D4:18C8][2017-06-24T13:44:01]e000: Error 0x80091007: Hash mismatch for path: D:\Visual Studio 201 ...

  5. Codeforces 766D Mahmoud and a Dictionary 2017-02-21 14:03 107人阅读 评论(0) 收藏

    D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input ...

  6. [leetcode] 7. Binary Tree Level Order Traversal II

    这次相对来讲复杂点,题目如下: Given a binary tree, return the bottom-up level order traversal of its nodes' values ...

  7. [转载]利用近场探头和频谱仪查找EMI辐射问题

    原文链接 http://www.pesmatrix.com/news/html/?412.html 电磁兼容性(Electromagnetic Compatibility,简称EMC)是指设备或系统在 ...

  8. PipelineDB On Kafka

    PipelineDB 安装yum install https://s3-us-west-2.amazonaws.com/download.pipelinedb.com/pipelinedb-0.9.8 ...

  9. 搭建自己的 Docker 私有仓库服务

    关于 Docker 的介绍这里就省了,Docker 在其相关领域的火爆程度不亚于今年汽车行业里的特斯拉,docCloud 甚至把公司名都改成了 Docker, Inc. 好东西总是传播很快,我们现在已 ...

  10. 在 Docker 中部署 ASP.NET CORE 应用

    有了 Docker 之后, 部署起来却这间非常方便,环境不用搭了, 直接创建一个 microsoft/aspnetcore 的容器, 在本地开发好后, 把内容直接部署到容器中. 下面的命令是把本地发布 ...