zoukankan      html  css  js  c++  java
  • PHP-GD库开发手记

    创建带有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常用操作(第二版)
    mysql操作遇到的坑(第一版)
    php常用函数(第一版)
    ThinkPHP5实现定时任务
    mysql高级扩展
    laravel基础操作手册
    初次接触python时,整理的一些基础操作
    后端技术杂谈1:搜索引擎基础倒排索引
    搞懂分布式技术21:浅谈分布式消息技术 Kafka
    搞懂分布式技术20:消息队列因何而生
  • 原文地址:https://www.cnblogs.com/leestar54/p/7411757.html
Copyright © 2011-2022 走看看