2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

外部Webサーバーにある画像ファイルをプログラミングレスでリサイズする

2
Last updated at Posted at 2022-09-21

はじめに

外部のWebサーバー(というかIPカメラ)がCGIを持っており、

http://ID:PASS@globalipcam:51234/path1/path2/snap.jpg

というURLでスナップ画像が取得できる。

この時ダウンロードできた画像ファイルの解像度は WQHD(2560x1440)。ちょっと大きめ。

変換前.png

このスナップ画像を定期的に取得してVGAで保存したい要件が出てきた。

以下の記事でNginxの設定だけでグローバルIPアドレスを取得する方法を記載したが、画像ファイルのリサイズも似たような方法で実現できないだろうか?と思い立ち調べてみた。

環境

お試し環境は以下の通り。

環境
Ubuntu 20.04.5 LTS
Nginx 1.18.0
  • 必要なNginxモジュールは「libnginx-mod-http-image-filter」だったが「インストール済み」になっていたので、標準で入るモジュールなのかもしれない。

結論

以下の設定だけで実現できた。

/etc/nginx/sites-enabled/default

vi /etc/nginx/sites-enabled/default
upstream globalipcam {
        server "globalipcam:51234";
}

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        location /globalipcam_resize_snap {
            proxy_set_header Authorization "Basic SUQ6UEFTUw==";
            proxy_pass http://globalipcam/path1/path2/snap.jpg;
            image_filter resize 640 360;
            image_filter_jpeg_quality 80;
        }
}

説明

upstream globalipcam {
        server "globalipcam:51234";
}
  • ポート番号が80以外の場合はupstreamを利用する必要がある模様。80なら次の「proxy_pass」だけで実現できる、と思う。
        location /globalipcam_resize_snap {
            proxy_set_header Authorization "Basic SUQ6UEFTUw==";
            proxy_pass http://globalipcam/path1/path2/snap.jpg;
            image_filter resize 640 360;
            image_filter_jpeg_quality 80;
        }
  • IPカメラ側にBasic認証が施されているので「proxy_set_header Authorization」にて設定。
  • 「proxy_pass」にある"globalipcam"は「upstream」の名前。
  • 「image_filter」で必要なパラメータ値を指定。"crop"などもできる模様。

URL

上記設定&reload後に、ブラウザから以下のURLにアクセスすると所望した解像度の画像ファイルが表示される。

http://local_nginx_server_ip/globalipcam_resize_snap

変換後.png

最後に

いやぁ~、Nginxってすごいですね。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?