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

<?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);
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值