Airson ùine mhòr a-nis, air a bhrosnachadh leis an artaigil
Thàinig an artaigil seo gu teasairginn
Mar sin dè a tha a dhìth oirnn
Thèid eisimpleirean a thoirt seachad stèidhichte air Debian.
Stàladh nginx agus nginx-extras
apt-get update
apt-get install nginx-extras
Stàladh LuaJIT
apt-get -y install lua5.1 luajit-5.1 libluajit-5.1-dev
A stàladh imagemagick
apt-get -y install imagemagick
agus leabharlannan magaidh dha, na mo chùis airson dreach 6
apt-cache search libmagickwand
apt-get -y install libmagickwand-6.q16-3 libmagickwand-6.q16-dev
Togail lua-imagick
Bidh sinn a’ clonadh an stòr (no a’ gabhail an zip agus ga dhì-phapadh)
cd ~
git clone https://github.com/isage/lua-imagick.git
cd lua-imagick
mkdir build
cd build
cmake ..
make
make install
Ma chaidh a h-uile càil gu math, faodaidh tu Nginx a rèiteachadh.
Bheir mi eisimpleir de rèiteachadh an aoigheachd backend, a tha, gu dearbh, an urra ri ath-mheudachadh. Tha e air a chuir air adhart leis an t-seirbheisiche aghaidh, cuideachd le Nginx, far a bheil caching a’ tachairt airson ùine sònraichte (làithean) agus rudan eile.
config backend nginx
# 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
}
}
Bidh na bha a dhìth (a 'leudachadh an ìomhaigh timcheall na h-oirean) a' tachairt a 'cleachdadh img:extent()
agus tha e air a mhìneachadh a’ cleachdadh am paramadair resize
le soidhne +
aig an deireadh.
Tha na roghainnean a leanas rim faighinn:
- WxH (Cùm co-mheas taobh, cleachd meud nas àirde)
- WxH ^ (Cùm co-mheas taobh, cleachd meud nas ìsle (bàrr))
- WxH! (Leig seachad co-mheas taobh)
- WxH + (Cùm co-mheas taobh, cuir crìochan taobh)
Clàr geàrr-chunntas le toraidhean ath-mheudachadh
Iarr paramadair uri
Meud ìomhaigh toraidh
?ath-mheudachadh=400×200
200 × 200
?ath-mheudachadh=400×200^
400 × 400
?ath-mheudachadh=400×200!
400 × 200 (Gun a bhith co-rèireach)
?ath-mheudachadh=400×200+
400 × 200 (Co-roinneil)
An toradh
A 'beachdachadh air cumhachd agus sìmplidh an dòigh-obrach seo, faodaidh tu rudan a chur an gnìomh le loidsig gu math iom-fhillte, mar eisimpleir, a' cur comharran-uisge no a 'cur an gnìomh cead le ruigsinneachd cuingealaichte. Gus faighinn a-mach comasan an API airson a bhith ag obair le ìomhaighean, faodaidh tu sùil a thoirt air sgrìobhainnean an leabharlainn
Source: www.habr.com