Implementation of php image thumbnails,

Source: Internet
Author: User
Tags bmp image imagejpeg unpack

Implementation of php 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_r ), $ 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');

 

Related reading:

Code sharing for generating image thumbnails using php

Php batch generation of image thumbnails

Php image thumbnails phpThumb

Implementation of php image thumbnails

Php uploads images to generate thumbnails (GD library)

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.