How to use PHP to generate thumbnails of pictures _php instance

Source: Internet
Author: User
Tags fread imagejpeg php script strlen unpack

  Function: Support jpg,jpeg,gif,png,bmp picture format, support according to the proportion of the original picture scaling, you can choose in the picture scaling process whether the picture should be trimmed, added to the picture quality control, you can achieve the highest quality of thumbnail images. The code for the complete class is as follows:
 

<?php/** * Function: PHP to generate thumbnail images class * * resizeimage{public $type;//Picture type public $width;//actual width public $height; Height public $resize _width;//the changed width public $resize _height;//the height public $cut after the change;/whether to map public $srcimg;//source Image public $DSTIMG//target image address public $im;//image quality Function resizeimage ($img, $wid, $hei, $c, $dstpath, $qualit
   y=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 the picture $this->initi_img (),//Initialize image $this-& Gt
   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);//The proportions of the changed image @ $ratio = ($this-& Gt;width)/($this->height)//ratio of the actual image if ($this->cut = = = ' 1 ') {//Fig if ($img _func=== ' imagepng ' && (Str_replace ('. ', ', php_version) >=512)) {//
    For PHP version greater than 5.12 parameters after the change of processing $quality = 9; } if ($ratio >= $resize _ratio) {//High priority $newimg =imagecreatetruecolor ($this->resize_width, $this->resize_heigh
     T); 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{//not >= $resize _ratio) {$newimg =imagecreatetruecolor ($this->resize_width, ($this->res
     ize_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_h
     eight); @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-&GT;IM=IMAGECR
   Eatefromjpeg ($this->srcimg);
   if ($this->type== ' gif ') {$this->im=imagecreatefromgif ($this->srcimg);
   The 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);
   The 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;
   function Imagecreatefrombmp ($filename) {//Custom functions to process BMP pictures 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-(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)%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 ', ', ', ', ', ' 0 ', ' upload/xabc.bmp ');


Another powerful php picture thumbnail class: Phpthumb

In addition to scaling pictures, the class can also convert pictures into different format output (for example, the image in GIF format is exported to PNG format), and its features include color, special 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 folder. Now to use a rounded corner thumbnail of "an", simply use the IMG tag's HTML with SRC as

  

 Adjust the Path to Phpthumb according to where the Phpthumb files.

  
 

Generally, you just need the following files.

In fact, Phpthumb's function is more powerful than I thought, and here's a list of some of his other useful parameters:

SRC: Address of the target picture

W: the width of the output picture

H: The height of the output picture (if not specified he will be scaled by the W parameter ratio)

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

BG: Background When output (if required)

SW, SH, SX, sy: Partial output, wide and high, starting position

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

SFN: Output A frame in a GIF animation

Fltr[]: Filters, can have many effects, including sharpening, Blur, spin flip, watermark, border, shading, color adjustment, etc.

Use Demo:

Load class library file

 require_once ' path/to/thumblib.inc.php ';

 Instantiate the class library, the incoming image you want to process the address can be a network address, can also be a local address

 $thumb = phpthumbfactory::create (' http://www.shlongyingjixie.com/');

 Reduce the picture to a maximum width of 100px or 100px, and when only one parameter is entered, it is the widest size.

 $thumb->resize (m);

 Reduce the picture to the original percentage, such as 50 is the original 50%.

 $thumb->resizepercent (m);

 Capture a 175px * 175px image, note that this is intercepted, the excess part of the direct cut off, not forced to change the size.
 $thumb->adaptiveresize (175, 175);

 From the center of the picture, intercept the 200px * 100px picture.

 $thumb->cropfromcenter (MB);

 Screenshot, the first two parameters are required to solve the picture's upper right corner of the coordinate x,y. The following two parameters are needed to solve the picture wide and high.

 $thumb->crop (MB, MB);
 
 Reverse 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 ');

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.