How to generate thumbnails of images using PHP, _php tutorial

Source: Internet
Author: User
Tags bmp image fread imagejpeg unpack

A way to generate thumbnails of pictures using PHP,


Function: Support jpg,jpeg,gif,png,bmp picture format, support the scale of the original picture, you can choose whether to cut the picture in the process of image zooming, add the picture quality control, can achieve the highest quality of thumbnail image. The code for the complete class is as follows:

<?php/** * Function: PHP generated thumbnail image class */class resizeimage{public $type;//Picture type public $width;//actual width public $height;//Actual height Publ IC $resize _width;//changed width public $resize after the change of the height of public $cut;//whether the drawing public $srcimg;//The source image public $dstimg;//target graph Like address public $im;//Image public $quality temporarily created;//Picture 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 type of picture $this->initi_img ();//Initialize Image $this-   > dst_img ($dstpath);//target Image address @ $this->width=imagesx ($this->im);   @ $this->height=imagesy ($this->im);  $this->newimg ();//Generate Image @ImageDestroy ($this->im); } function Newimg () {$resize _ratio= ($this->resize_width)/($this->resize_height);//Change the proportions of the image @ $ratio = ($this-& Gt;width)/($this->height);//ratio of actual image if (($this->cut) = = ' 1 ') {//Graph if ($img _func=== 'Imagepng ' && (Str_replace ('. ', ', php_version) >=512) {//For the processing of PHP versions greater than 5.12 parameters $quality = 9; if ($ratio >= $resize _ratio) {//High priority $newimg =imagecreatetruecolor ($this->resize_width, $this->resize_     height); Imagecopyresampled ($newimg, $this->im,0,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{//does not map if ($ratio >= $resize _ratio) {$newimg =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 image if ($this->type== ' jpg ' | | $this->type== ' JPEG ') {$this->im=imagecreatef   Romjpeg ($this->srcimg);   } if ($this->type== ' gif ') {$this->im=imagecreatefromgif ($this->srcimg);   } if ($this->type== ' 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 handles BMP picture if (! $f 1=fopen ($filename, "RB")) Returnfalse;   $FILE =unpack ("Vfile_type/vfile_size/vreserved/vbitmap_offset", Fread ($f 1,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 ($f 1,40));   $BMP [' Colors ']=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_per_pixel2 ']=ceil ($BMP [' bytes_per_pixel ']);   $BMP [' Decal ']= ($BMP [' width ']* $BMP [' Bytes_per_pixel ']/4]; $BMP [' Decal ']-=floor ($BMP [' Width ']* $BMP [' Bytes_per_pixel ']/4);   $BMP [' Decal ']=4-($BMP [' decal ']);   if ($BMP [' decal ']==4) $BMP [' Decal ']=0;   $PALETTE =array ();   if ($BMP [' colors ']<16777216) {$PALETTE =unpack (' V '. $BMP [' Colors '],fread ($f 1, $BMP [' Colors ']*4 ')];   } $IMG =fread ($f 1, $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));     $COLOR [1]= $PALETTE [$COLOR [1]+1];      } elseif ($BMP [' Bits_per_pixel ']==4) {$COLOR =unpack ("n", $VIDE. substr ($IMG, Floor ($P), 1));      if ($P)%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 [' decal '];   } fclose ($f 1);  Return$res; }}?>

The simple way to use the code is as follows:


$resizeimage =new resizeimage (' upload/abc.bmp ', ' I ', ' n ', ' 0 ', ' upload/xabc.bmp ');


Another powerful PHP image thumbnail class: Phpthumb

In addition to scaling the picture, the class can also convert the picture to different format output (such as the GIF format of the image output to PNG format), its features include color, effects, etc.

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. Rounded corner thumbnail of an image, simply use the IMG tag of HTML with SRC as

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

Generally, only the following files are required.

In fact, the function of phpthumb than I originally thought to be more powerful, below some of his other useful parameters are listed:

SRC: Address of the target image

W: the width of the output picture

H: The height of the output image (if not specified he will be scaled by the W parameter, etc.)

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

BG: Background When output (if required)

SW, SH, SX, SY: Local output, wide height, starting position

F: Output format, can be JPEG, PNG, GIF, ICO

SFN: Output A frame in an animated GIF

Fltr[]: Filters, can have a lot of effects, including sharpening, Blur, spin flip, watermark, border, matte, color adjustment, etc.

Use Demo:

Load class library file require_once ' path/to/thumblib.inc.php '; Instantiate the class library, the address of the incoming image you want to process can be a network address, or it can be a local address $thumb = phpthumbfactory::create (' http://www.shlongyingjixie.com/'); Reduce the picture to a maximum width of 100px or up to 100px, when only one parameter is entered, the widest size is limited. $thumb->resize (100, 100); Reduce the picture to the original percentage, such as 50 is the original 50%. $thumb->resizepercent (50); Intercept a 175px * 175px picture, note that this is the intercept, the part of the excess is cut off directly, is not forced to change the size. $thumb->adaptiveresize (175, 175); From the center of the image, capture 200px * 100px images. $thumb->cropfromcenter (200, 100); , the first two parameters are the coordinates x, y of the upper-right corner of the picture that needs to be solved. The following two parameters are required to solve the picture width, height. $thumb->crop (+,--);  Invert the picture clockwise 180 degrees $thumb->rotateimagendegrees (180); Save (Generate) pictures, you can save other formats, detailed reference documents $thumb->save (' newpath/of/image.jpg ');

http://www.bkjia.com/PHPjc/1049145.html www.bkjia.com true http://www.bkjia.com/PHPjc/1049145.html techarticle using PHP to generate thumbnail image of the method, function: Support jpg,jpeg,gif,png,bmp image format, support the scale of the original picture to zoom, you can choose whether in the process of image scaling ...

  • 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.