php 图片加文字 图片生成图片水印

来源:互联网 发布:apache与php关系 编辑:程序博客网 时间:2024/06/05 03:21
<?php
class TupianController{
    /*
     * bgurl 背景url
     * tuurl 图片url
     * dingwei [x,y] 图片在背景图的xy 位置
     * daxiao  [w,h] 图片的大小
     * pathfile 存储路径文件名
     */
    public function Perweima($bgurl,$tuurl,$dingwei,$daxiao,$pathfile){
        $base_name = $bgurl;
        $e = $tuurl;
        // Content type
        header('Content-Type: image/jpeg');
        // Load
        $thumb = imagecreatefromjpeg($base_name);// 图片创建到jpeg
        if(is_string($e)){
            list($width, $height) = getimagesize($e);// 获取图片的大小等信息
            if(strpos($e,'png')){
                $e_p = imagecreatefrompng($e);// 图片创建到png
            }
            if(strpos($e,'jpg')){
                $e_p = imagecreatefromjpeg($e);// 图片创建到jpg
            }
        }else{
            $e_p = $tuurl;
        }
        // 图片复制
        $is = imagecopyresized($thumb,$e_p,$dingwei[0],$dingwei[1], 0, 0,$daxiao[0],$daxiao[0],$width,$height);
        imagejpeg($thumb,$pathfile);
        imagedestroy($thumb);   
    }


    /*
     * 图片加文字
     * str 添加文字
     * dingwei 0 字体大小
               1 选转的角度
               2 字体x轴位置
               3 字体y轴位置
     * pathfile 背景图路径
     * fontpath 字体路径
     * savepathimg 图片存储路径
     */
    public function shengc_pngimage_str($str,$dingwei,$pathfile,$fontpath,$savepathimg){
        $block = imagecreatefromjpeg($pathfile);// 图片创建到jpeg
        //拾取一个完全透明的颜色,不要用imagecolorallocate拾色     
        $color = imagecolorallocate($block,255,255,255); 
        //字体拾色     
        // imagealphablending($block , false);
        //关闭混合模式,以便透明颜色能覆盖原画板     
        // imagefill($block , 0 , 0 , $bg);
        //填充     
        // imagefttext($block,15,0,365,1043,$color,$fontpath,$text);    
        imagefttext($block,$dingwei[0],$dingwei[1],$dingwei[2],$dingwei[3], $color, $fontpath,$str);
        // imagestring($block,25,365,1043,'',$color); 
        imagesavealpha($block , true);
        //设置保存PNG时保留透明通道信息     
        header("content-type:image/png");   
        imagepng($block,$savepathimg);
        //生成图片     
        imagedestroy($block); 
    }
}


$hecheng = new TupianController();
$pathfilesc = './bg1.jpg';
$hecheng->Perweima('./bg.jpg','./erweima.jpg',[295,1070],[160,160],$pathfilesc);
$hecheng->shengc_pngimage_str('XXX',[18,0,365,1042],$pathfilesc,'./msyh.ttf',$pathfilesc);
?>
原创粉丝点击