PHP implementation of image thumbnails method, _php tutorial

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

PHP implementation of image thumbnails method,


This section of the code implementation functionality has these:

Support Jpg,jpeg,gif,png,bmp picture format, support the scale of the original picture to zoom, you can choose whether the picture in the process of scaling the picture needs to be cut, add 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 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;//After the change of the height of public $cut;//whether the drawing public $srcimg;//source image public $dstimg;// The target image 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);//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 the image @ $ratio = ($this- >width)/($this->height);//ratio of actual image if (($this->cut) = = ' 1 ') {//Graph if ($img _func=== ' imagepng ' && (str_ Replace ('. ', ', php_version) >=512) {//for PHP version greater than 5.12The processing condition after the 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,0, $this->resize_width, $this->resize_height, (($this Height) * $resize _ratio), $this->height) imagejpeg ($newimg, $this->dstimg, $this->quality);} if ($ratio < $resize _ratio) {//Width priority $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 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=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")) 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==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 ', ' I ', ' n ', ' 0 ', ' upload/xabc.bmp ');

The above code is a small series to share the PHP implementation of image thumbnails of the method, we hope to help!

Articles you may be interested in:

    • PHP GD2 upload image/Text watermark/image watermark/equal scale thumbnail/implementation code
    • Super easy to use a PHP upload image class (random name, thumbnail, add watermark)
    • PHP uses GD library to generate high-quality thumbnail images
    • Phpthumb PHP Image thumbnail gallery
    • Clipping and shrinking of PHP images into a thumbnail that fits your needs
    • Using the GD Library to implement PHP service-side picture cropping and generate thumbnails share
    • PHP implementation of upload images to generate thumbnails example
    • PHP pictures and other proportional zoom release into the thumbnail function to share
    • Jquery+php+ajax display upload progress and generate thumbnail code for multiple images
    • PHP implements the method of uploading image thumbnails by a specified size, etc.
    • How PHP generates image thumbnails

http://www.bkjia.com/PHPjc/1117085.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117085.html techarticle PHP Implementation of image thumbnails method, this section of the code to implement the function has these: support Jpg,jpeg,gif,png,bmp image format, support the scale of the original picture, you can choose to zoom in the picture ...

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