How nginx adds watermark to images through PHP proxy

PHPz
Release: 2023-05-29 08:37:11
forward
1662 people have browsed it

How nginx adds watermark to images through PHP proxy

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, $request_uri parameter is accessed Image path.

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

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

Related labels:
source:yisu.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template