Java and PHP generate thumbnails or size compression methods java
Package com. app. image; import java. io. *; import java. util. date; import java. awt. *; import java. awt. image. *; import javax. imageio. imageIO; import com.sun.image.codec.jpeg. *;/*** Image Compression processing ** @ author Cui Suqiang */public class ImageThumbOrCompress {private Image img; private int width; private int height; public static void main (String [] args) throws Exception {System. out. println ("start:" + new Date (). toLocaleString (); ImageThumbOrCompress imgCom = new ImageThumbOrCompress ("D:/Droid4x/test.jpg"); imgCom. resizeFix (400,400); System. out. println ("end:" + new Date (). toLocaleString ();}/*** constructor */public ImageThumbOrCompress (String fileName) throws IOException {File file = new File (fileName); // read File img = ImageIO. read (file); // construct the Image object width = img. getWidth (null); // Obtain the source image width height = img. getHeight (null ); // Obtain the source image length}/*** compression by width or height * @ param w int max width * @ param h int max height */public void resizeFix (int w, int h) throws IOException {if (width/height> w/h) {resizeByWidth (w);} else {resizeByHeight (h );}} /*** scale down the image based on the width * @ param w int new width */public void resizeByWidth (int w) throws IOException {int h = (int) (height * w/width); resize (w, h);}/*** based on height, proportional scaling image * @ param h int new height */public void resizeByHeight (int h) throws IOException {int w = (int) (width * h/height ); resize (w, h );} /*** forcibly compress/enlarge the image to a fixed size * @ param w int new width * @ param h int new height */public void resize (int w, int h) throws IOException {// SCALE_SMOOTH's scaling algorithm generates a thumbnail. the smoothness is higher than the speed. the generated image quality is better, but the speed is slow. BufferedImage image = new BufferedImage (w, h, BufferedImage. TYPE_INT_RGB);/* image. getGraphics (). drawImage (img. getScaledInstance (newWidth, newHeight, Image. SCALE_SMOOTH), 0, 0, null); **/image. getGraphics (). drawImage (img, 0, 0, w, h, null); // draw the reduced image File destFile = new File ("D:/Droid4x/test2.jpg "); fileOutputStream out = new FileOutputStream (destFile); // output to the File Stream // Convert bmp, png, and gif to jpg?imageencoder encoder = javascodec. createJPEGEncoder (out); encoder. encode (image); // JPEG encoded out. close ();}}
In addition, the image size is not changed and the quality is compressed in JDK 6 or above.
Package com. app. image; import java. awt. image. bufferedImage; import java. awt. image. colorModel; import java. io. file; import java. io. fileOutputStream; import javax. imageio. IIOImage; import javax. imageio. imageIO; import javax. imageio. imageWriteParam; import javax. imageio. imageWriter; public class ImageQualityCompress {public static void main (String [] args) {if (compressPic ("D:/Droid4x/test.jpg", "D:/Droid4x/tes T3.jpg ") {System. out. println (" compressed successfully! ");} Else {System. out. println (" compression failed! ") ;}} Public static boolean compressPic (String srcFilePath, String descFilePath) {File file = null; BufferedImage src = null; FileOutputStream out = null; ImageWriter imgWrier; ImageWriteParam imgWriteParams; // specify the image writing method as jpg imgWrier = ImageIO. getImageWritersByFormatName ("jpg "). next (); imgWriteParams = new javax.imageio.plugins.jpeg. required imagewriteparam (null); // to use compression, you must specify the compression method as MODE_EXPLICIT img. WriteParams. setCompressionMode (imgWriteParams. MODE_EXPLICIT); // specify the compression degree. The value of qality is 0 ~ Within the range of 1, imgWriteParams. setCompressionQuality (0.1f); imgWriteParams. setProgressiveMode (imgWriteParams. MODE_DISABLED); ColorModel colorModel = ColorModel. getRGBdefault (); // specifies the color mode imgWriteParams used for compression. setDestinationType (new javax. imageio. imageTypeSpecifier (colorModel, colorModel. createCompatibleSampleModel (16, 16); try {if (srcFilePath = null | srcFilePath. length () = 0) {return false;} else {file = new File (srcFilePath); src = ImageIO. read (file); out = new FileOutputStream (descFilePath); imgWrier. reset (); // you must specify the out value before calling the write method. ImageOutputStream can use any OutputStream to construct imgWrier. setOutput (ImageIO. createImageOutputStream (out); // call the write method to write the image imgWrier to the input stream. write (null, new IIOImage (src, null, null), imgWriteParams); out. flush (); out. close () ;}} catch (Exception e) {e. printStackTrace (); return false;} return true ;}}
Php
// By MoreWindows ( http://blog.csdn.net/MoreWindows ) Class CImage {/*** generate an image that maintains the horizontal comparison of the source image, supporting .png. jpg. gif * images are in. PNG format * $ srcFile original image file name * $ toW thumbnail width * $ toH Thumbnail height * $ toFile thumbnail file name, overwrite the original image file * @ return bool */public static function CreateThumbnail ($ srcFile, $ toW, $ toH, $ toFile = "") {if ($ toFile = "") {$ toFile = $ srcFile;} $ info = ""; // returns an array containing four units, 0-width, 1-height, 2-image type, 3-width text description. // If the request fails, false is returned and a warning is generated. $ Data = getimagesize ($ srcFile, $ info); if (! $ Data) return false; // load the file to the resource variable im switch ($ data [2]) // 1-GIF, 2-JPG, 3-PNG {case 1: if (! Function_exists ("imagecreatefromgif") {echo "the GD can't support. gif, please use. jpeg or. png! Back "; exit () ;}$ im = imagecreatefromgif ($ srcFile); break; case 2: if (! Function_exists ("imagecreatefromjpeg") {echo "the GD can't support. jpeg, please use other picture! Back "; exit () ;}$ im = imagecreatefromjpeg ($ srcFile); break; case 3: $ im = imagecreatefrompng ($ srcFile); break ;} // calculate the width and height of the thumbnail $ srcW = imagesx ($ im); $ srcH = imagesy ($ im); $ toWH = $ toW/$ toH; $ srcWH = $ srcW/$ srcH; if ($ toWH <= $ srcWH) {$ ftoW = $ toW; $ ftoH = (int) ($ ftoW * ($ srcH/$ srcW);} else {$ ftoH = $ toH; $ ftoW = (int) ($ ftoH * ($ srcW/$ srcH);} if (function_exists ("imagecreatetruecolor") {$ ni = imagecreatetruecolor ($ ftoW, $ ftoH ); // create a true color image if ($ ni) {// re-sample and copy part of the image and adjust the size to maintain a good definition of imagecopyresampled ($ ni, $ im, 0, 0, 0, 0, 0, $ ftoW, $ ftoH, $ srcW, $ srcH);} else {// Copy part of the image and resize it $ ni = imagecreate ($ ftoW, $ ftoH ); imagecopyresized ($ ni, $ im, 0, 0, 0, 0, $ ftoW, $ ftoH, $ srcW, $ srcH );}} else {$ ni = imagecreate ($ ftoW, $ ftoH); imagecopyresized ($ ni, $ im, 0, 0, 0, 0, $ ftoW, $ ftoH, $ srcW, $ srcH);} // save it to the file in the. PNG format imagepng ($ ni, $ toFile); // output the image to the browser or file ImageDestroy ($ ni) in PNG format ); imageDestroy ($ im); return true ;}}
From: http://my.oschina.net/ososchina/blog/482951