N'ihi na ezi oge ụfọdụ ugbu a, sitere n'ike mmụọ nsọ akụkọ
Akụkọ a gbatara ọsọ enyemaka
Yabụ kedu ihe anyị chọrọ
A ga-enye ihe atụ dabere na Debian.
Ịwụnye nginx na nginx-extras
apt-get update
apt-get install nginx-extras
Ịwụnye LuaJIT
apt-get -y install lua5.1 luajit-5.1 libluajit-5.1-dev
Ịwụnye imagemagick
apt-get -y install imagemagick
na ụlọ akwụkwọ anwansi Maka ya, n'ọnọdụ m maka ụdị 6
apt-cache search libmagickwand
apt-get -y install libmagickwand-6.q16-3 libmagickwand-6.q16-dev
Ụlọ lua-imagick
Anyị na-emechi ebe nchekwa ahụ (ma ọ bụ were zip wee bupụ ya)
cd ~
git clone https://github.com/isage/lua-imagick.git
cd lua-imagick
mkdir build
cd build
cmake ..
make
make install
Ọ bụrụ na ihe niile gara nke ọma, ị nwere ike hazie Nginx.
M ga-enye ihe atụ nke nhazi nke backend host, nke, n'ezie, na-ahụ maka ịmegharị. Onye nkesa n'ihu na-akwado ya, yana Nginx, ebe caching na-eme maka oge ụfọdụ (ụbọchị) na ihe ndị ọzọ.
nginx backend nhazi
# 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
}
}
Ihe achọrọ (ịgbasa onyonyo gburugburu n'akụkụ) na-eme site na iji img:extent()
ma kọwaa ya site na iji paramita resize
ya na akara +
na njedebe.
Nhọrọ ndị a dị:
- WxH (Dobe akụkụ-akụkụ, jiri akụkụ dị elu)
- WxH^ ( Debe akụkụ-akụkụ, jiri akụkụ dị ala (ihe ọkụkụ))
- WxH! (Leghara akụkụ-oke)
- WxH+ (Debe akụkụ-akụkụ, gbakwunye oke akụkụ)
Tebụlụ nchịkọta nwere nsonaazụ ngbanwe
Rịọ oke uri
Nha onyonyo mpụta
?nha = 400×200
200 × 200
?nha =400×200^
400 × 400
?nha =400×200!
400×200 (Ọ bụghị nha nha)
?nha =400×200+
400×200 (nke nhata)
Nsonaazụ
N'ịtụle ike na ịdị mfe nke usoro a, ị nwere ike mejuputa ihe nwere ezi uche dị mgbagwoju anya, dịka ọmụmaatụ, ịgbakwunye watermarks ma ọ bụ itinye ikike na ohere nwere oke. Iji chọpụta ike API na-arụ ọrụ na onyonyo, ị nwere ike ịtu aka na akwụkwọ ọba akwụkwọ
isi: www.habr.com