合成图片方法

<?php
class Share {/** 生成分享图片* */function cre_share_study_img(){$auth = json_decode(base64_decode($_GET['auth']),true);$url = trim(strip_tags($auth['url']));$uid = max(0,strip_tags($auth['id'])); //120618218;if(empty($url) || !$uid){exit();}$userInfo = get_user_info($uid);if(empty($userInfo) ){exit();}$avatar = $userInfo['avatar'];$nickname = $userInfo['nickname'];$bg_img = ROOT_PATH.'/static/study/study_bg.png';$imgArr=getimagesize($bg_img);$bg_width = $imgArr[0];$required_memory = $imgArr[0] * $imgArr[1] * $imgArr['bits'];$new_limit=memory_get_usage() + $required_memory + 20000000;ini_set("memory_limit", $new_limit);$image = imagecreatefrompng($bg_img);$black = imagecolorallocate($image, 61, 61, 61); // 黑色$font = ROOT_PATH.'/static/redpacket/msyh.ttc';$text = "{$nickname} 的书屋";$text_len = (strlen($text) + mb_strlen($text,'utf-8')) / 2;$text_begin = ($bg_width-($text_len*16))/2;imagettftext($image, 26, 0, $text_begin, 380, $black, $font,$text);// 头像$is_icon = $this->put_file_from_url_content($avatar,ROOT_PATH.'/static/study/user_temp.png');if($is_icon){ // 保存网络图片成功执行合并头像$icon_img = $this->radius_img($is_icon);header('Content-type: image/png;');$is_icon_png = ROOT_PATH.'/static/study/user.png';imagepng($icon_img,$is_icon_png);imagedestroy($icon_img);$imgsarr = getimagesize($is_icon_png);$icon_width = $imgsarr[0];if (strstr($imgsarr['mime'], 'jpeg')){$icon_img = imagecreatefromjpeg($is_icon_png);}else{$icon_img = imagecreatefrompng($is_icon_png);}unlink($is_icon);unlink($is_icon_png);$avatarWidth = 155;imagecopyresampled($image, $icon_img, ($bg_width-$avatarWidth)/2, 165, 0, 0, $avatarWidth, $avatarWidth, $icon_width, $icon_width);//合成头像到}//合成二维码到$qrcode = $this->cre_qrcode($url);$imgsarr = getimagesize($qrcode);$icon_width = $imgsarr[0];$qrcode = imagecreatefrompng($qrcode);$code_img_w = 360;imagecopyresampled($image, $qrcode, ($bg_width-$code_img_w)/2, 595, 0, 0, $code_img_w, $code_img_w,$icon_width, $icon_width);header('Content-type: image/png;');imagepng($image);imagedestroy($image);ini_restore ("memory_limit");}// 生成二维码function cre_qrcode($url='',$tmp_qr = ROOT_PATH.'/static/study/qr_code.png'){vendor('Phcode.phpqrcode');$errorCorrectionLevel = 3;//容错级别$matrixPointSize = 10;//生成图片大小//生成二维码图片QRcode::png($url, $tmp_qr, $errorCorrectionLevel, $matrixPointSize, 2);return $tmp_qr;}// 把图片转换成 圆形private function radius_img($imgpath = './public/qrcode/qrcode.jpg', $radius = 0){$ext = pathinfo($imgpath);$src_img = null;switch ($ext['extension']){case 'jpg':$src_img = imagecreatefromjpeg($imgpath);break;case 'png':$src_img = imagecreatefrompng($imgpath);break;case 'gif':$src_img = imagecreatefromgif($imgpath);break;}$wh = getimagesize($imgpath);$w = $wh[0];$h = $wh[1];$radius = $radius == 0 ? (min($w, $h) / 2) : $radius;$img = imagecreatetruecolor($w, $h);//这一句一定要有imagesavealpha($img, true);//拾取一个完全透明的颜色,最后一个参数127为全透明$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);imagefill($img, 0, 0, $bg);$r = $radius; //圆 角半径for ($x = 0; $x < $w; $x++){for ($y = 0; $y < $h; $y++){$rgbColor = imagecolorat($src_img, $x, $y);if (($x >= $radius && $x <= ($w - $radius)) || ($y >= $radius && $y <= ($h - $radius))){//不在四角的范围内,直接画imagesetpixel($img, $x, $y, $rgbColor);}else{//在四角的范围内选择画//上左$y_x = $r; //圆心X坐标$y_y = $r; //圆心Y坐标if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))){imagesetpixel($img, $x, $y, $rgbColor);}//上右$y_x = $w - $r; //圆心X坐标$y_y = $r; //圆心Y坐标if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))){imagesetpixel($img, $x, $y, $rgbColor);}//下左$y_x = $r; //圆心X坐标$y_y = $h - $r; //圆心Y坐标if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))){imagesetpixel($img, $x, $y, $rgbColor);}//下右$y_x = $w - $r; //圆心X坐标$y_y = $h - $r; //圆心Y坐标if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))){imagesetpixel($img, $x, $y, $rgbColor);}}}}return $img;}/*** 异步将远程链接上的内容(图片或内容)写到本地* @param unknown $url 远程地址* @param unknown $saveName保存在服务器上的文件名* @param unknown $path保存路径* @return boolean*/function put_file_from_url_content($url='',$path='/static/study/user_temp') {//方法一://推荐用该方法$header = array('User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0','Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3','Accept-Encoding: gzip, deflate',);
//        $url='http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKkGpNuUhaBniatRsiaG7ksqmhUWzkk40kTRS6icQS7kJcsfxcibQo7vDFcKibr7NHb9YIXiaXsEtLcdL6A/0';$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);curl_setopt($curl, CURLOPT_ENCODING, 'gzip');curl_setopt($curl, CURLOPT_HTTPHEADER, $header);$data = curl_exec($curl);$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);curl_close($curl);if ($code == 200) {//把URL格式的图片转成base64_encode格式的!$imgType = 'image/jpeg';if(function_exists('get_headers')){$imghttp = get_headers($url,true);$imgType = $imghttp['Content-Type'];};$imgBase64Code = "data:{$imgType};base64," . base64_encode($data);$img_content = $imgBase64Code;//图片内容if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img_content, $result)) {$type = $result[2];//得到图片类型png?jpg?gif?if($type == 'jpeg'){$type = 'jpg';}$path = "{$path}.{$type}";if (file_put_contents($path, base64_decode(str_replace($result[1], '', $img_content)))) {return $path;}}}return false;}
}

  

注意 背景图片,字体,二维码图片,头像图片都必须先存在

php 合成图片,合成圆形图片相关推荐

  1. Android裁剪图片为圆形图片

    Android裁剪图片为圆形图片 http://blog.csdn.net/kkmike999/article/details/16359713 2013-11-16 19:15  3234人阅读  ...

  2. 使用Glide加载圆角矩形图片、圆形图片

    Glide是一个功能强大的图片加载库,下面是平常开发中使用到的一些功能,基于Glide-4.X版本的用法 Glide-github地址 Glide jar包下载地址 加载圆形图片: RequestOp ...

  3. Glide加载常用类型图片(带白色边框的圆形图、圆角矩形图片、圆形图片)

    一:依赖包: Glide_github implementation 'com.github.bumptech.glide:glide:4.8.0' annotationProcessor 'com. ...

  4. Python 将矩形图片转为圆形图片

    使用PIL库将矩形图片转为原型图片 from PIL import Image, ImageDraw, ImageFilterdef crop_max_square(pil_img):return c ...

  5. android圆角glide,使用Glide加载圆角矩形图片、圆形图片

    Glide是一个功能强大的图片加载库,下面是平常开发中使用到的一些功能,基于Glide-4.X版本的用法 Glide-github地址 Glide jar包下载地址 加载圆形图片:RequestOpt ...

  6. Android 心形图片心形ImageView、带边框的的心形图片和圆形图片

    源码地址:心形图片.心形ImageView.圆形图片.圆形ImageView 实际效果如下: 一.思路 1.图片尺寸:加载的图片尺寸不会完全统一,可以是正方形,长方形等,这里不需要考虑正方形,因为一般 ...

  7. Flutter组件--Image 图片组件,圆形图片

    1.属性 名称 color和colorBlendMode 设置图片的背景颜色,通常和colorBlendMode配合一起使用,这样可以是图片颜色和背景色混合.上面的图片就是进行了颜色的混合,绿色背景和 ...

  8. android画圆形背景图片,android圆形图片,圆形背景文字的CircleTextImageView开源组件...

    转[http://blog.csdn.net/ys408973279/article/details/50350313] 在项目中我们经常遇到需要显示圆形头像的需求,一般我们都使用hdodenhof/ ...

  9. 方形图片转为圆形图片

    在线工具:http://www.quickpicturetools.com/en/rounded_corners/ 打开 在线工具:http://www.quickpicturetools.com/e ...

  10. css圆形图片处理示例(方形图片变圆形图片)

    <div style="width:120px; height:120px; border-radius:50%; overflow:hidden;"> <img ...

最新文章

  1. 笔记:Linux下C编程实现每隔一秒向文件中写入当前时间
  2. Python应用——自定义函数:分割PDF文件函数
  3. 利用Python制作王者荣耀出装小助手,引来了老板的注意!
  4. Python 深浅copy 和文件操作
  5. 【CF321E】+【bzoj5311】贞鱼
  6. [hdu1242]优先队列
  7. searchIndexer.exe占用过高CPU
  8. paip..net VS2010提示当前上下文中不存在名称的解决
  9. Java Runtime 详解
  10. linux备份整个系统
  11. [kuangbin带你飞] 专题一 简单搜索 题解(超详细注释,史上最强题解)
  12. str.trim()去除空格
  13. 大数据解决方案-最新全套文件
  14. matlab 代码转 Python
  15. PPT插件(islide)
  16. 机器学习基石(林軒田)笔记之十二
  17. 利用Xutils框架进行断点续传下载
  18. 【Linux杂篇】Windows远程登陆Linux、Linux静态IP配置
  19. Kx 系统与国信证券签定 OEM 合作协议
  20. Delphi 2010正式版下载(RAD Studio 2010下载)

热门文章

  1. Adobe PS常用快捷键
  2. VSLAM学习记录-求导:李群与李代数
  3. 微信小程序开发开篇词 自顶向下,云端赋能:小程序的高效开发之道
  4. Android应用程序中的DVM和Linux中进程的区别
  5. 操作系统基础:内存管理逻辑思维导图,简单看懂内存管理的逻辑
  6. cmake编译时中途报错:undefined reference to ‘xxx‘.未定义引用
  7. C盘空间大释放——Downloaded Installations
  8. python学习心得和体会
  9. win10下使用Winsat对系统进行评分
  10. php to es7,只需五步 集成新版 Elasticsearch7.9 中文搜索 到你的 Laravel7 项目