用php画一个蓝底红色的圆_php实现背景图上添加圆形logo图标的方法

本文通过实例代码介绍了使用PHP将方形logo压缩、转换为圆形logo,并与背景图合并的方法,适用于制作网站或应用的个性化图标。
摘要由CSDN通过智能技术生成

本文实例讲述了php实现背景图上添加圆形logo图标的方法。分享给大家供大家参考,具体如下:

说一下步骤:

总共分 3 步:

1. 压缩logo 成固定大小的方形图片

2. 将logo 转成圆形logo

3. 将logo与背景图合并

废话不多说,直接上代码:

/**

* 作者:friker

* 开发时间:20160516

* 功能:图片处理

*

*/

class ImageController extends CI_Controller{

public function __construct()

{

parent::__construct();

date_default_timezone_set('Asia/Shanghai');

error_reporting( E_ALL&~E_NOTICE&~E_WARNING);

$this->load->library('curl');

}

/**

* @todo : 本函数用于 将方形的图片压缩后

* 再裁减成圆形 做成logo

* 与背景图合并

* @return 返回url

*/

public function index(){

//头像

$headimgurl = 'a.jpg';

//背景图

$bgurl = './aa.png';

$imgs['dst'] = $bgurl;

//第一步 压缩图片

$imggzip = $this->resize_img($headimgurl);

//第二步 裁减成圆角图片

$imgs['src'] = $this->test($imggzip);

//第三步 合并图片

$dest = $this->mergerImg($imgs);

}

public function resize_img($url,$path='./'){

$imgname = $path.uniqid().'.jpg';

$file = $url;

list($width, $height) = getimagesize($file); //获取原图尺寸

$percent = (110/$width);

//缩放尺寸

$newwidth = $width * $percent;

$newheight = $height * $percent;

$src_im = imagecreatefromjpeg($file);

$dst_im = imagecreatetruecolor($newwidth, $newheight);

imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($dst_im, $imgname); //输出压缩后的图片

imagedestroy($dst_im);

imagedestroy($src_im);

return $imgname;

}

//第一步生成圆角图片

public function test($url,$path='./'){

$w = 110; $h=110; // original size

$original_path= $url;

$dest_path = $path.uniqid().'.png';

$src = imagecreatefromstring(file_get_contents($original_path));

$newpic = imagecreatetruecolor($w,$h);

imagealphablending($newpic,false);

$transparent = imagecolorallocatealpha($newpic, 0, 0, 0, 127);

$r=$w/2;

for($x=0;$x

for($y=0;$y

$c = imagecolorat($src,$x,$y);

$_x = $x - $w/2;

$_y = $y - $h/2;

if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){

imagesetpixel($newpic,$x,$y,$c);

}else{

imagesetpixel($newpic,$x,$y,$transparent);

}

}

imagesavealpha($newpic, true);

// header('Content-Type: image/png');

imagepng($newpic, $dest_path);

imagedestroy($newpic);

imagedestroy($src);

unlink($url);

return $dest_path;

}

//php 合并图片

public function mergerImg($imgs,$path='./') {

$imgname = $path.rand(1000,9999).uniqid().'.jpg';

list($max_width, $max_height) = getimagesize($imgs['dst']);

$dests = imagecreatetruecolor($max_width, $max_height);

$dst_im = imagecreatefrompng($imgs['dst']);

imagecopy($dests,$dst_im,0,0,0,0,$max_width,$max_height);

imagedestroy($dst_im);

$src_im = imagecreatefrompng($imgs['src']);

$src_info = getimagesize($imgs['src']);

imagecopy($dests,$src_im,270,202,0,0,$src_info[0],$src_info[1]);

imagedestroy($src_im);

// var_dump($imgs);exit;

// header("Content-type: image/jpeg");

imagejpeg($dests,$imgname);

// unlink($imgs['dst']);

unlink($imgs['src']);

return $imgname;

}

}

结果展示:

希望本文所述对大家PHP程序设计有所帮助。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值