Appbackend design (122.16--image processing .docx,

Source: Internet
Author: User
Tags imagemagick

Appbackend design (122.16--image processing .docx,

After the app is launched, it constantly receives feedback from users. If the feedback is very poor, there will be app revisions.

 

Once the app is revised, there will be relatively large UI changes. Once the UI is changed, the image size must be changed.

 

In the app backend design (1)-api (http://blog.csdn.net/newjueqi/article/details/14053733) This article, I mentioned a basic principle of app background image processing, the database only saves the path of the source image. For the same image, the general idea is as follows:

 

(1) Add a parameter at the end of the image url to declare the new size of the image to be generated, for example, the user needs the 80*80 size of the image (http://www.baidu.com/img/bdlogo.gif, then add the width and height parameters in the path of the image (similar to the CDN mechanism) http://www.baidu.com/img/bdlogo.gif? W = 80 & h = 80

(2) When the server receives an image request, it first finds out in the cache whether the image of this size has been generated. If there are records in the cache, it does not need to be regenerated.

(3) If the image size has not yet been generated, a new image size will be generated and the new image path will be cached.

 

 

In the entire app system architecture, the image should have two layers of cache:

(1) The local image cache of the app. When the app does not have the image, the app can fetch it.

(2) The image cache of the server records the storage paths of images of different sizes.

 

My suggestion is that, if you don't have enough money, you can directly use the cloud storage service of qiniu. Cloud storage can not only accelerate the download and upload of images, but also implement a large number of operations on images. You must know that speed is the most direct part of user experience.

 

If you really want to crop images by yourself, consider that image operations consume a lot of CPU, memory, and a lot of disk I/O, so be careful when selecting an image processing tool !!!

 

GraphicsMagick is recommended. It is a tested image processing software that supports multiple platforms and multi-language customer services. GraphicsMagick is a branch of ImageMagick. Compared with ImageMagick, TA processing speed is faster, consumes less resources, and large image processing websites such as Flickr and Etsy are already using TA.

 

When using GraphicsMagick, the most frustrating thing is how to configure the GraphicsMagick environment. I have read a lot of articles and noted that cmd cannot be used in linux. setSearchPath (path, you just need to put the path in the configuration file.

 

The GraphicsMagick + Im4java image cropping tool class I wrote below,


/*****/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. Specify the width, height adaptive, and proportional scaling; 2. Specify the height, width adaptive, and proportional scaling; 3. Specify the longest side, short side adaptive, proportional scaling; 4. Specify the shortest side, long side adaptive, proportional scaling; 5. specify the maximum width and height, and proportional scaling; 6. fixed width and height, center crop) */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 the width, height adaptive, and proportional scaling. 2. Specify the height, width adaptive, and proportional scaling; 3. Specify the longest side, short side adaptive, proportional scaling; 4. Specify the shortest side, long side adaptive, proportional scaling; 5. specify the maximum width and height, and proportional scaling; 6. fixed width and height, center cropping) * @ param src Source file path * @ param desc target file path * @ param width specifying width * @ param he Ight specifies the height * @ param maxFrame specifies the longest side * @ param minFrame specifies the shortest side * @ 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. re SizeDefineWidthHeight (src, desc, width, height);} cmd. run (op); return str;} // specify the width, high adaptability, and 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 height, adaptive width, and 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;} // specify the longest side, short side adaptive, proportional scaling; public IMOperation resizeDefineLong (String src, String desc, int maxFrame) throws Exception {InputStream is = new FileInputStream (src); // read BufferedImage buff = ImageIO by file name. read (is); int srcWidth = buff. getWidth (); // get the image width int srcHeight = buff. getHeight (); // obtain the image height 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;} // specify the shortest side, adaptive long side, and proportional scaling; public IMOperation resizeDefineShort (String src, String desc, int minFrame) throws Exception {InputStream is = new FileInputStream (src); // read BufferedImage buff = ImageIO by file name. read (is); int srcWidth = buff. getWidth (); // get the image width int srcHei Ght = buff. getHeight (); // obtain the image height 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;} // specify the maximum width and height, proportional scaling; public IMOperation resizeMaxWidthHeight (String src, String desc, int width, int height) {IMOperation op = new IMOperation (); op. addImage (src); op. resize (width, height ,'! '); Op. addImage (desc); return op;} // fixed width and height, center crop public IMOperation resizeDefineWidthHeight (String src, String desc, int width, int height) {IMOperation op = new 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 ;}}



New "app backend technology" chat QQ group: 254659220

[Author] Zeng jiansheng

[Author email] h6k65@126.com

[Author QQ] 190678908

[Sina Weibo] @ newjueqi

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


Related Article

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.