温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php如何实现背景图上添加圆形logo图标

发布时间:2021-06-25 10:07:38 阅读:167 作者:小新 栏目:开发技术
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章将为大家详细讲解有关php如何实现背景图上添加圆形logo图标,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

具体如下:

总共分 3 步:

1. 压缩logo 成固定大小的方形图片
2. 将logo 转成圆形logo
3. 将logo与背景图合并

代码如下:

<?php
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_im0000$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($newpic000127);
    $r=$w/2;
    for($x=0;$x<$w;$x++)
      for($y=0;$y<$h;$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($newpictrue);
    // 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如何实现背景图上添加圆形logo图标

关于“php如何实现背景图上添加圆形logo图标”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×