javascript 使用nginx调整图像大小(这是替换phpThumb脚本)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 使用nginx调整图像大小(这是替换phpThumb脚本)相关的知识,希望对你有一定的参考价值。

// Image resize with nginx (this is replace phpThumb script)

function escapeRegExp(str) {
    return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}

function replaceAll(str, find, replace) {
    return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}

function isEmpty(str) {
    return (!str || 0 === str.length);
}

function js_get_uri_src_dirname(req, res) {
  var src, decoded;
  src = req.args["src"];
  if(isEmpty(src)) {
    decoded = "/s/images/no-photo.jpg";
  } else {
    decoded = decodeURIComponent(src);
  }
  var dirname = decoded.replace(/[^\\\/]*$/, '');
  return dirname;
}

function js_get_image_uri(req, res) {
  var uri, src, w, h, q, decoded, filename, dirname;
  src = req.args["src"];
  w = req.args["w"];
  h = req.args["h"];
  q = req.args["q"];
  if(isEmpty(src)) {
    decoded = "/s/images/no-photo.jpg";
  } else {
    src = replaceAll(src, "+", "%20");
    decoded = decodeURIComponent(src);
  }
  filename = decoded.replace(/^.*[\\\/]/, '');
  if(isEmpty(w)) {
    w = "150";
  }
  if(isEmpty(h)) {
    h = "150";
  }
  if(isEmpty(q)) {
    q = "90";
  }
  dirname = decoded.replace(/[^\\\/]*$/, '');
  uri = "image_resize/" + filename + "?width=" + w + "&height=" + h + "&path=" + dirname + "&quality=" + q;
  return uri;
}

function js_get_image_filename(req, res) {
  var image_filename, src, w, h, q, decoded, filename, dirname;
  src = req.args["src"];
  w = req.args["w"];
  h = req.args["h"];
  q = req.args["q"];
  if(isEmpty(src)) {
    decoded = "/s/images/no-photo.jpg";
  } else {
    src = replaceAll(src, "+", "%20");
    decoded = decodeURIComponent(src);
  }
  filename = decoded.replace(/^.*[\\\/]/, '');
  if(isEmpty(w)) {
    w = "150";
  }
  if(isEmpty(h)) {
    h = "150";
  }
  if(isEmpty(q)) {
    q = "90";
  }
  dirname = decoded.replace(/[^\\\/]*$/, '');
  image_filename = dirname + w + h + q + "_" + filename;
  return image_filename;
}

function js_get_resized_image_filename(req, res) {
  var resized_image_filename, src, w, h, q, decoded, filename, dirname;
  src = req.args["src"];
  w = req.args["w"];
  h = req.args["h"];
  q = req.args["q"];
  if(isEmpty(src)) {
    decoded = "/s/images/no-photo.jpg";
  } else {
    src = replaceAll(src, "+", "%20");
    decoded = decodeURIComponent(src);
  }
  filename = decoded.replace(/^.*[\\\/]/, '');
  if(isEmpty(w)) {
    w = "150";
  }
  if(isEmpty(h)) {
    h = "150";
  }
  if(isEmpty(q)) {
    q = "90";
  }
  dirname = decoded.replace(/[^\\\/]*$/, '');
  resized_image_filename = w + h + q + "_" + filename + "?path=" + dirname;
  return resized_image_filename;
}

以上是关于javascript 使用nginx调整图像大小(这是替换phpThumb脚本)的主要内容,如果未能解决你的问题,请参考以下文章

使用 javascript 画布调整图像大小(平滑)

在将图像分配给 img 标签之前使用 javascript 调整图像大小

easelJS - 使用 Javascript 调整图像大小,然后将其与 SpriteSheet 一起使用

在上传到服务器之前使用 JavaScript 在客户端调整图像大小

在 JavaScript 中调整 Base-64 图像的大小而不使用画布

ini Nginx图像过滤器调整代理服务大小