PHP class method for generating image thumbnails

Source: Internet
Author: User

PHP generates thumbnail images of 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;//The source image public $dstimg;//target image ground public $im;//Image public $quality temporarily created;//Picture quality public $img _array=array (' jpg ', ' png ', ' gif ');//http://www.phpernote.com/        php-function/782.html function __construct ($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 the picture $this->type= $this->checkfiletype ($this->srcimg);//More stringent detection of the picture type if (!in_array ($this->type,$        This->img_array) {return ';}        $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->width)/($this->height);//The ratio of the actual image if (($this->cut) = = ' 1 ') {//(function_exists (' Imagepng ') &            & (Str_replace ('. ', ' ', php_version) >=512)} {//For processing after PHP version greater than 5.12 parameter changed $quality=9;} if ($ratio >= $resize _ratio) {//High priority $newimg =imagecreatetruecolor ($this->resize_width, $this->resize_he                ight); 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-&                Gt;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->resiz                E_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->im=imagecreatefromjpeg ($this->srcimg);        } if ($this->type== ' gif ') {$this->im=imagecreatefromgif ($this->srcimg);        } if ($this->type== ' png ') {$this->im=imagecreatefrompng ($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; }//read the file the first few bytes to determine the file type function Checkfiletype ($filename) {$file =fopen ($filename, ' RB '); $bin =fread ($file, 2);// Read-only 2-byte fclose ($file), $strInfo [email protected] ("C2chars", $bin), $typeCode =intval ($strInfo [' chars1 ']. $strInfo [' chars2 ']); $fileType = '; switch ($typeCode) {case 7790: $fileType = ' exe '; break;case 7784: $fileType = ' midi '; break;case 8297:$ Filetype= ' rar '; break;case 255216: $fileType = ' jpg '; break;case 7173: $fileType = ' gif '; BreaK;case 6677: $fileType = ' bmp '; break;case 13780: $fileType = ' png '; Break;default: $fileType = ' unknown '. $typeCode; break;} if ($strInfo [' chars1 ']== '-1 ' && $strInfo [' chars2 ']== ' -40 ') {return ' JPG ';} if ($strInfo [' chars1 ']== ' -119 ' && $strInfo [' chars2 ']== ' + ') {return ' PNG ';} return $fileType;}}

The use of this class is also very simple, the following is based on the original image generated a width of not more than 100, high not more than 200 of the thumbnail image:

$resizeimage =new resizeimage (' www.phpernote.com.jpg ', 100,200,0, ' php and MySQL development_thumb.jpg ');

First, let's take a look at the program code for compressing images with PHP:

<?php2header (' content-type:image/png '); 3[email protected] (' http://www.phpernote.com/images/logo.png '); 4imagepng ($image, ' logo.png ', 0); Notice the number 0, which is the compression level, parameter range: 0-9*/5imagedestroy ($image);

The third parameter of the Imagepng function above is the one to be analyzed in this article, which means that the quality level of the image is generated. Here can be divided into 10 levels (0-9), 0 grade is no compression, the picture will not be distorted, the picture is the most clear, but the volume of the picture is also the largest, with the compression level of the number is increasing, the picture will become more and more unclear, but the volume of the compressed picture can be reduced to the original 50%, compression ratio is still

Here is a specific example, now an original volume is 125k, the following is a different compression level after the test results:

Imagepng ($img, null,0); --Size = 225K
Imagepng ($img, null,1); --Size = 85.9K
Imagepng ($img, null,2); --Size = 83.7K
Imagepng ($img, null,3); --Size = 80.9K
Imagepng ($img, null,4); --Size = 74.6K
Imagepng ($img, null,5); --Size = 73.8K
Imagepng ($img, null,6); --Size = 73K
Imagepng ($img, null,7); --Size = 72.4K
Imagepng ($img, null,8); --Size = 71K
Imagepng ($img, null,9); --Size = 70.6K

Note that when the compression level is 0, the volume is larger than the original picture is actually a slight compression, and 0 levels of compression is a little compression is not, so it will be larger than the original picture.

According to the above conclusions and the actual test results, the author concludes that in the optimization of the picture, the general use of 2 grade more appropriate, the picture is not distorted, but the volume reduced by 30%, to achieve the purpose of optimization. If the 6, 7, 8, 9 grade compression, the picture is distorted, but the volume reduction is not obvious. So it is not appropriate to use these ratings for compression optimization pictures

PHP class method for generating image thumbnails

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.