合并图片和加文字

/**
     * 合并图片,
     *
     * @param unknown $imgs
     * @param unknown $realname
     * @param unknown $userid
     */
    public function mergerImg($imgs,$realname,$userid,$imgpath='shareimgs/') {
        /* if(!file_exists($imgs['dst'])){
            file_put_contents('wx.log',date('Y-m-d H:i:s').':框架图片路径错误。'.$imgs['dst']);
            return false;
        } */
        if(!file_exists($imgs['src'])){
            file_put_contents('wx.log',date('Y-m-d H:i:s').':二维码图片路径错误。'.$imgs['src']);
            return false;
        }
        if(!file_exists($imgs['touxiang'])){
            file_put_contents('wx.log',date('Y-m-d H:i:s').':头像图片路径错误。'.$imgs['touxiang']);
            return false;
        }
            
        $file=BASE_UPLOAD_PATH.'/'.$imgpath.$userid.'.jpg';
        
        /* if(file_exists($file)){
            return $file;
        } */
        if(!file_exists(BASE_UPLOAD_PATH.'/'.$imgpath)){
            mkdir(BASE_UPLOAD_PATH.'/'.$imgpath);
        }
        
        //得到图片的宽高
        list($max_width, $max_height) = getimagesize($imgs['dst']);
        $dests = imagecreatetruecolor($max_width, $max_height);//创建一个真彩色图片流
        $dst_im=$this->imageCreateFrom_sss($imgs['dst']);
    
        imagecopy($dests,$dst_im,0,0,0,0,$max_width,$max_height);
        imagedestroy($dst_im);
    
        $src_im=$this->imageCreateFrom_sss($imgs['touxiang']);//头像
        $src_info = getimagesize($imgs['touxiang']);
        //var_dump($src_info);
//imagecopyresized($dst_image,$src_image,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h);
//将于src_w的宽度和高度src_h     src_image的位置(src_x,src_y),并将其放置在dst_w的宽度和高度dst_h dst_image矩形区域,它是在位置的矩形区域(dst_x,dst_y)。
        $r=imagecopyresized( $dests , $src_im , 150 , 460 , 0 , 0 , 100 , 100 , $src_info[0] , $src_info[1]);
        if(!$r){
            file_put_contents('wx.log',date('Y-m-d H:i:s').':图像合成失败,图片路径->'.$imgs['touxiang']);
            return false;
        }
        imagedestroy($src_im);
    
    
        $src_im=$this->imageCreateFrom_sss($imgs['src']);//二维码
        $src_info = getimagesize($imgs['src']);
        $r=imagecopyresized($dests , $src_im , 150 , 585 , 0 , 0 , 338 , 338 , $src_info[0] , $src_info[1]);
        if(!$r){
            file_put_contents('wx.log',date('Y-m-d H:i:s').':二维码合成失败,图片路径->'.$imgs['src']);
            return false;
        }
        imagedestroy($src_im);
    
        //建立一幅100*30的图像
        //    $image = imagecreatetruecolor(2000,100);
        //设置背景颜色
        // $bgcolor = imagecolorallocate($image,0,0,0);
        //设置字体颜色
        $textcolor = imagecolorallocate($dests,0,0,0);
    
        //$black = ImageColorAllocate($dests, 0,0,0);
        $white = ImageColorAllocate($dests, 255, 255, 255);
    
//array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
        ImageTTFText($dests, 20, 0, 260, 520, $white, BASE_ROOT_PATH.'/wap/js/simsun.ttc', $realname);
        //ImageTTFText($dests, 20, 0, 450, 620, $white, "./simhei.ttf", $groupname);
        //ImageTTFText($dests, 20, 0, 450, 690, $white, "./simhei.ttf", $typena);
        //var_dump($r);exit;
        //把字符串写在图像左上角
        //imagestring($dests,20,450,540,"$realname",$textcolor);
        //imagestring($dests,20,450,620,"Hello world!",$textcolor);
        //imagestring($dests,20,450,690,"Hello world!",$textcolor);
        //header('Content-type: image/jpg');
        
        imagejpeg($dests,$file);//输出到文件
        //imagejpeg($dests);//输出到浏览器
        imagedestroy($dests);
        return $file;
    }
    
    public function get_extension($file_name){
        $extend =explode("." , $file_name);
        return end($extend);
    }
    
    
    public function imageCreateFrom_sss($big_img){
        $im='';
        $imgage = getimagesize($big_img); //得到原始大图片
        switch ($imgage[2]) { // 图像类型判断
            case 1:
                $im = imagecreatefromgif($big_img);
                break;
            case 2:
                $im = imagecreatefromjpeg($big_img);
                break;
            case 3:
                $im = imagecreatefrompng($big_img);
                break;
        }
        return $im;
    }

posted @ 2017-06-26 11:06  nike_ha  阅读(420)  评论(0编辑  收藏  举报