php生成海报.解决抓取微信头像速度慢的问题

<?php 

/** 
 * 分享图片生成 
 * @param $gData 商品数据,array 
 * @param $codeName 二维码图片 
 * @param $fileName string 保存文件名,默认空则直接输入图片 
 */
function createSharePng($gData,$codeName,$fileName = ''){

  $im = imagecreatefrompng('code_png/userbak.png');
  //字体文件 
  $font_file = "black.ttf"; 
  $font_file_bold = "zongyi.ttf"; 
 

  //设定字体的颜色 
  $font_color_1 = ImageColorAllocate ($im, 140, 140, 140); 
  $font_color_2 = ImageColorAllocate ($im, 50, 50, 50); 
  $font_color_hui = ImageColorAllocate ($im, 120, 120, 120); 
  $font_color_3 = ImageColorAllocate ($im, 129, 129, 129); 
  $font_color_purple = ImageColorAllocate ($im, 80, 50, 120); 
  $font_color_orange = ImageColorAllocate ($im, 240, 150, 50); 
  
  $fang_bg_color = ImageColorAllocate ($im, 254, 216, 217); 
  

  //$avatar=file_get_contents($gData['pic']);
  $avatar=$gData['pic'];
  //商品图片 
  list($w,$h) = getimagesize($avatar); 
  $goodImg = createImageFromFile($avatar); 
  //$goodImg = roundImgs($goodImg); 
  //$goodImg= radius_img($goodImg, $w,$h, $radius = 255);
  imageantialias($goodImg, true);//抗锯齿,


//开始缩放图像

  $width=100;
  $height=100;
  imagecopyresized($im, $goodImg, 134, 80, 0, 0,$width, $height, $w, $h);
  $margin_left=270;
 //用户名
  imagettftext($im, 26,0, $margin_left,110, $font_color_2,$font_file, $gData['username']); 
  imagettftext($im, 26,0, $margin_left,165, $font_color_hui ,$font_file, '友至ID '.$gData['youzhiid']);
  //imagettftext($im, 18,0, 650,125, $font_color_orange ,$font_file, $gData["hpd"]);

  //二维码 
  list($code_w,$code_h) = getimagesize($codeName); 
  $codeImg = createImageFromFile($codeName); 
  imageantialias($im, true);//抗锯齿,
  //var_dump($codeImg);exit();
  imagecopyresized($im, $codeImg, 108, 180, 0, 0, 500, 500, $code_w, $code_h); 
  
  //提示语 
  //imagettftext($im, 12,0, 8, 620, $font_color_purple ,$font_file, "温馨提示:"); 
  

  
  //输出图片 
  if($fileName){ 
    imagepng ($im,$fileName); 
  }else{ 
    Header("Content-Type: image/png"); 
    imagepng ($im); 
  } 
  
  //释放空间 
  imagedestroy($im); 
  imagedestroy($img);
  imagedestroy($goodImg); 
  imagedestroy($codeImg); 
} 
  
/** 
 * 从图片文件创建Image资源 
 * @param $file 图片文件,支持url 
 * @return bool|resource  成功返回图片image资源,失败返回false 
 */
function createImageFromFile($file){ 
  if(preg_match('/http(s)?:\/\//',$file)){ 
    $fileSuffix = getNetworkImgType($file); 
  }else{ 
    $fileSuffix = pathinfo($file, PATHINFO_EXTENSION); 
  } 

  if(!$fileSuffix) return false; 
  
  switch ($fileSuffix){ 
    case 'jpeg': 
      $theImage = @imagecreatefromjpeg($file); 
      break; 
    case 'jpg': 
      $theImage = @imagecreatefromjpeg($file); 
      break; 
    case 'png': 
      $theImage = imagecreatefrompng($file); 
      break; 
    case 'gif': 
      $theImage = @imagecreatefromgif($file); 
      break; 
    default: 
      $theImage = @imagecreatefromstring(getImage($file)); 
      break; 
  } 
  
  return $theImage; 
} 
  
/** 
 * 获取网络图片类型 
 * @param $url 网络图片url,支持不带后缀名url 
 * @return bool 
 */
function getNetworkImgType($url){ 
  $ch = curl_init(); //初始化curl 
  curl_setopt($ch, CURLOPT_URL, $url); //设置需要获取的URL 
  curl_setopt($ch, CURLOPT_NOBODY, 1); 
  curl_setopt($ch, CURLOPT_ENCODING, ""); //加速 这个地方留空就可以了
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置超时 
  curl_setopt($ch, CURLOPT_TIMEOUT, 3); 
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持https 
  curl_exec($ch);//执行curl会话 
  $http_code = curl_getinfo($ch);//获取curl连接资源句柄信息 
  curl_close($ch);//关闭资源连接 
  
  if ($http_code['http_code'] == 200) { 
    $theImgType = explode('/',$http_code['content_type']); 
  
    if($theImgType[0] == 'image'){ 
      return $theImgType[1]; 
    }else{ 
      return false; 
    } 
  }else{ 
    return false; 
  } 
} 
  
/** 
 * 分行连续截取字符串 
 * @param $str 需要截取的字符串,UTF-8 
 * @param int $row 截取的行数 
 * @param int $number  每行截取的字数,中文长度 
 * @param bool $suffix 最后行是否添加‘...'后缀 
 * @return array  返回数组共$row个元素,下标1到$row 
 */
function cn_row_substr($str,$row = 1,$number = 10,$suffix = true){ 
  $result = array(); 
  for ($r=1;$r<=$row;$r++){ 
    $result[$r] = ''; 
  } 
  
  $str = trim($str); 
  if(!$str) return $result; 
  
  $theStrlen = strlen($str); 
  
  //每行实际字节长度 
  $oneRowNum = $number * 3; 
  for($r=1;$r<=$row;$r++){ 
    if($r == $row and $theStrlen > $r * $oneRowNum and $suffix){ 
      $result[$r] = mg_cn_substr($str,$oneRowNum-6,($r-1)* $oneRowNum).'...'; 
    }else{ 
      $result[$r] = mg_cn_substr($str,$oneRowNum,($r-1)* $oneRowNum); 
    } 
    if($theStrlen < $r * $oneRowNum) break; 
  } 
  
  return $result; 
} 




  
/** 
 * 按字节截取utf-8字符串 
 * 识别汉字全角符号,全角中文3个字节,半角英文1个字节 
 * @param $str 需要切取的字符串 
 * @param $len 截取长度[字节] 
 * @param int $start  截取开始位置,默认0 
 * @return string 
 */
function mg_cn_substr($str,$len,$start = 0){ 
  $q_str = ''; 
  $q_strlen = ($start + $len)>strlen($str) ? strlen($str) : ($start + $len); 
  
  //如果start不为起始位置,若起始位置为乱码就按照UTF-8编码获取新start 
  if($start and json_encode(substr($str,$start,1)) === false){ 
    for($a=0;$a<3;$a++){ 
      $new_start = $start + $a; 
      $m_str = substr($str,$new_start,3); 
      if(json_encode($m_str) !== false) { 
        $start = $new_start; 
        break; 
      } 
    } 
  } 
  
  //切取内容 
  for($i=$start;$i<$q_strlen;$i++){ 
    //ord()函数取得substr()的第一个字符的ASCII码,如果大于0xa0的话则是中文字符 
    if(ord(substr($str,$i,1))>0xa0){ 
      $q_str .= substr($str,$i,3); 
      $i+=2; 
    }else{ 
      $q_str .= substr($str,$i,1); 
    } 
  } 
  return $q_str; 
} 


 

//判断如果是微信头像就走代理

if(strpos($_GET['avatar'],'wx.qlogo') !== false){ 
  $avatar='http://xxx.com/tt.php?url='.$_GET['avatar'];
}else{
  $avatar=$_GET['avatar'];
}





$gData = [ 
  'pic' => $avatar, 
  'username' =>$_GET['nickname'], 
  'youzhiid' => $_GET['youzhiid'] ,
  'hpd' => 90 
]; 

//输出到图片 

/**
 * 图片圆角处理函数
 *
 * @param source  $srcImg GD图片资源
 * @param integer $radius 圆角大小 = 最短边的长度 / $radius
 */
function roundImgs($srcImg, $radius=2) {
    $w = imagesx($srcImg);
    $h = imagesy($srcImg);
    $radius = min($w, $h) / $radius;
    $img = imagecreatetruecolor($w, $h);
    imagesavealpha($img, true); // 设置透明通道
    $bg = imagecolorallocatealpha($img, 255, 255, 255, 127); // 拾取一个完全透明的颜色, 最后一个参数127为全透明
    imagefill($img, 0, 0, $bg);
    $r = $radius; // 圆 角半径
    for ($x = 0; $x < $w; $x++) {
        for ($y = 0; $y < $h; $y++) {
            $rgbColor = imagecolorat($srcImg, $x, $y);
            if (($x >= $radius && $x <= ($w - $radius)) || ($y >= $radius && $y <= ($h - $radius))) {
                imagesetpixel($img, $x, $y, $rgbColor); // 不在四角的范围内,直接画
            } else { // 在四角的范围内选择画
                // 上左
                $yx = $r; // 圆心X坐标
                $yy = $r; // 圆心Y坐标
                if (((($x - $yx) * ($x - $yx) + ($y - $yy) * ($y - $yy)) <= ($r * $r))) {
                    imagesetpixel($img, $x, $y, $rgbColor);
                }
                // 上右
                $yx = $w - $r; // 圆心X坐标
                $yy = $r; // 圆心Y坐标
                if (((($x - $yx) * ($x - $yx) + ($y - $yy) * ($y - $yy)) <= ($r * $r))) {
                    imagesetpixel($img, $x, $y, $rgbColor);
                }
                // 下左
                $yx = $r; // 圆心X坐标
                $yy = $h - $r; // 圆心Y坐标
                if (((($x - $yx) * ($x - $yx) + ($y - $yy) * ($y - $yy)) <= ($r * $r))) {
                    imagesetpixel($img, $x, $y, $rgbColor);
                }
                // 下右
                $yx = $w - $r; // 圆心X坐标
                $yy = $h - $r; // 圆心Y坐标
                if (((($x - $yx) * ($x - $yx) + ($y - $yy) * ($y - $yy)) <= ($r * $r))) {
                    imagesetpixel($img, $x, $y, $rgbColor);
                }
            }
        }
    }
    return $img;
}




/**
 * 画一个带圆角的背景图
 * @param $im  底图
 * @param $dst_x 画出的图的(0,0)位于底图的x轴位置
 * @param $dst_y 画出的图的(0,0)位于底图的y轴位置
 * @param $image_w 画的图的宽
 * @param $image_h 画的图的高
 * @param $radius 圆角的值
 */
function imagebackgroundmycard($srcImg, $dst_x, $dst_y, $image_w, $image_h, $radius)
{
    $resource = imagecreatetruecolor($image_w, $image_h);
    $bgcolor = imagecolorallocate($resource, 0xef, 0xef, 0xe1);//该图的背景色

    imagefill($resource, 0, 0, $bgcolor);
    $lt_corner = get_lt_rounder_corner($radius, 255, 255, 255);//圆角的背景色

    // lt(左上角)
    imagecopymerge($resource, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);
    // lb(左下角)
    $lb_corner = imagerotate($lt_corner, 90, 0);
    imagecopymerge($resource, $lb_corner, 0, $image_h - $radius, 0, 0, $radius, $radius, 100);
    // rb(右上角)
    $rb_corner = imagerotate($lt_corner, 180, 0);
    imagecopymerge($resource, $rb_corner, $image_w - $radius, $image_h - $radius, 0, 0, $radius, $radius, 100);
    // rt(右下角)
    $rt_corner = imagerotate($lt_corner, 270, 0);
    imagecopymerge($resource, $rt_corner, $image_w - $radius, 0, 0, 0, $radius, $radius, 100);
    return $im;
    //imagecopy($im, $resource, $dst_x, $dst_y, 0, 0, $image_w, $image_h);
}


//直接输出 
createSharePng($gData,'二维码地址'); 



function radius_img($src_img, $width,$height, $radius = 15) {
 
     $w  = &$width;
     $h  = &$height;
     // $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;
 }

 /**
 * curl模拟get进行 http 或 https url请求(可选附带cookie)
 * @param bool $type  请求类型:true为https请求,false为http请求 
 * @param string $url 请求地址
 * @param string  $cookie cookie字符串
 * @return  string  返回字符串
 */
function getImage(string $url )
 {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_ENCODING, ""); //加速 这个地方留空就可以了
  curl_setopt($ch, CURLOPT_HEADER, 0);
  $output = curl_exec($ch);
  curl_close($ch);
  return $output;
 }


 ?>

php 远程获取微信头像有时候会很慢,这里写了个php代理来加速一下 tt.php

$url=$_GET['url'];
//curl 没有做错误处理
 function getImage(string $url )
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, ""); //加速 这个地方留空就可以了
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}

效果

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值