当前位置:首页 > 开发教程 > php教程 >

PHP实现多图片合并在图片上面添加logo

时间:2018-02-03 20:27 来源:互联网 作者:源码搜藏 收藏

今天有个客户项目想把自己的logo加在用户上传的图片上,需求提给我了,我就用php写了个图片合并的函数,废话不多说了上代码。 ? php function mergerImg ( $imgs ) { list ( $max_width , $max_height ) = getimagesize ( $imgs [ dst ]); $dests = imagecre

说一下步骤:

总共分 3 步:

1. 压缩logo 成固定大小的方形图片

2. 将logo 转成圆形logo

3. 将logo与背景图合并

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

 

[php] view plain copy
 
  1. <php  
  2. /** 
  3.  * 作者:friker 
  4.  * 开发时间:20160516 
  5.  * 功能:图片处理 
  6.  * 
  7.  */  
  8. class ImageController extends CI_Controller{  
  9.   
  10.     public function __construct()  
  11.     {  
  12.         parent::__construct();  
  13.         date_default_timezone_set('Asia/Shanghai');  
  14.         error_reporting( E_ALL&~E_NOTICE&~E_WARNING);  
  15.         $this->load->library('curl');  
  16.     }  
  17.   
  18.     /** 
  19.      * @todo : 本函数用于 将方形的图片压缩后 
  20.      *         再裁减成圆形 做成logo 
  21.      *         与背景图合并 
  22.      * @return 返回url 
  23.      */  
  24.     public function index(){  
  25.         //头像  
  26.         $headimgurl = 'a.jpg';  
  27.         //背景图  
  28.         $bgurl = './aa.png';  
  29.         $imgs['dst'] = $bgurl;  
  30.         //第一步 压缩图片  
  31.         $imggzip = $this->resize_img($headimgurl);  
  32.         //第二步 裁减成圆角图片  
  33.         $imgs['src'] = $this->test($imggzip);  
  34.         //第三步 合并图片  
  35.         $dest = $this->mergerImg($imgs);  
  36.     }  
  37.   
  38.     public function resize_img($url,$path='./'){  
  39.         $imgname = $path.uniqid().'.jpg';  
  40.         $file = $url;  
  41.         list($width$height) = getimagesize($file); //获取原图尺寸  
  42.         $percent = (110/$width);  
  43.         //缩放尺寸  
  44.         $newwidth = $width * $percent;  
  45.         $newheight = $height * $percent;  
  46.         $src_im = imagecreatefromjpeg($file);  
  47.         $dst_im = imagecreatetruecolor($newwidth$newheight);  
  48.         imagecopyresized($dst_im$src_im, 0, 0, 0, 0, $newwidth$newheight$width$height);  
  49.         imagejpeg($dst_im$imgname); //输出压缩后的图片  
  50.         imagedestroy($dst_im);  
  51.         imagedestroy($src_im);  
  52.         return $imgname;  
  53.     }  
  54.   
  55.     //第一步生成圆角图片  
  56.     public function test($url,$path='./'){  
  57.         $w = 110;  $h=110; // original size  
  58.         $original_path$url;  
  59.         $dest_path = $path.uniqid().'.png';  
  60.         $src = imagecreatefromstring(file_get_contents($original_path));  
  61.         $newpic = imagecreatetruecolor($w,$h);  
  62.         imagealphablending($newpic,false);  
  63.         $transparent = imagecolorallocatealpha($newpic, 0, 0, 0, 127);  
  64.         $r=$w/2;  
  65.         for($x=0;$x<$w;$x++)  
  66.             for($y=0;$y<$h;$y++){  
  67.                 $c = imagecolorat($src,$x,$y);  
  68.                 $_x = $x - $w/2;  
  69.                 $_y = $y - $h/2;  
  70.                 if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){  
  71.                     imagesetpixel($newpic,$x,$y,$c);  
  72.                 }else{  
  73.                     imagesetpixel($newpic,$x,$y,$transparent);  
  74.                 }  
  75.             }  
  76.         imagesavealpha($newpic, true);  
  77.         // header('Content-Type: image/png');  
  78.         imagepng($newpic$dest_path);  
  79.         imagedestroy($newpic);  
  80.         imagedestroy($src);  
  81.         unlink($url);  
  82.         return $dest_path;  
  83.     }  
  84.   
  85.     //php 合并图片  
  86.     public function mergerImg($imgs,$path='./') {  
  87.   
  88.         $imgname = $path.rand(1000,9999).uniqid().'.jpg';  
  89.         list($max_width$max_height) = getimagesize($imgs['dst']);  
  90.         $dests = imagecreatetruecolor($max_width$max_height);  
  91.         $dst_im = imagecreatefrompng($imgs['dst']);  
  92.         imagecopy($dests,$dst_im,0,0,0,0,$max_width,$max_height);  
  93.         imagedestroy($dst_im);  
  94.   
  95.         $src_im = imagecreatefrompng($imgs['src']);  
  96.         $src_info = getimagesize($imgs['src']);  
  97.         imagecopy($dests,$src_im,270,202,0,0,$src_info[0],$src_info[1]);  
  98.         imagedestroy($src_im);  
  99.   
  100.         // var_dump($imgs);exit;  
  101.         // header("Content-type: image/jpeg");  
  102.         imagejpeg($dests,$imgname);  
  103.         // unlink($imgs['dst']);  
  104.         unlink($imgs['src']);  
  105.         return $imgname;  
  106.     }  
  107.   
  108.   
  109. }  

结果展示:

 

PHP实现多图片合并在图片上面添加logo


php教程阅读排行

最新文章