Created
February 19, 2019 15:26
-
-
Save lugrus2000/587bdadc27f9b4810a513c68f0033b63 to your computer and use it in GitHub Desktop.
Image resize with nginx (this is replace phpThumb script)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment