How to implement PHP thumbnail image

Source: Internet
Author: User
Tags fread imagejpeg unpack
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 of the class*/classresizeimage{ Public$type;//type of picture Public$width;//Actual width Public$height;//actual height Public$resize _width;//the width after the change Public$resize _height;//the height after the change Public$cut;//whether or not to cut the map Public$srcimg;//Source Image Public$dstimg;//Destination Image Address Public$im;//temporarily created images Public$quality;//Picture qualityfunctionResizeimage ($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);//type of picture$this->initi_img ();//Initializing an image$this-Dst_img ($dstpath);//Destination Image Address@$this->width=imagesx ($this-im); @$this->height=imagesy ($this-im); $this->newimg ();//Creating Images@ImageDestroy ($this-im); }  functionnewimg () {$resize _ratio=($this->resize_width)/($this->resize_height);//The proportions of the altered image@$ratio=($this->width)/($this->height);//ratio of actual imagesif(($this->cut) = = ' 1 ') {//Crop Chartif($img _func= = = ' Imagepng ' && (Str_replace('.','',php_version) (>=512)) {//for processing after the PHP version is greater than 5.12 parameter changes$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{//do not cut the mapif($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); }   }  }  functionIniti_img () {//Initializing an imageif($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-srcimg); }   if($this->type== ' WBM '){    @$this->im=imagecreatefromwbmp ($this-srcimg); }   if($this->type== ' BMP '){    $this->im=$this->imagecreatefrombmp ($this-srcimg); }  }  functionDst_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;  }      functionImagecreatefrombmp ($filename){//Custom Function processing BMP picturesif(!$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]; }     ElseReturnfalse; 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 ', ' n ', ' n ', ' 0 ', ' upload/xabc.bmp ');

Related reading:

PHP generated image thumbnail class code share

How to generate thumbnail images in bulk in PHP

PHP Picture thumbnail class Phpthumb

How to implement PHP thumbnail image

PHP upload images generated thumbnails (GD library)

The above describes the PHP image thumbnail implementation method, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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