1

I use the following configuration block

location ~* \.(apng)$ 
{
 try_files $uri $uri/ /getbait.php$args;
 add_header  Cache-Control public;
 add_header Cache-Control must-revalidate;
 expires 7d;
}

to ensure that all requests for APNG images to my server get processed by a PHP script which first loads them in Imagick, places a watermark on the image and then outputs the result.

This works just fine. However, I know have a slightly different requirement. The configuration block above as it stands is catch all. I need to modify it so the watermark placed is different depending on the exact request path. So for example

https://example.com/watermark/example.apng 

would spit out an image bearing one watermark whilst

https://example.com/sample/example.apng

would spit out an image bearing a different watermark. I know that I need to modify the location ~* \ bit of my directive above and specify wildcard APNG paths to two different folders with two different control blocks. However, my knowledge of Nginx configuration and the relevant RegEx is quite simply not up to that challenge. Hopefully, someone here can help me out.

0

I was going to delete this question but thought I would leave it here with the solution I eventually found for the benefit of others seeking to do similar things

Here is what eventually worked for me

location ~* ^/path/to/dirA/.+\.(apng)$
{
 add_header Access-Control-Allow-Origin "*";
 add_header  Cache-Control public;
 add_header Cache-Control must-revalidate;
 expires 7d;
 try_files $uri $uri/ /phpscriptA.php$args;    
}

I should explain that in my context the images here are accessed once only since the actual images are delivered by a CDN. The only time this URL is accessed directly is when the CDN does its origin pull

0

The simple solution to this would just be to look at $_SERVER['REQUEST_URI'] in the PHP script.

Also, if you're not already, and this gets any sort of traffic, it would be wise to cache the generated images somewhere. Ideally in the correct path so that the try_files directive finds them without having to run any PHP code if the image has already been generated.

Edit: As an example -

The existing config matches any .apng file and looks for the file, or sends to a php script.

location ~* \.(apng)$ 
{
 try_files $uri $uri/ /getbait.php$args;
 add_header  Cache-Control public;
 add_header Cache-Control must-revalidate;
 expires 7d;
}

In getbait.php you could do something like the following:

// REQUEST_URI should contain something like /path/to/watermark/imagename.apng

if( preg_match('/\/([^\/]+)\/([^\.]+.apng)$/', $_SERVER['REQUEST_URI'], $imageData) ){

    // $imageData[1] should now be watermark
    // $imageData[2] should be imagename.apng

    // At this point you can use a switch statement like below,
    // or an if statement, look up the watermark in some database,
    // or even just use $imageData[1] directly as the filename of the
    // watermark to load - if you're using an image overlay. (that way adding 
    // new watermarks is just a case of uploading the new watermark overlay 
    // image)

    switch($imageData[1]){
        case 'watermark1':
            // code
            break;
        case 'watermark2':
            // code
            break;
    }
}
5
  • Thank you for the answer but I fail to see the relevance to my question. I had in the mean time figured out how to do this. See my answer below
    – DroidOS
    Jan 11 '18 at 12:44
  • I appreciate the question was about nginx config, but your requirement was to generate different watermarks based on the image path requested. My answer suggested that the easiest way to do this would be to just look at the path in the PHP script itself, rather than complicating the nginx config. It looks like you've just ended up creating a separate location block for each unique watermark which may work for you, but is more awkward if there's a lot of different watermarks, they change often, you want it to be dynamic and map the path to a watermark in a database, etc.
    – USD Matt
    Jan 12 '18 at 11:00
  • The beauty of using a PHP script to return the watermark is that you do not need separate sets of watermarked images for the different folder locations - which in fact need not be real folders at all! All you need to do is use different scripts, source the same base image and then overlay a different water mark in each script! That is what I am doing. The water marked images only ever get pulled once by my CDN so it is not like those scripts run too often.
    – DroidOS
    Jan 12 '18 at 11:10
  • 1
    I don't really get this last comment. I'm not suggesting changing anything about how the original process works. Your original config matched any .apng file, and sent the request to a PHP script. I'm just suggesting modifying that PHP script to extract the path from the request data so it knows which watermark to use. It's still using the PHP script and you only need the one script and existing nginx config to handle whatever watermarks you need.
    – USD Matt
    Jan 12 '18 at 11:15
  • OK - I understand now. You do have a very valid point. Having just the one PHP script to maintain will avoid issues in the future.
    – DroidOS
    Jan 12 '18 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.