/**
* 生成图片
* @param $awarded_url 奖品图片
* @param $awarded_title 奖品信息
* @param $awarded_avatar 用户头像
* @param $awarded_nickname 用户昵称
* @param $awarded_phone 用户手机号
* @param $awarded_time 中奖时间
*/
private function create_img($awarded_url='', $awarded_title='', $awarded_avatar='', $awarded_nickname='', $awarded_phone='', $awarded_time)
{
header('Content-type: image/png');
// 创建画布
$image = imagecreatetruecolor(589, 767);
// 设置背景图片
$bg_file = MODULE_ROOT . '/images/awarded_bg.png';
$bg = imagecreatefrompng($bg_file);
// 将背景图片拷贝到画布相应位置
imagecopyresampled($image, $bg, 0, 0, 0, 0, 589, 767, 589, 767);
imagedestroy($bg);
// 设置字体,指向ttf文件
$font_file = MODULE_ROOT . '/css/msyh.ttf';
$awarded_file_img = ATTACHMENT_ROOT . '/' . $awarded_url;
$ext = pathinfo($awarded_file_img);
$awarded_img = null;
switch ($ext['extension']){
case 'jpg':
$awarded_img = imagecreatefromjpeg($awarded_file_img);
break;
case 'png':
$awarded_img = imagecreatefrompng($awarded_file_img);
break;
case 'gif':
$awarded_img = imagecreatefromgif($awarded_file_img);
break;
}
list($awarded_with, $awarded_height) = getimagesize($awarded_file_img);
imagecopyresampled($image, $awarded_img, 245, 210, 0, 0, 100, 100, $awarded_with, $awarded_height);
imagedestroy($awarded_img);
$awarded_color = imagecolorallocate($image, 255, 255, 255);
imagefttext($image, 21, 0, 210, 400, $awarded_color, $font_file, $awarded_title);
$title_color = imagecolorallocate($image, 0, 0, 0);
$avatar_file = $this->put_file_from_url_content($awarded_avatar, ATTACHMENT_ROOT . '/images/user_temp.png');
$avatar_img = $this->radius_img($avatar_file);
imagecopyresampled($image, $avatar_img, 180, 540, 0, 0, 40, 40, 132, 132);
imagedestroy($avatar_img);
imagefttext($image, 21, 0, 230, 570, $title_color, $font_file, mb_substr($awarded_nickname, 0, 12));
// $requireinfo = @iunserializer($myawardeds['requireinfo']);
// foreach ($requireinfo as $k=> $v) {
// if (strpos($k, 'shoujihao') !== false) {
// $phone = $v;
// }
// }
// $phone = substr_replace($phone, '******', 3, 6);
imagefttext($image, 21, 0, 220, 610, $title_color, $font_file, $awarded_phone);
imagefttext($image, 21, 0, 220, 650, $title_color, $font_file, $awarded_time);
// 生成图片格式
imagepng($image);
// 销毁图片
imagedestroy($image);
}
/**
* 异步将远程链接上的内容(图片或内容)写到本地
* @param $url 远程地址
* @param $path保存路径
* @return boolean
*/
private function put_file_from_url_content($url = '', $path = '')
{
$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',);
$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;
}
// 把图片转换成 圆形
private function radius_img($imgpath = '', $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;
}