Backend Development 3 min read

Configuring Nginx Proxy for Image Watermarking with a PHP Watermark Class

This article demonstrates how to set up an Nginx location block that proxies image requests to a PHP watermark service, provides a complete PHP Watermark class implementation, and shows a usage example for applying watermarks to images on the fly.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Configuring Nginx Proxy for Image Watermarking with a PHP Watermark Class

This guide explains how to configure Nginx to proxy image requests to a PHP service that adds watermarks, and includes a full PHP Watermark class implementation.

nginx configuration

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

The /img/ prefix identifies the directory to be proxied, while the $request_uri variable passes the original image path to the backend.

PHP Watermark class

/** Watermark class */
class Watermark {
    /** Composite image watermark */
    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);
    }
    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: /* other formats */
        }
    }
    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: /* other formats */
        }
    }
}

Usage example

public function watermark() {
    // Image path prefix
    $image = '/data/img/' . input('url');
    self::imageMarking($image, 'watermark.png');
    exit;
}

The article also mentions that the http_image_filter_module can be used for watermarking directly in Nginx, but the presented PHP solution is a practical fallback.

proxyimage processingConfigurationWatermarknginx
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.