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

高洛峰
Lepaskan: 2023-03-03 13:50:02
asal
1537 orang telah melayarinya

本文实例讲述了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(&#39;Asia/Shanghai&#39;);
    error_reporting( E_ALL&~E_NOTICE&~E_WARNING);
    $this->load->library(&#39;curl&#39;);
  }
  /**
   * @todo : 本函数用于 将方形的图片压缩后
   *     再裁减成圆形 做成logo
   *     与背景图合并
   * @return 返回url
   */
  public function index(){
    //头像
    $headimgurl = &#39;a.jpg&#39;;
    //背景图
    $bgurl = &#39;./aa.png&#39;;
    $imgs[&#39;dst&#39;] = $bgurl;
    //第一步 压缩图片
    $imggzip = $this->resize_img($headimgurl);
    //第二步 裁减成圆角图片
    $imgs[&#39;src&#39;] = $this->test($imggzip);
    //第三步 合并图片
    $dest = $this->mergerImg($imgs);
  }
  public function resize_img($url,$path=&#39;./&#39;){
    $imgname = $path.uniqid().&#39;.jpg&#39;;
    $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=&#39;./&#39;){
    $w = 110; $h=110; // original size
    $original_path= $url;
    $dest_path = $path.uniqid().&#39;.png&#39;;
    $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(&#39;Content-Type: image/png&#39;);
    imagepng($newpic, $dest_path);
    imagedestroy($newpic);
    imagedestroy($src);
    unlink($url);
    return $dest_path;
  }
  //php 合并图片
  public function mergerImg($imgs,$path=&#39;./&#39;) {
    $imgname = $path.rand(1000,9999).uniqid().&#39;.jpg&#39;;
    list($max_width, $max_height) = getimagesize($imgs[&#39;dst&#39;]);
    $dests = imagecreatetruecolor($max_width, $max_height);
    $dst_im = imagecreatefrompng($imgs[&#39;dst&#39;]);
    imagecopy($dests,$dst_im,0,0,0,0,$max_width,$max_height);
    imagedestroy($dst_im);
    $src_im = imagecreatefrompng($imgs[&#39;src&#39;]);
    $src_info = getimagesize($imgs[&#39;src&#39;]);
    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[&#39;dst&#39;]);
    unlink($imgs[&#39;src&#39;]);
    return $imgname;
  }
}
Salin selepas log masuk

   

结果展示:

20161031140826605.jpg


Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!