Home > Backend Development > PHP Problem > How does nginx add watermark to images through PHP proxy (detailed code)

How does nginx add watermark to images through PHP proxy (detailed code)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-03-16 10:58:02
forward
2421 people have browsed it

This article brings you relevant knowledge about how nginx adds watermarks to images through PHP agents, as well as related issues about how to call them, through code examples. , let’s take a look at it, I hope it will be helpful to everyone.

How does nginx add watermark to images through PHP proxy (detailed code)

(Recommended tutorials: nginx tutorial, PHP video tutorial)

nginx configuration proxy

location ~ /image/.*\.(gif|jpg|jpeg|png)$ {
    proxy_pass http://127.0.0.1:8888/test/watermark?url=$request_uri;
}
Copy after login

/img/ matches the directory prefix to be proxied, and the $request_uri parameter is the image path to be accessed.

php watermark class

/** 水印类
 * Class Watermark
 * @package app\test
 */
class Watermark
{
    /** 合成图片水印
     * @param string $dstImage 原图片
     * @param string $waterImg 水印图
     */
    public static function imageMarking($dstImage, $waterImg){
        //获取图片信息
        $dstInfo = getimagesize($dstImage);
        $waterInfo = getimagesize($waterImg);
        //创建图像
        $dstImgObj = self::imageCreateFrom($dstImage, $dstInfo[2]);
        $waterImgObj = self::imageCreateFrom($waterImg, $waterInfo[2]);
        //合成水印
        imagecopyresized($dstImgObj,$waterImgObj,0, 0,0,0,$dstInfo[0], $dstInfo[1],$waterInfo[0],$waterInfo[1]);
        //输出图片
        self::imageOut($dstImgObj,$waterInfo[2]);
        //销毁资源对象
        imagedestroy($dstImgObj);
        imagedestroy($waterImgObj);
    }
    /** 生成图片对象
     * @param string $imgFile 图片路径
     * @param string $type 图片类型
     * @return false|\GdImage|resource
     */
    private static function imageCreateFrom($imgFile, $type) {
        switch ($type) {
            case IMAGETYPE_GIF:
                return imagecreatefromgif($imgFile);
            case IMAGETYPE_JPEG:
                return imagecreatefromjpeg($imgFile);
            case IMAGETYPE_PNG:
                return imagecreatefrompng($imgFile);
            default :   //其他格式
        }
    }
    /** 输出图片
     * @param string $imageObj
     * @param string $type
     */
    private static function imageOut($imageObj,$type){
        switch ($type) {
            case 1:
                header("Content-Type: image/gif");
                imagegif($imageObj);
                break;
            case 2:
                header("Content-Type: image/jpeg");
                imagejpeg($imageObj);
                break;
            case 3:
                header("Content-Type: image/png");
                imagepng($imageObj);
                break;
            default: //其他格式
        }
    }
}
Copy after login

Call example

public function watermark(){
    //图片路径前缀
    $image = '/data/img/' . input('url');
    self::imageMarking($image,'watermark.png');
    exit;
}
Copy after login

nginx If you are good at it, you can try to use the http_image_filter_module module to add watermarks , This is also a helpless move for me

(Recommended tutorials: nginx tutorial, PHP video tutorial)

The above is the detailed content of How does nginx add watermark to images through PHP proxy (detailed code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template