PHP下图片裁剪、圆形、合并

PHP 林涛 11540℃ 0评论

好把,标题就这样叫吧。

这个源码是从网上找的,不过源码是有问题的,我稍微做了修改,并且测试可行。

向原作者直径。

先上一个处理完的图形。

614957c9233e0b267

代码:

<?php
/**
  *==============================================
  * Description	 生成图片
  *==============================================
  *
  * @FILE_NAME : makeImg.php
  * @author : zuiw
  * @MailAddr : mr.lintao@gmail.com
  * @copyright : Copyright (c) 2016 iamlintao.com 
  * @DATE : 2016-9-2 上午12:50:48
  * 
  * 原作者:friker
  * 
  *
  *--------------------------------------------------------------------------------------------
  * @Mark :
  * @Tutorial : 
  * @Todo :   
  *--------------------------------------------------------------------------------------------
  */

 $a = new ImageController();
 $b = $a->index();

class ImageController{

	public function __construct()
	{
		date_default_timezone_set('Asia/Shanghai');
		error_reporting( E_ALL&~E_NOTICE&~E_WARNING);
	}

	/**
	 * @todo : 本函数用于 将方形的图片压缩后
	 *         再裁减成圆形 做成logo
	 *         与背景图合并
	 * @return 返回url
	 */
	public function index(){
		//头像
		$headimgurl = 'head.jpg';
		//背景图
		$bgurl = 'hecheng.png';
		$imgs['dst'] = $bgurl;
		
		//第一步 压缩图片
		$imggzip = $this->resize_img($headimgurl);
		//第二步 裁减成圆角图片
		$imgs['src'] = $this->test($imggzip);
		//第三步 合并图片
		$dest = $this->mergerImg($imgs);
		
		echo $dest;exit;
	}

	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;
    }


}

如需转载请注明: 转载自26点的博客

本文链接地址: PHP下图片裁剪、圆形、合并

转载请注明:26点的博客 » PHP下图片裁剪、圆形、合并

喜欢 (0)
发表我的评论
取消评论

表情