A
A
Alexander2016-12-23 11:55:17
Nginx
Alexander, 2016-12-23 11:55:17

How to rename a file on upload in Nginx?

Lord, help me. There is a site, there are pictures in it, there is a resizing of these pictures. The resize link looks like site.ru/images/2016/12/22/7c3b090855d3ec4c92722e6d276a8c02.jpg:h250 , i.e. via : sets the size parameter. And everything would be fine, but only when saving the image, it is assigned the extension .jpg-h250 .
I tried this build:

location ~* ^/.+?\.(jpe?g|JPE?G|png|PNG)\:(\d+x\d+|[hw]\d+)$ {
        rewrite ^/(.*)/([^/]+?)\.(jpe?g|JPE?G|png|PNG)\:(\d+x\d+|[hw]\d+)$ /$1/resized-$4/$2.$3 break;
        root /var/www/site/www/static;

        if ($request_filename ~ "^.*/(.+):(.+)$"){
            set $fname $1;
            add_header 'Content-Disposition' 'inline;filename="$fname"';
        }
}

Does not help. I ask for advice

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2016-12-23
@ushliy

It's decided.
Now, when requesting the site.ru/images/2016/12/22/7c3b090855d3ec4c92722e6d276a8c02.jpg:h250 type, a file with the required size is first generated, placed in a folder with a name corresponding to the parameter, and then redirected along the site.ru/images/2016/ type path 12/22/resized-h250/7c3b090855d3ec4c92722e6d276a8c02.jpg The config
looks like this:

location ~* ^/.+?\.(jpe?g|JPE?G|png|PNG)\:(\d+x\d+|[hw]\d+)$ {
        rewrite ^/(.*)/([^/]+?)\.(jpe?g|JPE?G|png|PNG)\:(\d+x\d+|[hw]\d+)$ /$1/resized-$4/$2.$3 permanent;
        root /var/www/site/www/static;
        try_files $uri @resize_image;
    }

A
akzhan, 2016-12-23
@akzhan

just change the file naming convention in nginx to let the width and height be subfolders, roughly speaking.

/uploads/640/480/great_trump.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question