Image server with AWS + Nginx


link

 

 

Dynamic image distribution

receive http request, then return the image.

  1. look up requested img on local (h80-piyo.jpg),
    if it exists, return it でしゅ~りょ~
  2. if requested img doesn’t exist on local, send request to imageHandler module
    1. look up base img (piyo.jpg)
      if it doesn’t exist, download from S3, then save to local
    2. convert base img (h80-piyo.jpg) -> requested img (h80-piyo.jpg), then save to local
  3. return requested img (h80-piyo.jpg)

 

HTTP request

$DOMAIN/$DIR/$BUCKET/$meta_info-$file.$ext?h=$hash
var desc
DOMAIN img.hidepiy.comとかね
DIR ディレクトリ名 掘る機能はOFFにしてるから、手動で掘ってね☆
BUCKET bucket name of S3
meta info set meta info how to convert image
file file name
ext file’s extention
hash used as easy password

 

hash

sha1($secret_key$meta_info-$FILE.$ext)

 

mode (set in meta_info)

mode example hogehoge
original piyo.jpg or o-piyo.jpg
screen sffff00-piyo.jpg ffff00のように16進数で色指定でスクリーン合成
crop c100x100-piyo.jpg M100x100でリサイズしたあとに、はみ出たとこcrop
CROP C100x100-piyo.jpg 100*100を切り出す
min resize m100x100-piyo.jpg 縦横比キープ、100*100に画像が納まるようにリサイズ
MAX resize M100x100-piyo.jpg 縦横比キープ、100*100の四角を画像が隠すようにリサイズ
no propotion n100x100-piyo.jpg 縦横比を捨てて100*100にリサイズ
width w80-piyo.jpg 縦横比をキープしつつ、横幅80にリサイズ
WIDTH W80-piyo.jpg 縦はそのまんま横幅80にリサイズ
height w80-piyo.jpg 縦横比をキープしつつ、高さ80にリサイズ
HEIGHT W80-piyo.jpg 横はそのまんま高さ80にリサイズ
remove r-piyo.jpg remove piyo.jpg *-piyo.jpg
  • c100x100-sffff00-piyo.jpg のように、screenは他のモードにくっつけられる
  • 100×100 ← こ~ゆ~のは width x height
  • 見てのとおり、ファイル名に “-”(ハイフン)は使えない。アンスコとかでもいいかもね

 

sample

screen
doraemon.jpg(180×240) sffc0cb-doraemon.jpg s008000-doraemon.jpg
crop
c100x100-doraemon.jpg C100x100-doraemon.jpg
   
resize
m100x100-doraemon.jpg M100x100-doraemon.jpg n100x100-doraemon.jpg
 
width
w80-doraemon.jpg W80-doraemon.jpg
 
height
h80-doraemon.jpg H80-doraemon.jpg
 

 

set-up

nginx.conf

 

imageHandler

perl module, using Imager library inside.

CPAN Imager : http://search.cpan.org/~tonyc/Imager-0.91/Imager.pm

I’ve tried or tried to try these,
Imlib2, ImageMagick, libd, then the Imager is the best for my use.

 

Outroduction

 

Leave a comment

Your email address will not be published. Required fields are marked *