php实现背景图上添加圆形logo图标的方法

yipeiwu_com5年前PHP代码库

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

说一下步骤:

总共分 3 步:

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

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

<?php
/**
 * 作者: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<$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($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相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

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

相关文章

如何利用php array_multisort函数 对数据库结果进行复杂排序

首先讲一下需求:数据库中有4个字段分别是id,volume,edition,name. 要求对查询结果按照volume+edition从大到小排序。下面将一下array_multisor...

学习php设计模式 php实现工厂模式(factory)

学习php设计模式 php实现工厂模式(factory)

一、意图 定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使用一个类的实例化延迟到其子类【GOF95】 二、工厂模式结构图 三、工厂模式中主要角色...

利用PHP自动生成印有用户信息的名片

利用PHP自动生成印有用户信息的名片

前言 无论是自己要在精心P过的自拍上添加个性文字,或者是摄影爱好者要在拍摄的作品里添加水印,亦或是在网页或者移动应用中实时生成文字和图片的组合,我们都需要找到一个合适且靠谱的方法来将图片...

利用php下载xls文件(自己动手写的)

利用php下载xls文件(自己动手写的)

昨天看ECSHOP源码的时候,碰到了一点自己没有学过的只是--如何利用php实现下载xls文件。根据它的源码,我动手实现了一下,成功实现了这个效果。 源码: 复制代码 代码如下: <...

使用PHP遍历文件夹与子目录的函数代码

使用PHP遍历文件夹与子目录的函数代码

我们要使用的函数有 Scandir,它的作用是列出指定路径中的文件和目录,就像 Dir 一样。 > 与更强力的 Glob() 函数,作用是以数组的形式返回与指定模式相匹配的文件名...