php實現背景圖上添加圓形logo表徵圖

來源:互聯網
上載者:User
這篇文章主要介紹了php實現背景圖上添加圓形logo表徵圖的方法,結合執行個體形式較為詳細的分析了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;  }}

結果展示:

以上就是本文的全部內容,希望對大家的學習有所協助。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.