php實現(xiàn)背景圖上添加圓形logo圖標的方法
本文實例講述了php實現(xiàn)背景圖上添加圓形logo圖標的方法。分享給大家供大家參考,具體如下:
說一下步驟:
總共分 3 步:
1. 壓縮logo 成固定大小的方形圖片
2. 將logo 轉(zhuǎn)成圓形logo
3. 將logo與背景圖合并
廢話不多說,直接上代碼:
<?php
/**
* 作者:friker
* 開發(fā)時間: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 : 本函數(shù)用于 將方形的圖片壓縮后
* 再裁減成圓形 做成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;
}
}
結(jié)果展示:

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- Thinkphp3.2.3整合phpqrcode生成帶logo的二維碼
- php生成帶logo二維碼方法小結(jié)
- PHP基于phpqrcode生成帶LOGO圖像的二維碼實例
- PHP實現(xiàn)批量生成App各種尺寸Logo
- PHP下通過QRCode類庫創(chuàng)建中間帶網(wǎng)站LOGO的二維碼
- 使用PHP生成二維碼的兩種方法(帶logo圖像)
- PHP實現(xiàn)生成帶背景的圖形驗證碼功能
- PHP實現(xiàn)對png圖像進行縮放的方法(支持透明背景)
- PHP圖片處理之圖片背景、畫布操作
- php縮放gif和png圖透明背景變成黑色的解決方法
- PHP實現(xiàn)生成透明背景的PNG縮略圖函數(shù)分享
相關(guān)文章
高并發(fā)php uniqid不重復(fù)唯一標識符生成方案
這篇文章主要介紹了高并發(fā)php uniqid不重復(fù)唯一標識符生成方案,對高并發(fā)感興趣的同學,一定要看一下2021-04-04
php 模擬GMAIL,HOTMAIL(MSN),YAHOO,163,126郵箱登錄的詳細介紹
本篇文章是對php模擬GMAIL,HOTMAIL(MSN),YAHOO,163,126郵箱登錄的方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06
PHP實現(xiàn)獲取并生成數(shù)據(jù)庫字典的方法
這篇文章主要介紹了PHP實現(xiàn)獲取并生成數(shù)據(jù)庫字典的方法,可實現(xiàn)讀取數(shù)據(jù)庫并列出詳細數(shù)據(jù)庫信息的功能,需要的朋友可以參考下2016-05-05

