PHP generated image thumbnail class

Source: Internet
Author: User
Tags fread imagejpeg unpack
PHP generated image thumbnail class (supports common image formats)

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:

/** * Function: PHP generated thumbnail image class */class resizeimage{public $type;//Picture type public $width;//actual width public $height  ;//actual height public $resize _width;//changed width public $resize _height;//changed height public $cut;//whether to crop public $srcimg;//The source image public $dstimg;//The Target image address public $im;//temporary image public $quality;//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 proportion of images @ $ratio = ($this->width)/($ This->height)///The ratio of the actual image if (($this->cut) = = ' 1 ') {//Graph if ($img _func=== ' imagepng ' && (str                _replace ('. ', ' ', php_version) >=512) {//For processing after PHP version is greater than 5.12 parameter $quality = 9; } if ($ratio >= $resize _ratio) {//High priority $newimg =imagecreatetruecolor ($this->resize_widt                    H, $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 ($thi                    S->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=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);            }} 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 function handles BMP picture if (! $f 1=fopen ($filename, "RB")) Returnfa            LSE;            $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) $COLO                    R=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 ');
  • Related Article

    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.