首頁 運維 Nginx Nginx代理輸出縮放圖片怎麼實現

Nginx代理輸出縮放圖片怎麼實現

May 16, 2023 pm 01:55 PM
nginx

nginx 設定檔:

# document ppt convert configuration.
upstream document.polyv.net {
 server 127.0.0.1:8080;
}

server {
 listen 80;
 server_name document.polyv.net;
 index index.html index.htm;
 charset utf-8;
 client_max_body_size 1000m;

 # ignore favicon.ico not exist.
 location = /favicon.ico {
  log_not_found off;
  access_log off;
 }

 # not allow to visit hidden files.
 location ~ /\. {
  deny all;
  access_log off;
  log_not_found off;
 }

 location / {
  if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$) {
   add_header content-disposition: 'attachment;';
   add_header content-type: 'application/octet-stream';
  }

  proxy_pass http://document.polyv.net;
  proxy_set_header x-real-ip $remote_addr;
  proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
  proxy_set_header request_host $host;

  # include proxy.conf;
  charset utf-8;
 }

 # user upload files
 location /images/ {
   #expires 7d;
  alias /data03/ovp/blobs/;
   proxy_store on;
   proxy_store_access user:rw group:rw all:rw;
   proxy_set_header accept-encoding "";
   if ( !-f $request_filename ) {
    proxy_pass http://document.polyv.net;
   }
 }

 location /blobs/ {
   #expires 7d;
  alias /data03/ovp/blobs/;
 }

  location /preview/images/ {
   #expires 7d;
   alias /data03/ovp/blobs/;
   proxy_store on;
   proxy_store_access user:rw group:rw all:rw;
   proxy_set_header accept-encoding "";
   if ( !-f $request_filename ) {
    proxy_pass http://document.polyv.net;
   }
  }

}
登入後複製

代理程式輸出縮放圖片

r​​rreee

圖片縮放的業務

package com.document.handle.controller;

import java.io.bufferedinputstream;
import java.io.file;
import java.io.ioexception;
import java.io.outputstream;
import java.net.httpurlconnection;
import java.net.url;

import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import org.apache.commons.lang3.stringutils;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.servletrequestutils;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.requestmapping;

import com.document.tool.imagemagickutils;
import com.document.tool.systemconfig;

@controller
public class imageagentcontroller {

 private static final logger log = loggerfactory.getlogger(imageagentcontroller.class);

 /**
  * ppt预览图片代理输出
  * @throws ioexception
  */
 @requestmapping("/preview/images/{year}/{month}/{md5id}/{preview}/{filename}.{ext}")
 public void cropimage(@pathvariable string year, @pathvariable string month, @pathvariable string md5id,
   @pathvariable string preview, @pathvariable string filename, @pathvariable string ext,
   httpservletrequest request, httpservletresponse response) throws ioexception {
  // string rootdir = "/data03/ovp/blobs/";
  string rootdir = systemconfig.getblobdirectory();
  string oname = filename.substring(1, filename.length());// 原图文件名
  string dirstring = rootdir + year + "/" + month + "/" + md5id + "/" + oname + "." + ext;
  string targetfilestring = rootdir + year + "/" + month + "/" + md5id + "/preview/" + filename + "." + ext;

  //如果原图存在
  file originimage = new file(oname);
  if(originimage.exists()){
   log.info("corpimage..." + dirstring + " -> " + targetfilestring);
   file newfile = new file(targetfilestring);
   string pathstring = newfile.getparent();
   log.info("pathstring...{} {}", pathstring);
   file pathfile = new file(pathstring);
   if (!pathfile.exists()) {
    log.info("---create file---");
    pathfile.mkdirs();
   }
   boolean status = imagemagickutils.scale(dirstring, targetfilestring, 240, 180);
   if (status) {
    response.reset();
    response.setcontenttype("image/" + ext);

    java.io.inputstream in = new java.io.fileinputstream(targetfilestring);
    // filenameurlutils.getimagefilename(targetfilestring);

    if (in != null) {
     byte[] b = new byte[1024];
     int len;
     while ((len = in.read(b)) != -1) {
      response.getoutputstream().write(b);
     }

     in.close();
    }
   }
  }else{
   log.info("原图目录不存在-preview:{}",dirstring); 
  }
 }


 /**
  * ppt固定尺寸图片代理输出
  * @throws ioexception
  * https://cache.yisu.com/upload/ask_collection/20210726/113/19154.png
  *
  * http://document.polyv.net/images/2016/03/de37d2ceb11ac068c18c5e4428541075/jpg-3.png
  */
 @requestmapping("/images/{year}/{month}/{md5id}/{filename}/{width}x{height}.{ext}")
 public void cropfixedimage(@pathvariable string year, @pathvariable string month, @pathvariable string md5id,
   @pathvariable string filename, @pathvariable integer width, @pathvariable integer height, @pathvariable string ext,
   httpservletrequest request, httpservletresponse response) throws ioexception {
  // string rootdir = "/data03/ovp/blobs/";
  string rootdir = systemconfig.getblobdirectory();
  //string oname = filename.substring(1, filename.length());// 原图文件名
  string dirstring = rootdir + year + "/" + month + "/" + md5id + "/" + ( filename + "." + ext);
  string targetfilestring = rootdir + year + "/" + month + "/" + md5id + "/" + filename + "/" + (width + "x" + height + "." + ext);

  //如果原图存在
  file originimage = new file(dirstring);
  if(originimage.exists()){
   file targetfilestringfile = new file(targetfilestring);
   if(!targetfilestringfile.exists()){
    log.info("corpimage..." + dirstring + " -> " + targetfilestring);
    file newfile = new file(targetfilestring);
    string pathstring = newfile.getparent();
    log.info("pathstring...{} {}", pathstring);
    file pathfile = new file(pathstring);
    if (!pathfile.exists()) {
     log.info("---create file---");
     pathfile.mkdirs();
    }
    imagemagickutils.resizewh(dirstring, targetfilestring,width,height);
   }
   response.setcontenttype("image/" + ext);
   java.io.inputstream in = null;
   try{
    in = new java.io.fileinputstream(targetfilestring);
    response.setcontentlength(in.available());
    byte[] buffer = new byte[1024];
    int count = 0;
    while ((count = in.read(buffer)) > 0) {
     response.getoutputstream().write(buffer, 0, count);
    }
    response.flushbuffer();
   }catch(exception e){
    e.printstacktrace();
   }finally {
    try {
     in.close();
    } catch (exception e) {

    }
   }
  }else{
   log.info("原图目录不存在:{}",dirstring);
  }




 }


 /**
  * 图片下载
  */
 @requestmapping("get/image/data")
 public void downloadimage(httpservletrequest request, httpservletresponse response) throws ioexception { 
  string filepath = servletrequestutils.getstringparameter(request, "filepath", ""); //图片访问路劲
  string filename = servletrequestutils.getstringparameter(request, "filename", ""); //名称

  if(stringutils.isnotblank(filepath) || stringutils.isnotblank(filename)){
   string desturl = filepath;
   //log.info("--------------"+filepath); 
   string fileformat=filepath.substring(filepath.lastindexof("."));
   //string name=filename.trim()+fileformat;
   string name=filepath.substring(filepath.lastindexof("/")+1, filepath.length()); 
   //file f = new file(filepath);
   //response.setheader("content-disposition", "attachment; filename="+java.net.urlencoder.encode(f.getname(),"utf-8")); 
   //log.info("--------------"+f.getname());

   // 建立链接 
   url url = new url(desturl); 
   httpurlconnection httpurl = (httpurlconnection) url.openconnection(); 
   // 连接指定的资源 
   httpurl.connect(); 
   // 获取网络输入流 
   bufferedinputstream bis = new bufferedinputstream(httpurl.getinputstream()); 

   integer lenf=httpurl.getcontentlength();
   //string lenf=this.getfilelength(4189053, 7189053);
   response.setcontenttype("application/x-msdownload"); 
   response.setheader("content-length", lenf.tostring());//文件大小值5几m
   response.setheader("content-disposition", "attachment; filename="+java.net.urlencoder.encode(name,"utf-8"));
   outputstream out = response.getoutputstream();
   byte[] buf = new byte[1024]; 
   if (desturl != null) { 
    bufferedinputstream br = bis; 
    int len = 0; 
    while ((len = br.read(buf)) > 0){ 
     out.write(buf, 0, len); 
    }     
    br.close(); 
   } 
   out.flush(); 
   out.close(); 
  }

 } 

}
登入後複製

伺服器上要安裝imagemagick。

以上是Nginx代理輸出縮放圖片怎麼實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

tomcat伺服器怎麼能讓外網訪問 tomcat伺服器怎麼能讓外網訪問 Apr 21, 2024 am 07:22 AM

tomcat伺服器怎麼能讓外網訪問

nginx啟動指令和停止指令是什麼 nginx啟動指令和停止指令是什麼 Apr 02, 2024 pm 08:45 PM

nginx啟動指令和停止指令是什麼

thinkphp怎麼運行 thinkphp怎麼運行 Apr 09, 2024 pm 05:39 PM

thinkphp怎麼運行

Welcome to nginx!怎麼解決? Welcome to nginx!怎麼解決? Apr 17, 2024 am 05:12 AM

Welcome to nginx!怎麼解決?

phpmyadmin怎麼註冊 phpmyadmin怎麼註冊 Apr 07, 2024 pm 02:45 PM

phpmyadmin怎麼註冊

nodejs專案怎麼部署到伺服器 nodejs專案怎麼部署到伺服器 Apr 21, 2024 am 04:40 AM

nodejs專案怎麼部署到伺服器

造訪網站出現nginx怎麼解決 造訪網站出現nginx怎麼解決 Apr 02, 2024 pm 08:39 PM

造訪網站出現nginx怎麼解決

docker容器之間如何通信 docker容器之間如何通信 Apr 07, 2024 pm 06:24 PM

docker容器之間如何通信

See all articles