TP5.1生成海报

//远程下载图片
function downImg($imgUrl, $savePath = '',$filename = '')
{
    if(!isDirFile($savePath)){
        mkdir($savePath,0777,true);
    }
    if($savePath == ''){
        return false;
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $imgUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
    $file = curl_exec($ch);
    curl_close($ch);
    $ext = pathinfo($imgUrl,PATHINFO_EXTENSION);
    if(!$ext)
    $ext = 'png';
    $resource = fopen($savePath . $filename . '.' .$ext, 'w');
    $res = fwrite($resource, $file);
    fclose($resource);
    if($res){
        return $filename . '.' .$ext;
    }else{
        return false;
    }
}

//生成缩略图
function imgToThumb($imgUrl){
    $image = \think\Image::open($imgUrl);
    $image->thumb(104, 104)->radius(50)->save($imgUrl);
}
//判断文件或者目录是否存在
function isDirFile($path,$isfile = false){
    if($isfile){
        return file_exists($path) ? true : false;
    }else{
        return is_dir($path) ? true : false;
    }
}
//水印
function water($codefile,$text,$newFile,$waterBg="./uploads/haibao.jpg"){
    //判断图片尺寸-生成缩略图
    list($width, $height, $type, $attr) = getimagesize($codefile);
    if($width > 104 || $height > 104){
        imgToThumb($codefile);
    }
    if(!isDirFile(\Env::get('root_path').'public/'.$waterBg,true)){
        return false;
    }
    $image = \think\Image::open($waterBg);

    /*
    if($strlen>8){
        $text = mb_substr($text,0,8,'UTF-8').'...';
        $offset = [380,985];
    }
    */
    $textName = isset($text[0]) ? $text[0] : '';
    $textData = isset($text[1]) ? $text[1] : '';
    $textDays = isset($text[2]) ? $text[2] : '';
    $textNums = isset($text[3]) ? $text[3] : '';

    $image->text($textName,\Env::get('root_path').'public/static/font/msyh.ttf',24,'#323232',[150,822])
    ->text($textData,\Env::get('root_path').'public/static/font/msyh.ttf',14,'#c5a98f',[150,864])
    ->text($textDays,\Env::get('root_path').'public/static/font/msyh.ttf',46,'#9e7758',[360,810])
    ->text($textNums,\Env::get('root_path').'public/static/font/msyh.ttf',46,'#9e7758',[540,810])
    ->water($codefile,[36,804])->save($newFile); 
    return $newFile;
}

 

转载于:https://my.oschina.net/u/914487/blog/3079087

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值