Use PHP to generate image thumbnails,

Source: Internet
Author: User
Tags bmp image imagejpeg unpack

Use PHP to generate image thumbnails,

Function: jpg, jpeg, gif, png, and bmp image formats are supported. You can scale the image according to the original image proportion. You can select whether to crop the image during image scaling, added image quality control to achieve the highest quality of the thumbnail. The complete class code is as follows:
 

<? Php/*** function: php class for generating thumbnail slices */class ResizeImage {public $ type; // image type public $ width; // actual width public $ height; // actual height public $ resize_width; // The width public $ resize_height after the change; // The height public $ cut after the change; // whether to cut the image public $ srcimg; // source image public $ dstimg; // target image address public $ im; // temporary image public $ quality; // Image quality function resizeimage ($ img, $ wid, $ hei, $ c, $ dstpath, $ quality = 100) {$ this-> srcimg = $ img; $ this-> resize_width = $ wid; $ this-> resize _ Height = $ hei; $ this-> cut = $ c; $ this-> quality = $ quality; $ this-> type = strtolower (substr (strrchr ($ this-> srcimg ,'. '), 1); // The image type $ this-> initi_img (); // initialize the image $ this-> dst_img ($ dstpath ); // target image address @ $ this-> width = imagesx ($ this-> im); @ $ this-> height = imagesy ($ this-> im ); $ this-> newimg (); // generates an image @ ImageDestroy ($ this-> im);} function newimg () {$ resize_ratio = ($ this-> resize_width) /($ this-> resize_height); // ratio of the changed image @ $ rat Io = ($ this-> width)/($ this-> height); // ratio of the actual image if ($ this-> cut) = '1 ') {// cut image if ($ img_func = 'imagepng '& (str_replace ('. ', '', PHP_VERSION)> = 512) {// processing status after the php version is greater than 5.12 parameter change $ quality = 9 ;} if ($ ratio >=$ resize_ratio) {// high priority $ newimg = imagecreatetruecolor ($ this-> resize_width, $ this-> resize_height); imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, $ this-> resize_width, $ this-> resize_height, ($ this-> height) * $ resize_ratio ), $ This-> height); imagejpeg ($ newimg, $ this-> dstimg, $ this-> quality);} if ($ ratio <$ resize_ratio) {// width first $ newimg = imagecreatetruecolor ($ this-> resize_width, $ this-> resize_height); imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, $ this-> resize_width, $ this-> resize_height, $ this-> width, ($ this-> width)/$ resize_ratio); imagejpeg ($ newimg, $ this-> dstimg, $ this-> quality) ;}} else {// No picture if ($ ratio >=$ resize_ratio) {$ newi Mg = imagecreatetruecolor ($ this-> resize_width, ($ this-> resize_width)/$ ratio); imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, $ this-> resize_width, ($ this-> resize_width)/$ ratio, $ this-> width, $ this-> height); imagejpeg ($ newimg, $ this-> dstimg, $ this-> quality);} if ($ ratio <$ resize_ratio) {@$ newimg = imagecreatetruecolor ($ this-> resize_height) * $ ratio, $ this-> resize_height); @ imagecopyresampled ($ newimg, $ this-> im, 0, 0, 0, 0, ($ this-> resize_height) * $ ratio, $ this-> resize_height, $ this-> width, $ this-> height); @ imagejpeg ($ newimg, $ this-> dstimg, $ this-> quality) ;}} function initi_img () {// initialize the image if ($ this-> type = 'jpg '| $ this-> type = 'jpeg ') {$ this-> im = imagecreatefromjpeg ($ this-> srcimg);} if ($ this-> type = 'gif ') {$ this-> im = imagecreatefromgif ($ this-> srcimg);} if ($ this-> type = 'png ') {$ this-> im = imagecreatefrompng ($ this-> srcim G);} if ($ this-> type = 'wbm ') {@ $ this-> im = imagecreatefromwbmp ($ this-> srcimg );} if ($ this-> type = 'bmp ') {$ this-> im = $ this-> ImageCreateFromBMP ($ this-> srcimg );}} function dst_img ($ dstpath) {// image destination address $ full_length = strlen ($ this-> srcimg); $ type_length = strlen ($ this-> type ); $ name_length = $ full_length-$ type_length; $ name = substr ($ this-> srcimg, 0, $ name_length-1); $ this-> dstimg = $ dstpath; // echo $ this-> dstimg;} f Unction ImageCreateFromBMP ($ filename) {// custom function to process bmp image if (! $ F1 = fopen ($ filename, "rb") returnFALSE; $ FILE = unpack ("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread ($ f1, 14 )); if ($ FILE ['file _ type']! = 19778) returnFALSE; $ BMP = unpack ('vheader _ size/Vwidth/Vheight/vplanes/vbits_per_pixel '. '/Vcompression/Vsize_bitmap/Vhoriz_resolution '. '/Vvert_resolution/Vcolors_used/Vcolors_important', fread ($ f1, 40); $ BMP ['color'] = pow (2, $ BMP ['bits _ per_pixel ']); if ($ BMP ['size _ bitmap'] = 0) $ BMP ['size _ bitmap'] = $ FILE ['file _ size']-$ file ['bitmap _ offset']; $ BMP ['bytes _ per_pixel '] = $ BMP ['bits _ per_pixel']/8; $ BMP ['bytes _ pe R_pixel2 '] = ceil ($ BMP ['bytes _ per_pixel']); $ BMP ['desc'] = ($ BMP ['width'] * $ BMP ['bytes _ per_pixel ']/4 ); $ BMP ['desc']-= floor ($ BMP ['width'] * $ BMP ['bytes _ per_pixel ']/4 ); $ BMP ['Demo'] = 4-(4 * $ BMP ['Demo']); if ($ BMP ['Demo'] = 4) $ BMP ['desc'] = 0; $ PALETTE = array (); if ($ BMP ['color'] <16777216) {$ PALETTE = unpack ('V '. $ BMP ['colors '], fread ($ f1, $ BMP ['colors'] * 4);} $ IMG = fread ($ f1, $ BMP ['size _ bitmap']); $ VIDE = chr (0 ); $ Res = imagecreatetruecolor ($ BMP ['width'], $ BMP ['height']); $ P = 0; $ Y = $ BMP ['height']-1; while ($ Y> = 0) {$ X = 0; while ($ X <$ BMP ['width']) {if ($ BMP ['bits _ per_pixel '] = 24) $ COLOR = unpack ("V", substr ($ IMG, $ P, 3 ). $ VIDE); elseif ($ BMP ['bits _ per_pixel '] = 16) {$ COLOR = unpack ("n", substr ($ IMG, $ P, 2); $ COLOR [1] = $ PALETTE [$ COLOR [1] + 1];} elseif ($ BMP ['bits _ per_pixel '] = 8) {$ COLOR = unpack ("n", $ VIDE. substr ($ IMG, $ P, 1); $ COL OR [1] = $ PALETTE [$ COLOR [1] + 1];} elseif ($ BMP ['bits _ per_pixel '] = 4) {$ COLOR = unpack ("n", $ VIDE. substr ($ IMG, floor ($ P), 1); if ($ P * 2) % 2 = 0) $ COLOR [1] = ($ COLOR [1]> 4); else $ COLOR [1] = ($ COLOR [1] & 0x0F ); $ COLOR [1] = $ PALETTE [$ COLOR [1] + 1];} elseif ($ BMP ['bits _ per_pixel '] = 1) {$ COLOR = unpack ("n", $ VIDE. substr ($ IMG, floor ($ P), 1); if ($ P * 8) % 8 = 0) $ COLOR [1] = $ COLOR [1]> 7; elseif ($ P * 8) % 8 = 1) $ COLOR [1] = ($ COLOR [1] & 0x40)> 6; Elseif ($ P * 8) % 8 = 2) $ COLOR [1] = ($ COLOR [1] & 0x20)> 5; elseif ($ P * 8) % 8 = 3) $ COLOR [1] = ($ COLOR [1] & 0x10)> 4; elseif ($ P * 8) % 8 = 4) $ COLOR [1] = ($ COLOR [1] & 0x8)> 3; elseif ($ P * 8) % 8 = 5) $ COLOR [1] = ($ COLOR [1] & 0x4)> 2; elseif ($ P * 8) % 8 = 6) $ COLOR [1] = ($ COLOR [1] & 0x2)> 1; elseif ($ P * 8) % 8 = 7) $ COLOR [1] = ($ COLOR [1] & 0x1 ); $ COLOR [1] = $ PALETTE [$ COLOR [1] + 1];} else returnFALSE; imagesetpixel ($ res, $ X, $ Y, $ COLOR [1]); $ X ++; $ P + = $ BMP [' Bytes_per_pixel '];} $ Y --; $ P + = $ BMP ['dece'];} fclose ($ f1); return $ res ;}}?>

The usage code is as follows:

 

$resizeimage=new ResizeImage('upload/abc.bmp', '120', '90', '0', 'upload/xabc.bmp');


Another powerful PHP image thumbnail class: phpThumb

In addition to scaling images, this class can also convert images into different formats for output (for example, exporting GIF images to PNG formats). Its special features include colors and special effects.

Official Website: http://phpthumb.sourceforge.net/

We'll be using phpThumb, the open source PHP script to generate thumbnails on the fly.

Download and extract phpThumb to somewhere in your website folder. Now to use a rounded corner thumbnail of an image, simply use the img tag of HTML with src

   Adjust the path to phpThumb according to where you place the phpThumb files. 

 

Generally, you only need the following files.

In fact, phpThumb is more powerful than I previously imagined. Below I will list some other useful parameters:

Src: Address of the Target Image

W: width of the output image

H: height of the output image (if not specified, the image will be scaled by the equal ratio of w parameter)

Q: If the output is in JPG format, you can specify the output quality.

Bg: Background of the output (if needed)

Sw, sh, sx, sy: local output, width and height, starting position

F: output format, which can be jpeg, png, gif, or ico.

Sfn: outputs a frame in the GIF animation.

Fltr []: filters can have many effects, including sharpening, blur, rotation, watermark, border, masking, and color adjustment.

Demo:

 

// Load the library file require_once 'path/to/ThumbLib. inc. php '; // instantiate the class library. The address of the image you want to process can be a network address or a local address $ thumb = PhpThumbFactory: create ('HTTP: // www.shlongyingjixie.com/'); // scale down the image to a maximum width of PX or a maximum of PX. When only one parameter is input, the maximum size is limited. $ Thumb-> resize (100,100); // scale down the image proportional ratio to the original percentage. For example, 50 is the original 50%. $ Thumb-> resizePercent (50); // capture a 175px * 175px image. Note that this is a screenshot. the excess part is cut directly, not forcibly changed. $ Thumb-> adaptiveResize (175,175); // extract a PX * PX image from the image center. $ Thumb-> cropFromCenter (200,100); //, the first two parameters are the coordinates X and Y in the upper-right corner of the image to be parsed. The following two parameters are the width and height of the image to be solved. $ Thumb-> crop (100,100,300,200); // rotate the image 180 degrees clockwise. $ thumb-> rotateImageNDegrees (180); // save (generate) the image. You can save it in other formats, for more information, see $ thumb-> save ('newpath/Of/image.jpg ');

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.