Nam satis iam dudum, inspirata art
Hoc articulum ad liberandum
Quid ergo opus est?
Exempla ex Debian dabuntur.
Installing nginx et nginx-extras
apt-get update
apt-get install nginx-extras
installing LuaJIT
apt-get -y install lua5.1 luajit-5.1 libluajit-5.1-dev
Installing imagemagick
apt-get -y install imagemagick
et bibliothecas magicwand ad illud, in mea causa pro versione 6
apt-cache search libmagickwand
apt-get -y install libmagickwand-6.q16-3 libmagickwand-6.q16-dev
lua-imaginatio Building
Reconditorium clone (seu zip et EXPEDIO accipere)
cd ~
git clone https://github.com/isage/lua-imagick.git
cd lua-imagick
mkdir build
cd build
cmake ..
make
make install
Si bene omnia sunt, configurare Nginx potes.
Exemplum dabo Mandi hostiae backendi, quae re vera est resipiscendi. Imitatur a fronte servo, etiam cum Nginx, ubi caching certum tempus (dierum) et alia occurrit.
nginx backend config
# Backend image server
server {
listen 8082;
listen [::]:8082;
set $files_root /var/www/example.lh/frontend/web;
root $files_root;
access_log off;
expires 1d;
location /files {
# Π΄Π΅ΡΠΎΠ»ΡΠ½ΡΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ ΡΠ΅ΡΠ°ΠΉΠ·Π°
set $w 700;
set $h 700;
set $q 89;
#1-89 allowed
if ($arg_q ~ "^([1-9]|[1-8][0-9])$") {
set $q $arg_q;
}
if ($arg_resize ~ "([d-]+)x([d+!^]+)") {
set $w $1;
set $h $2;
rewrite ^(.*)$ /resize/$w/$h/$q$uri last;
}
rewrite ^(.*)$ /resize/$w/$h/$q$uri last;
}
location ~* ^/resize/([d]+)/([d+!^]+)/([d]+)/files/(.+)$ {
default_type 'text/plain';
set $w $1;
set $h $2;
set $q $3;
set $fname $4;
# ΠΡΡΡ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΡΡΡ Π²ΡΠ½Π΅ΡΡΠΈ Π²Π΅ΡΡ Lua ΠΊΠΎΠ΄ Π² ΠΎΡΠ΄Π΅Π»ΡΠ½ΡΠΉ ΡΠ°ΠΉΠ»
# content_by_lua_file /var/www/some.lua;
# lua_code_cache off; #dev
content_by_lua '
local magick = require "imagick"
local img = magick.open(ngx.var.files_root .. "/files/" .. ngx.var.fname)
if not img then ngx.exit(ngx.HTTP_NOT_FOUND) end
img:set_gravity(magick.gravity["CenterGravity"])
if string.match(ngx.var.h, "%d+%+") then
local h = string.gsub(ngx.var.h, "(%+)", "")
resize = ngx.var.w .. "x" .. h
-- Π΄Π»Ρ png Ρ Π°Π»ΡΡΠ° ΠΊΠ°Π½Π°Π»ΠΎΠΌ
img:set_bg_color(img:has_alphachannel() and "none" or img:get_bg_color())
img:smart_resize(resize)
img:extent(ngx.var.w, h)
else
img:smart_resize(ngx.var.w .. "x" .. ngx.var.h)
end
if ngx.var.arg_q then img:set_quality(ngx.var.q) end
ngx.say(img:blob())
';
}
}
# Upstream
upstream imageserver {
server localhost:8082;
}
server {
listen 80;
server_name examaple.lh;
# ΠΎΡΠΏΡΠ°Π²Π»ΡΠ΅ΠΌ Π²ΡΠ΅ jpg ΠΈ png ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΈ Π½Π° imageserver
location ~* ^/files/.+.(jpg|png) {
proxy_buffers 8 2m;
proxy_buffer_size 10m;
proxy_busy_buffers_size 10m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://imageserver; # Backend image server
}
}
Quod erat faciendum (expansus imaginem circa margines) accidit utens img:extent()
et definitur per modulum resize
cum signo +
in termino.
Optiones sequentes praesto sunt:
- WxH.
- WxH^ (Rationem aspectum custodi, dimensio inferioris usus))
- WxH! (Ignorare aspect-ratio)
- WxH+.
Summarium mensam cum resize proventuum
Request uri parametri
Output Image Location
? Resize = DC CD
200 Γ 200
?resize=400Γ200^
400 Γ 400
?resize=400Γ200!
400Γ200 (non proportionales)
?resize=400Γ200+
400Γ200 (Proportional)
exitum
Considerans hanc vim ac simplicitatem aditus, res efficere potes cum logica admodum multiplici, exempli gratia, additis notis vel auctoritati limitatis accessu exsequendo. Ut facultates API investigandi cum imaginibus operandi, ad documentum bibliothecae referre potes
Source: www.habr.com