java JDK安装这里就不在说了。
ImageMagick 是一个功能很强的图片处理程序。可应用在多种操作系统平台上。
使用过程如下:
1.下载了ImageMagick-6.4.0-0.tar.gz和jmagick-6.4.0.tar.gz压缩包。
注意下载包必须包含图1和图2的内容。有下载的文件不全只包含了两个so文件和两个jar文件。
在某些情况下可以使用。但是我的linux 是64位的。网上没找到编译好的64位so文件和jar文件。
2.安装环境要求:
如果安装了老版本ImageMagick的包,一定要先删除 yum remove ImageMagick
确认已经安装了zlib、freetype、libpng和jpeg-6b等lib的开发包,可以用rpm -aq查询,如果没有的请先安装。
观察配置后给出来的支持图形格式,看看该图形格式支持是否是yes,如果不是,那么必须先安装该图形格式的本地库。
例如,应该检查的rpm包:libpng libpng-devel libjpeg gd-devel libtiff。
我是通过网络来下载所需要的包,其他包根据实际情况安装。
例如:(安装缺少的libX11包)
[root@localhost ImageMagick-6.4.0]# yum list | grep libX11
libX11.i386 1.0.3-9.el5 installed
libX11.x86_64 1.0.3-9.el5 installed
libX11.i386 1.0.3-11.el5 base
libX11.x86_64 1.0.3-11.el5 base
libX11-devel.i386 1.0.3-11.el5 base
libX11-devel.x86_64 1.0.3-11.el5 base
[root@localhost ImageMagick-6.4.0]# yum install libX11-devel
3.安装ImageMagick包
tar xzvf ImageMagick-6.5.3-2.tar.gz
./configure --prefix=/opt/ImageMagick --enable-share --enable-static
make
make install
编译时如果发生如下异常请安装X11包。安装方法如上。
./magick/xwindow-private.h:27:21: error: X11/Xos.h: No such file or directory
安装完后使用convert logo: logo.gif 命令看看生成的文件是否正确。
特别说明,要分别测试你所需要的不同的图片类型,如png,gif,jpg等等。
如果出现no decode delegate for this image format `PG_script/533589.tif’
这样的错误请,
使用identify -list format 命令查看列表中是否有支持的图片类型?
[root@localhost logs]# identify -list format
Format Module Mode Description
-------------------------------------------------------------------------------
A* RAW rw+ Raw alpha samples
AI PDF rw- Adobe Illustrator CS2
ART* ART rw- PFS: 1st Publisher Clip Art
ARW DNG r-- Sony Alpha Raw Image Format
AVI* AVI r-- Microsoft Audio/Visual Interleaved
AVS* AVS rw+ AVS X image
B* RAW rw+ Raw blue samples
BMP* BMP rw- Microsoft Windows bitmap image
BMP2* BMP -w- Microsoft Windows bitmap image v2
BMP3* BMP -w- Microsoft Windows bitmap image v3
BRF* BRAILLE -w- BRF ASCII Braille format
C* RAW rw+ Raw cyan samples
CAPTION* CAPTION r-- Image caption
CIN* CIN rw+ Cineon Image File
CIP* CIP -w- Cisco IP phone image format
CLIP* CLIP -w+ Image Clip Mask
CMYK* CMYK rw+ Raw cyan, magenta, yellow, and black samples
CMYKA* CMYK rw+ Raw cyan, magenta, yellow, black, and opacity samples
CR2 DNG r-- Canon Digital Camera Raw Image Format
CRW DNG r-- Canon Digital Camera Raw Image Format
CUR* CUR rw- Microsoft icon
CUT* CUT r-- DR Halo
DCM* DCM r-- Digital Imaging and Communications in Medicine image
DICOM is used by the medical community for images like X-rays. The
specification, "Digital Imaging and Communications in Medicine
(DICOM)", is available at http://medical.nema.org/. In particular,
see part 5 which describes the image encoding (RLE, JPEG, JPEG-LS),
and supplement 61 which adds JPEG-2000 encoding.
DCR DNG r-- Kodak Digital Camera Raw Image File
DCX* PCX rw+ ZSoft IBM PC multi-page Paintbrush
DDS* DDS r-- Microsoft DirectDraw Surface
DFONT* TTF r-- Multi-face font package (Freetype 2.2.1)
DNG DNG r-- Digital Negative
DOT DOT --- Graphviz
DPS DPS --- Display Postscript Interpreter
DPX* DPX rw+ SMPTE 268M-2003 (DPX 2.0)
Digital Moving Picture Exchange Bitmap, Version 2.0.
See SMPTE 268M-2003 specification at http://www.smtpe.org
EPDF PDF rw- Encapsulated Portable Document Format
EPI PS rw- Encapsulated PostScript Interchange format
EPS PS rw- Encapsulated PostScript
EPS2* PS2 -w- Level II Encapsulated PostScript
EPS3* PS3 -w+ Level III Encapsulated PostScript
EPSF PS rw- Encapsulated PostScript
EPSI PS rw- Encapsulated PostScript Interchange format
FAX* FAX rw+ Group 3 FAX
FAX machines use non-square pixels which are 1.5 times wider than they
are tall but computer displays use square pixels, therefore FAX images
may appear to be narrow unless they are explicitly resized using a
geometry of "150x100%".
FITS* FITS rw- Flexible Image Transport System
FRACTAL* PLASMA r-- Plasma fractal image
FTS* FTS rw- Flexible Image Transport System
G* RAW rw+ Raw green samples
G3* FAX rw- Group 3 FAX
GIF* GIF rw+ CompuServe graphics interchange format
GIF87* GIF rw- CompuServe graphics interchange format (version 87a)
GRADIENT* GRADIENT r-- Gradual passing from one shade to another
GRAY* GRAY rw+ Raw gray samples
HISTOGRAM* HISTOGRAM -w- Histogram of the image
HTM* HTML -w- Hypertext Markup Language and a client-side image map
HTML* HTML -w- Hypertext Markup Language and a client-side image map
ICB* TGA rw+ Truevision Targa image
ICO* ICON rw+ Microsoft icon
ICON* ICON rw- Microsoft icon
INFO INFO -w+ The image format and characteristics
IPL* IPL rw+ IPL Image Sequence
ISOBRL* BRAILLE -w- ISO/TR 11548-1 format
JNG* PNG rw- JPEG Network Graphics
See http://www.libpng.org/pub/mng/ for details about the JNG
format.
JPEG* JPEG rw- Joint Photographic Experts Group JFIF format (62)
JPG* JPEG rw- Joint Photographic Experts Group JFIF format
K* RAW rw+ Raw black samples
K25 DNG r-- Kodak Digital Camera Raw Image Format
KDC DNG r-- Kodak Digital Camera Raw Image Format
LABEL* LABEL r-- Image label
M* RAW rw+ Raw magenta samples
M2V MPEG rw+ MPEG Video Stream
MAP* MAP rw- Colormap intensities and indices
MAT* MAT rw+ MATLAB image format
MATTE* MATTE -w+ MATTE format
MIFF* MIFF rw+ Magick Image File Format
MNG* PNG rw+ Multiple-image Network Graphics (libpng 1.2.10)
See http://www.libpng.org/pub/mng/ for details about the MNG
format.
MONO* MONO rw- Raw bi-level bitmap
MPC* MPC rw+ Magick Persistent Cache image format
MPEG MPEG rw+ MPEG Video Stream
MPG MPEG rw+ MPEG Video Stream
MRW DNG r-- Sony (Minolta) Raw Image File
MSL* MSL rw+ Magick Scripting Language
MSVG* SVG rw+ ImageMagick's own SVG internal renderer
MTV* MTV rw+ MTV Raytracing image format
MVG* MVG rw- Magick Vector Graphics
NEF DNG r-- Nikon Digital SLR Camera Raw Image File
NULL* NULL rw- Constant image of uniform color
O* RAW rw+ Raw opacity samples
ORF DNG r-- Olympus Digital Camera Raw Image File
OTB* OTB rw- On-the-air bitmap
OTF* TTF r-- Open Type font (Freetype 2.2.1)
PAL* UYVY rw- 16bit/pixel interleaved YUV
PALM* PALM rw+ Palm pixmap
PAM* PNM rw+ Common 2-dimensional bitmap format
PATTERN* PATTERN r-- Predefined pattern
PBM* PNM rw+ Portable bitmap format (black and white)
PCD* PCD rw- Photo CD
PCDS* PCD rw- Photo CD
PCL PCL rw- Printer Control Language
PCT* PICT rw- Apple Macintosh QuickDraw/PICT
PCX* PCX rw- ZSoft IBM PC Paintbrush
PDB* PDB rw+ Palm Database ImageViewer Format
PDF PDF rw+ Portable Document Format
PDFA PDF rw+ Portable Document Archive Format
PEF DNG r-- Pentax Electronic File
PFA* TTF r-- Postscript Type 1 font (ASCII) (Freetype 2.2.1)
PFB* TTF r-- Postscript Type 1 font (binary) (Freetype 2.2.1)
PFM* PFM rw+ Portable float format
PGM* PNM rw+ Portable graymap format (gray scale)
PICON* XPM rw- Personal Icon
PICT* PICT rw- Apple Macintosh QuickDraw/PICT
PIX* PIX r-- Alias/Wavefront RLE image format
PJPEG* JPEG rw- Progessive Joint Photographic Experts Group JFIF
PLASMA* PLASMA r-- Plasma fractal image
PNG* PNG rw- Portable Network Graphics (libpng 1.2.10)
See http://www.libpng.org/ for details about the PNG format.
PNG24* PNG rw- opaque 24-bit RGB (zlib 1.2.3)
PNG32* PNG rw- opaque or transparent 32-bit RGBA
PNG8* PNG rw- 8-bit indexed with optional binary transparency
PNM* PNM rw+ Portable anymap
PPM* PNM rw+ Portable pixmap format (color)
PREVIEW* PREVIEW -w- Show a preview an image enhancement, effect, or f/x
PS PS rw+ PostScript
PS2* PS2 -w+ Level II PostScript
PS3* PS3 -w+ Level III PostScript
PSD* PSD rw+ Adobe Photoshop bitmap
PWP* PWP r-- Seattle Film Works
R* RAW rw+ Raw red samples
RAF DNG r-- Fuji CCD-RAW Graphic File
RAS* SUN rw+ SUN Rasterfile
RGB* RGB rw+ Raw red, green, and blue samples
RGBA* RGB rw+ Raw red, green, blue, and alpha samples
RGBO* RGB rw+ Raw red, green, blue, and opacity samples
RLA* RLA r-- Alias/Wavefront image
RLE* RLE r-- Utah Run length encoded image
SCR* SCR r-- ZX-Spectrum SCREEN$
SCT* SCT r-- Scitex HandShake
SFW* SFW r-- Seattle Film Works
SGI* SGI rw+ Irix RGB image
SHTML* HTML -w- Hypertext Markup Language and a client-side image map
SR2 DNG r-- Sony Raw Format 2
SRF DNG r-- Sony Raw Format
STEGANO* STEGANO r-- Steganographic image
SUN* SUN rw+ SUN Rasterfile
SVG* SVG rw+ Scalable Vector Graphics (XML 2.6.26)
SVGZ* SVG rw+ Compressed Scalable Vector Graphics (XML 2.6.26)
TEXT* TXT rw+ Text
TGA* TGA rw+ Truevision Targa image
THUMBNAIL* THUMBNAIL -w+ EXIF Profile Thumbnail
TILE* TILE r-- Tile image with a texture
TIM* TIM r-- PSX TIM
TTC* TTF r-- TrueType font collection (Freetype 2.2.1)
TTF* TTF r-- TrueType font (Freetype 2.2.1)
TXT* TXT rw+ Text
UBRL* BRAILLE -w- Unicode Text format
UIL* UIL -w- X-Motif UIL table
UYVY* UYVY rw- 16bit/pixel interleaved YUV
VDA* TGA rw+ Truevision Targa image
VICAR* VICAR rw- VICAR rasterfile format
VID* VID rw+ Visual Image Directory
VIFF* VIFF rw+ Khoros Visualization image
VST* TGA rw+ Truevision Targa image
WBMP* WBMP rw- Wireless Bitmap (level 0) image
WMF* WMF --- Windows Meta File
WMZ* WMZ --- Compressed Windows Meta File
WPG* WPG r-- Word Perfect Graphics
X* X rw+ X Image
X3F DNG r-- Sigma Camera RAW Picture File
XBM* XBM rw- X Windows system bitmap (black and white)
XC* XC r-- Constant image uniform color
XCF* XCF r-- GIMP image
XPM* XPM rw- X Windows system pixmap (color)
XPS XPS r-- Microsoft XML Paper Specification
XV* VIFF rw+ Khoros Visualization image
XWD* XWD rw- X Windows system window dump (color)
Y* RAW rw+ Raw yellow samples
YCbCr* YCbCr rw+ Raw Y, Cb, and Cr samples
YCbCrA* YCbCr rw+ Raw Y, Cb, Cr, and opacity samples
YUV* YUV rw- CCIR 601 4:1:1 or 4:2:2
* native blob support
例:如果列表中没有对 PJPEG* JPEG文件类型。采用预编译参数的方法来解决!由于系统自己找不到JPeg相关头文件,我指定系统去找这样就能解决如上问题。
./configure --enable-share --enable-static CPPFLAGS=-I/usr/local/include LDFLAGS='-L/usr/local/lib'
make
make install
4.安装JMagick包
tar xzvf jmagick-6.4.0.tar.gz
(# chmod -v+r / 否则这个包内的文件无权限执行)
autoconf ./configure --prefix=/opt/JMagick --with-magick-home=/opt/ImageMagick --enable-share --enable-static --with-java-home=/usr/local/jdk1.6.0_23
make all
make install
5.在安装路径下找到编译好的libJMagick-6.4.0.so、libJMagick.so和jmagick-6.4.0.jar,分别将前两个方到tomcat的lib下。再将jmagick-6.4.0.jar放到webinfo的lib下。
否则会发生如下异常
java.lang.NoClassDefFoundError: Could not initialize class magick.ImageInfo
java.lang.UnsatisfiedLinkError: no JMagick in java.library.path
如果libJMagick-6.4.0.so、libJMagick.so不是编译、安装后生成的,运行系统后可能会发生如下异常
java.lang.UnsatisfiedLinkError: /usr/local/tsp/tomcat-8081/lib/libJMagick.so: /usr/local/tsp/tomcat-8081/lib/libJMagick.so: wrong ELF class: ELFCLASS32 (Possible cause: architecture word width mismatch)
6.配置环境变量
export PATH=$PATH:/opt/ImageMagick/bin
export LD_LIBRARY_PATH=:/opt/JMagick/lib:/opt/ImageMagick/lib
export DYLD_LIBRARY_PATH=:/opt/JMagick/lib:/opt/ImageMagick/lib
export CLASSPATH=$CLASSPATH:/opt/JMagick/lib
执行命令:
ldconfig
7.使用时注意:在Web Application Server(Resin,Tomcat),需要再运行时加入参数: -Djmagick.systemclassloader=no
或者在程序中写入:System.setProperty("jmagick.systemclassloader", "no");
8.关于异常
magick.MagickException: Cannot obtain image handle
这个异常出现的情况有很多种,有可能是图片的路径不对,也有可能环境变量配置不正确,也有可能是内部程序异常。
使用过程中异常的常见的出现次序:
(1)java.lang.NoClassDefFoundError: Could not initialize class magick.ImageInfo (环境变量、lib下对应的Jar相关)
(2)java.lang.UnsatisfiedLinkError: no JMagick in java.library.path (tomcat lib下无libJMagick-6.4.0.so、libJMagick.so文件相关)
(3)magick.MagickException: Cannot obtain image handle (多种情况!)
9.测试命令行使用方法:
convert -resize 100x200 22.jpg(图片路径) 11.jpg(缩放后存放路径) 图片按比例缩放
convert -resize 100x200\! 22.jpg(图片路径) 11.jpg(缩放后存放路径) 强制按给定的宽高缩放
convert -crop 100(长)x200(高)+10(x坐标)+10(y坐标) 22.jpg(图片路径) 11.jpg(缩放后存放路径) 裁剪图片
由于上传文件不能超过10M,ImageMagick-6.4.0-0.tar.gz自己到网上下载吧。http://downloads.jmagick.org/6.4.0/