Php image thumbnails implementation method, _ PHP Tutorial

Source: Internet
Author: User
Tags bmp image imagejpeg
Php image thumbnails ,. Php image thumbnail implementation method, function: Supports jpg, jpeg, gif, png, bmp image format, supports scaling according to the proportion of the original image, you can choose whether to implement php image thumbnails during image scaling,

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:

 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-> re Size_width)/($ this-> resize_height); // ratio of the changed image @ $ ratio = ($ 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, $ this-> resize_width, $ this-> resize_height, $ this-> width, ($ this-> width)/$ resize_ratio); imagejpeg ($ newimg, $ this-> dstimg, $ this-> qual Ity) ;}} else {// no picture if ($ ratio >=$ resize_ratio) {$ newimg = imagecreatetruecolor ($ this-> resize_width, ($ this-> resize_width) /$ ratio); imagecopyresampled ($ newimg, $ this-> im, 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, ($ 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-> typ E = 'PNG ') {$ this-> im = imagecreatefrompng ($ this-> srcimg);} if ($ this-> type = 'wbm ') {@ $ this-> im = imagecreatefromwbmp ($ this-> srcimg);} if ($ this-> type = 'bmp ') {$ this-> im = $ this-> ImageCreateFromBMP ($ this-> srcimg) ;}} function dst_img ($ dstpath) {// Image target 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;} function 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)

Http://www.bkjia.com/PHPjc/1114475.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1114475.htmlTechArticlephp image thumbnail implementation method, function: support jpg, jpeg, gif, png, bmp image format, support according to the proportion of the original image scaling, you can select whether or not you need...

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.