合成图片方法

<?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 裁剪图片为圆形图片

    转自http://blog.csdn.net/kkmike999/article/details/16359713 /** * 转换图片成圆形 * * @param bitmap * 传入Bitmap ...

  2. canvas文字自动换行、圆角矩形画法、生成图片手机长按保存、方形图片变圆形

    canvas的文字自动换行函数封装 // str:要绘制的字符串 // canvas:canvas对象 // initX:绘制字符串起始x坐标 // initY:绘制字符串起始y坐标 // lineH ...

  3. HTML5_图片合成_刮刮卡

    刮刮卡(图片合成) 定义: globalCompositeOperation 属性,设置或返回如何将源图像 将 myCanvas 的背景图设置为一张图片,(刮开后显示) // 目标图像(已有的,外面一 ...

  4. compass Sprites 雪碧图 小图片合成[Sass和compass学习笔记]

    demo 源码 地址 https://github.com/qqqzhch/webfans 什么是雪碧图? CSS雪碧 即CSS Sprites,也有人叫它CSS精灵,是一种CSS图像合并技术,该方法 ...

  5. C# 图片的裁剪,两个图片合成一个图片

    图片的裁剪,两个图片合成一个图片(这是从网上摘的) /// <summary>         /// 图片裁剪,生成新图,保存在同一目录下,名字加_new,格式1.png  新图1_ne ...

  6. android 文字图片合成

    引用:http://blog.csdn.net/cq361106306/article/details/8142526 两种方法: 1.直接在图片上写文字 String str = "PIC ...

  7. php 图片添加文字水印 以及 图片合成(微信快码传播)

    1.图片添加文字水印: $bigImgPath = 'backgroud.png'; $img = imagecreatefromstring(file_get_contents($bigImgPat ...

  8. android开发 两张bitmap图片合成一张图片

    场景:对android4.4解码gif(解码文章见前面一篇)后的图片进行每帧处理,android4.3 解码出来的每帧都很完整,但是到android4.4版本就不完整了,每帧都是在第一帧的基础上把被改 ...

  9. Java图片处理(一)图片合成

    如何将多个头像合成类似QQ的群头像? 如上图所示,如何用java将单一的图片合成如上群头像. 在一个正方形外框中,要将多个图片合成上述图片.首先要做的是,依据圆相交的程度,计算圆心坐标与图片间空白区域 ...

随机推荐

  1. codeforces163D

    Large Refrigerator CodeForces - 163D 给定一个长方体的体积V,求出这个长方体的最小表面积. 输入 第一行有一个整数t (1 ≤ t ≤ 500) — 测试数据的组数 ...

  2. Codeforces 601B. Lipshitz Sequence(单调栈)

    Codeforces 601B. Lipshitz Sequence 题意:,q个询问,每次询问给出l,r,求a数组[l,r]中所有子区间的L值的和. 思路:首先要观察到,斜率最大值只会出现在相邻两点 ...

  3. 锁对象Lock-同步问题更完美的处理方式

    Lock是java.util.concurrent.locks包下的接口,Lock 实现提供了比使用synchronized 方法和语句可获得的更广泛的锁定操作,它能以更优雅的方式处理线程同步问题,我 ...

  4. 性能监控系统 | 从0到1 搭建Web性能监控系统

    工具介绍 1. Statsd 是一个使用Node开发网络守护进程,它的特点是通过UDP(性能好,及时挂了也不影响主服务)或者TCP来监听各种数据信息,然后发送聚合数据到后端服务进行处理.常见支持的「G ...

  5. css滚动条美化

    /*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/ ::-webkit-scrollbar { width: 5px; height: 5px; background-color: #F5F5 ...

  6. Flask+uwsgi+Nginx+Ubuntu部署教程

    学习 Flask,写完一个 Flask 应用需要部署的时候,就想着折腾自己的服务器.根据搜索的教程照做,对于原理一知半解,磕磕碰碰,只要运行起来了,谢天谢地然后不再折腾了,到下一次还需要部署时,这样的 ...

  7. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-5.HttpClient4.x工具获取使用

    笔记 5.HttpClient4.x工具获取使用     简介:讲解httpClient4.x相关依赖,并封装基本方法. 1.加入依赖         <dependency>       ...

  8. 小D课堂 - 新版本微服务springcloud+Docker教程_6-04 自定义Zuul过滤器实现登录

    笔记 4.自定义Zuul过滤器实现登录鉴权实战     简介:自定义Zuul过滤器实现登录鉴权实战 1.新建一个filter包 2.新建一个类,实现ZuulFilter,重写里面的方法 3.在类顶部加 ...

  9. iscsi序列二、iscsi多路径配置方式

    一.ISCSI多路径应用 如果存储服务器到交换机只有一条线路的时候,那么一条线路出线故障,整个就没法使用了,所以多线路可以解决这个问题,避免单点故障 如上图,如果SAN服务器与客户端交换机只有一条线路 ...

  10. Hadoop集群安装压缩工具Snappy,用于Hbase

    最近项目中要用到Hadoop和Hbase,为了节省服务器的存储成本,并提高吞吐,安装并开启HBase的数据压缩为Snappy. 主流的HBase压缩方式有GZip | LZO | Snappy,Sna ...