thinkphp5.1 || 给图片添加文字,图片水印

composter下载扩展:

https://packagist.org/packages/aliyuncs/oss-sdk-php

composer require topthink/think-image

加字体文件:
在window中的C:\Windows\Fonts找到一个字体文件,加到thinkphp项目的根目录(或public/static/中),尽量不要有中文路径

代码文件:water.php

<?php

namespace app\api\controller;

use think\Image;
use think\facade\Env;

class Water extends Base
{
    
    
    /**
     * 图片加水印 文字水印和图片水印  \think\Image类添加tilewater方法,平铺水印
     */
    public function water()
    {
    
    
       
        //要加水印的图片
        $image = \think\Image::open('image/xcx_code/a.png'); 

        // 返回图片的宽度
        $width = $image->width();
        // 返回图片的高度
        $height = $image->height();
        // 返回图片的类型
        $type = $image->type();
        // 返回图片的mime类型
        $mime = $image->mime();
        // 返回图片的尺寸数组 0 图片宽度 1 图片高度
        $size = $image->size();

        //按照原图的比例生成一个最大为50*50的缩略图并保存为thumb.png
        $image_shuiyin = \think\Image::open('image/xcx_code/2_qr_code.png'); //打开水印图片
        $image_shuiyin = $image_shuiyin->thumb(50, 50)->save('image/xcx_code/2_qr_code.png'); //改变水印图片大小 50*50

        //把图片转成圆形
        $curcle_image = $this->curcle_image('image/xcx_code/2_qr_code.png', 'image/xcx_code/2_qr_code.png'); //把水印图片转成圆形,返回水印图片地址
         $image->water($curcle_image, [350, 30], 100)->save($curcle_image); //加图片水印后保存为 water_image.png

        //给图片加文字水印
        $image = \think\Image::open($curcle_image); //打开 要加水印的图片
        $image1 = $image->text('十年磨一剑 - 为API开发设计的高性能框架', Env::get('root_path').'public/static/HYQingKongTiJ.ttf', 20, '#000000', \think\Image::WATER_SOUTHEAST, 0, 50)->save('image/xcx_code/text.png'); //文字水印
        $image1 = $image->text('十年磨两剑 - 为API开发设计的高性能框架,高性能框架,高性能框架', Env::get('root_path').'public/static/HYQingKongTiJ.ttf', 20, '#000000',[100,10], 0, 0)->save('image/xcx_code/text.png'); //文字水印
    }


    //把图片 处理圆形
    public function curcle_image($url, $path = '../')
    {
    
      
        $w = 50;
        $h = 50; // 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);
        imagepng($newpic, $dest_path);
        imagedestroy($newpic);
        imagedestroy($src);
        // unlink($url);  
        return $dest_path;
    }
}

在这里插入图片描述

Guess you like

Origin blog.csdn.net/weixin_45703155/article/details/121827322