App back end Design (12)--image processing. docx

Source: Internet
Author: User
Tags imagemagick

App on-line, and constantly accept user feedback, so, very poor feedback, there will be app revision.

Once the app's revision, there will be a larger UI changes, a change in the UI, then the size of the picture must be changed.

In the app backend design (1)-api (http://blog.csdn.net/newjueqi/article/details/14053733) In this article, I mentioned a basic principle of app background image processing, where only the path of the original is saved in the database. For the same picture, for different models, different app versions need different sizes, the use of dynamically generated strategies, the general idea is as follows:

(1) Add parameters at the end of the URL of the image to declare the new dimensions of the image that need to be generated, for example: the size of the 80*80 of the user's image (Http://www.baidu.com/img/bdlogo.gif), Then the path of the picture plus the width and height of the parameters (similar to the CDN mechanism) Http://www.baidu.com/img/bdlogo.gif? W=80&h=80

(2) The server receives a picture request, first in the cache to find the size of the picture has been generated, if there is a record in the cache, you do not have to regenerate.

(3) If a picture of this size has not yet been generated, create a new picture size and place the newly generated picture path in the cache.

In the app's entire system architecture, the picture should have a two-tier cache:

(1) App local image cache, when the app does not have the picture, only go to the service to take

(2) The image cache of the server, record the different size of the picture save path

My suggestion is that, if the money is not bad, directly using the seven Cow cloud storage services, cloud storage can not only speed up the download of images upload, but also to achieve a large number of pictures operation. You know, speed is the most direct part of the user experience.

If you really want to realize the cutting of the picture, then take into account that the picture operation is very CPU, memory, and a lot of disk IO, so in the selection of image processing tools to be cautious!!!

It is recommended to use GraphicsMagick, a proven image processing software that supports multiple platforms and supports multi-lingual customer service. GraphicsMagick is a branch of ImageMagick, which is faster and consumes less resources than ImageMagick, and large image processing sites such as Flickr and Etsy are already using TA.

When using GraphicsMagick, the most frustrating is how to match graphicsmagick environment, consulted a large number of articles, are noted in Linux can not use Cmd.setsearchpath (path); , but after my experiment, is OK, and with this, you can let Linux and win run the same piece of code, just put the path in the configuration file.

Below I wrote the Graphicsmagick+im4java Picture clipping tool class,


/** * */package com.bmob.worker.image;import java.awt.image.bufferedimage;import Java.io.FileInputStream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstream;import java.util.concurrent.executionexception;/** */public class Image {/* 1, specified width, high adaptive, proportional scaling, 2, specified high, wide adaptive, proportional scaling, 3, specifying the longest edge, Short Edge adaptive, proportional scaling; 4, specify the shortest side, long edge adaptive, such as proportional scaling; 5, specify the maximum width and height, and other proportional scaling; 6, Fixed width high, center cutting) */public static int definewidth=1; public static int defineheight=2; public static int definelong=3; public static int defineshort=4; public static int maxwidthheight=5; public static int definewidthheight=6; /** * Image Scaling Method * * @param mode 1, specify wide, high adaptive, proportional scaling; 2, the specified high, wide adaptive, such as proportional scaling; 3, specify the longest side, short edge adaptive, such as proportional scaling; 4, specify the shortest side, long edge adaptive, such as proportional scaling; 5, specify the maximum width and height, and other proportional scaling; 6, Fixed width high, center cropping) * @param src source file path * @param desc target file path * @param width Specify width * @param height Specify high * @param maxframe specify the longest edge * @pa Ram Minframe Specifies the shortest edge * @return * @throws Exception */public string Resize (int mode, string src,string desc, int width, int Height, int maxframe, int minframe) tHrows Exception {String str= ""; Bhpapplication.init ();//create Commandconvertcmd cmd = This.getcmd (); imoperation op =null;if (mode==image.definewidth ) {Op=this.resizedefinewidth (Src,desc, width, height);} else if (mode==image.defineheight) {op=this.resizedefineheight (Src,desc, width, height);} else if (Mode==image.definelong) {Op=this.resizedefinelong (Src,desc, maxframe);} else if (mode==image.defineshort) {op=this.resizedefineshort (Src,desc, minframe);} else if (mode==image.maxwidthheight) {op=this.resizemaxwidthheight (Src,desc, width, height);} else if (mode==image.definewidthheight) {op=this.resizedefinewidthheight (Src,desc, width, height);} Cmd.run (OP); return str;}  Specify wide, high-adaptive, proportional scaling; public imoperation resizedefinewidth (String src,string desc, int width, int height) {imoperation op = new Imoperation (); op.addimage (SRC); op.resize (width,null); op.addimage (desc); return op;} Specify high, wide adaptive, proportional scaling; public imoperation resizedefineheight (String src,string desc, int width, int height) {imoperation op = new Imoperation (); op.addimage (SRC); op.resize (null,height); op.addimage (desc); return op;} Specifies the longest edge, the short edge adaptive, and so on scale; public imoperation Resizedefinelong (String src,string desc, int maxframe) throws exception{ InputStream is = new FileInputStream (src);//Read BufferedImage buff = Imageio.read (is) by file name; int Srcwidth=buff.getwidth ()  ;//Get the width of the picture int srcheight=buff.getheight (); Get the height of the picture is.close (); Close Streamimoperation op = new imoperation (); op.addimage (SRC); if (srcwidth>srcheight) {op.resize (maxframe,null);} Else{op.resize (null,maxframe);} Op.addimage (DESC); return op;} Specifies the shortest edge, long edge adaptive, equal scale scaling; public imoperation resizedefineshort (String src,string desc, int minframe) throws Exception { InputStream is = new FileInputStream (src);//Read BufferedImage buff = Imageio.read (is) by file name; int Srcwidth=buff.getwidth ()  ;//Get the width of the picture int srcheight=buff.getheight (); Get the height of the picture is.close (); Close Streamimoperation op = new imoperation (); op.addimage (SRC); if (srcwidth<srcheight) {op.resize (minframe,null);} Else{op.resize (null,minframe);} Op.addimage (DESC); return op;} Specifies the maximum width, scale, and scale; public imoperation resizemaxwidthheight (String src,string desc, int width, int height) {imoperation op = n EW imoperation (); op.addimage (SRC); Op.resize (Width,height, '! '); o P.addimage (DESC); return op;} Fixed width height, center crop public imoperation resizedefinewidthheight (String src,string desc, int width, int height) {imoperation op = ne  W imoperation (); op.addimage (SRC); op.gravity ("center"). Extent (width, height); Op.addimage (DESC); return op;} Public Convertcmd Getcmd () {convertcmd cmd = new Convertcmd (true);//set true, use graphicsmagickstring path = "/usr/local /graphicsmagick/bin "; GraphicsMagick Installation path Cmd.setsearchpath (path); return cmd;}}



a new "App backend technology" chat QQ Group: 254659220

[Article author] Zeng Jiansen

[Author Email] [Email protected]

[Author qq]190678908

[Sina Weibo] @newjueqi

[Blog]http://blog.csdn.net/newjueqi


App backend Design (12)--processing of pictures. docx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.