Install Grunt-contrib-imagemin in RHEL5 and integrate with Java EE

Source: Internet
Author: User
Tags file copy ming install node

Grunt-contib-imagemin is a nodejs based plug-in for lossless image compression. The entire grunt-contib-imagemin is made up of a number of widgets, such as Java jar packages, the following four plug-ins are its core components (Linux Toolkit, written in C):
1, gifsicle-compress GIF images, corresponding to the Nodejs plug-in gifsicle
2, jpegtran-compress JPEG images, corresponding Nodejs plug-in Imagemin-jpegtran
3, optipng-compress PNG images, corresponding Nodejs plug-in imagemin-optipng
4, svgo-compress SVG images, corresponding Nodejs plug-in imagemin-svgo

For example, I understand that it uses Nodejs to invoke the Linux Toolkit optipng to compress the image, replacing the command line for the OptiPNG Toolkit imagemin-optipng.

Installation steps:
1, install node and NPM, a bunch of online, do not repeat.
2, install GRUNT-CLI, that is, grunt client, it is also a Nodejs plug-in, used to invoke Grunt-contrib-imagemin, when installed it has a default path, directly invoke the installation command. We add the grunt command to the environment variable and we can use it in any directory.
Command:
npm install-g grunt-cli
ln-s/usr/local/bin/node_modules/grunt-cli/bin/grunt/usr/local/bin/grunt
3, installation grunt-contrib-imagemin, note that advanced to a specific directory, Plug-ins will be installed in the current directory, the default source if the connection failed, you can try the source of Taobao (at the end of the command to add parameters –registry=https:// registry.npm.taobao.org).
Command: NPM install grunt-contrib-imagemin–save-dev-d
This step-by-step installation is quite lengthy and needs to be slowly downloaded, and these plug-ins do not have a class of offline packages, as if they could only be downloaded by using the commands on the Internet.
4, test, in the Grunt-contrib-imagemin installation directory, create a new gruntfile.js, new folder image and put into the compressed picture 1.png, under the installation directory, call grunt Imagemin, test Note that the test needs to be used respectively jpg, PNG, GIF, each type of code is not the same, as for the so-called svgo, did not use so ignore it. The Gruntfile.js code is as follows:

Module.exports = function (grunt) {
    ' use strict ';
    Grunt.initconfig {
        imagemin: {
            dist: {
                options: {
                    optimizationlevel:3
                },
                files: {
                    ' Image/1.png ': ' Image/1.png '}}}
    );
    Grunt.loadnpmtasks (' grunt-contrib-imagemin ');
    Grunt.registertask (' img ', [' imagemin ']);

On the RHEL7, a success, the test page passed smoothly, happy. But RHEL7 version is actually very new, the company's servers are often older and can not connect to the extranet, in the RHEL7, migrated to the company's server RHEL4.8, a variety of errors (remember to compress the PNG newspaper is the wrong glibc version of the problem). Then you want to build the same version of the virtual machine, reinstall and then do the migration, 4.8 of the version is really hard to find, no way, found a 5.x, fortunately their GCC version, glibc version of almost, finally reproduce the problem.
The two main problems encountered and the solutions are as follows:
First, install Optipng-bin this plug-in, see the error is as follows:

optipng-bin@3.0.2 postinstall/tpsys/npm/taobaotest/node_modules/optipng-bin > Node lib/install.js? Request to Https://raw.github.com/imagemin/optipng-bin/3.0.2/vendor/linux/x64/optipng failed? OptiPNG pre-build test failed? Compiling from source? Error:./configure--with-system-zlib--prefix= "/tpsys/npm/taobaotest/node_modules/optipng-bin/vendor"--bindir= "
Tpsys/npm/taobaotest/node_modules/optipng-bin/vendor "&& make install Command failed:/bin/sh-c make install PNGRTRAN.C:98: Warning: the ' png_rtran_ok ' definition did not use pngxmem.c:in function ' pngx_malloc_rows_extended ': pngxmem.c:37:
Warning: The comparison result is always false due to a range of data type ratio.c:25: Error: conflict with ' opng_longest_impl_t ' type ratio.c:24: Error: ' opng_longest_impl_t ' last declaration in this RATIO.C:76: Error: expected declaration specifiers or ' ... ' before ' opng_ulongest_impl_t ' ratio.c:76: Error: expected Declaration specifiers or ' ... ' before ' opng_ulongest_impl_t ' ratio.c:in function ' Opng_sprint_uratio_impl ': ratio.c:90 : Error: ' opng_ulongest_impl_t ' not declared (first use in this function) Ratio.c:90: Error: (Even if multiple occurrences occur within a function, each undeclared identifier is reported only once in its ratio.c:90: Error: The function in which it resides.) RATIO.C:90: Error: expected '; ' before ' Integer_part ' ratio.c:95: Error: ' Denom ' not declared (first use in this function) ratio.c:97: error: ' num ' not declared (this function  ratio.c:117: Error: ' Integer_part ' not declared (first use within this function) ratio.c:118: Error: ' remainder ' not declared (first use in this function) Ratio.c:in function ' Opng_ulratio_to_factor_string ': ratio.c:158: Error: ' opng_ulongest_impl_t ' not declared (used for the first time in this function) ratio.c:158: Error: expected '; Before ' num ' ratio.c:159: Error: expected ' before ' denom ' ratio.c:160: Error: ' num ' not declared (used for the first time in this function) ratio.c:160: Error: ' Denom ' not sound Ming (first use in this function) ratio.c:160: Error: Too many arguments supplied to function ' Opng_sprint_uratio_impl ' ratio.c:156: Warning: Unused parameter ' ratio ' ratio.c:in function ' Opng_ulratio_to_percent_string ': ratio.c:170: Error: ' opng_ulongest_impl_t ' not declared (used for the first time in this function) ratio.c:170: Error: expected '; Before ' num ' ratio.c:171: Error: expected ' before ' denom ' ratio.c:172: Error: ' num ' not declared (used for the first time in this function) ratio.c:172: Error: ' Denom ' not sound Ming (first use in this function) ratio.c:172: Error: Too many arguments supplied to function ' Opng_sprint_uratio_impl ' ratio.c:168: Warning: Unused parameters ' RatiO ' make[1]: * * * [RATIO.O] Error 1 Make: * * * * [Install] Error 2 

WORKAROUND: Each plugin has been questioned in the issue of the GitHub, HTTPS://GITHUB.COM/IMAGEMIN/OPTIPNG-BIN/ISSUES/43, they suggest a lower version. Note here that the drop version, not down grunt-contrib-imagemin or Optipng-bin version (for this puzzled for a long time), meaning to reduce the version of the OptiPNG component (this is written in C, Plug-ins are only called it), the error is not a plug-in error, Instead, the invoked OptiPNG component compiles without a pass.
After the OptiPNG component is compiled, only one file named OptiPNG, which is placed in the Optipng-bin/vendor directory in the plugin (can be created manually without the vendor), we can download the download OptiPNG component completely, It would be nice to have the copy in after compiling. Found in the http://sourceforge.net of the history of each version, using 7.5 of the error and the same as above, the use of 7.0 on the compilation passed, compiled after the resulting optipng file copy to the Optipng-bin/vendor, problem solving.
Historical version download address: http://sourceforge.net/projects/optipng/files/OptiPNG/
Compilation and installation are simple, straightforward./configure, make and make install just fine.

Second, jpegtran-bin error, and above OptiPNG encounter very much like, the truth should be the same; this is not said to be a bug, but the compilation environment caused the compilation error, but this time I github issue found someone uploaded Jpegtran file, It is also the vendor directory, the direct attempt to replace the solution.
Issue Address: HTTPS://GITHUB.COM/IMAGEMIN/JPEGTRAN-BIN/PULL/63
Download file Address: Https://github.com/nervo/jpegtran-bin/commits/85514e054daeb770a9534b2568ca9b04c2a4c14a/vendor/linux/x64/jpegtran
I've been testing it all over again until the test is no problem.

> jpegtran-bin@3.0.4 postinstall/tpsys/news/2jepgtran-bin/node_modules/jpegtran-bin > Node lib/install.js? The '/tpsys/news/2jepgtran-bin/node_modules/jpegtran-bin/vendor/jpegtran ' binary doesn ' t seem to work correctly? Jpegtran pre-build test failed? Compiling from source? Error:./configure--disable-shared--prefix= "/tpsys/news/2jepgtran-bin/node_modules/jpegtran-bin/vendor"--bindir = "/tpsys/news/2jepgtran-bin/node_modules/jpegtran-bin/vendor" && make install Command failed:/bin/sh-c./ Configure--disable-shared--prefix= "/tpsys/news/2jepgtran-bin/node_modules/jpegtran-bin/vendor"--bindir= "/tpsys" /news/2jepgtran-bin/node_modules/jpegtran-bin/vendor "Configure:error:no nasm (netwide assembler) found at Childpro Cess.exithandler (Child_process.js:751:12) at Childprocess.emit (events.js:110:17) at Maybeclose (child_process.js : 1015:16) at Process.childprocess._handle.onexit (child_process.js:1087:5) 

Finally, with the integration of Java EE
Customer requirements in the system can manually click to compress a picture, the implementation is actually very no technical content, with the JDK from the call command line of the tool class, directly invoke the command line, do not know whether there is a better way. Each time a picture is compressed, a temporary gruntfile.js file is produced, and the command is completed before it is deleted. There are a few things to watch out for:
1, specify Gruntfile file command, grunt imagemin-g gruntfile.js-s
2, specify file path can only use relative path, absolute path is not good to make

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.