"PHP thumbnail class" Phone photos do not generate thumbnail problems and solutions

Source: Internet
Author: User
Tags transparent color sca

" original, declined reprint "

First, the problem occurred these days to do the mobile phone upload photos and cut out the interface of the thumbnail test, found that no matter how, the generated thumbnail is a piece of black. :-(then took this thumbnail class list to test, found that as long as the phone is taken out of the photos can not be processed thumbnail image ....
Ii. Problem Analysis and Solutions
After the group of advice, found that the problem may appear in the file type of judgment, because the PNG image comes with a transparent layer, resulting in the file can not be directly converted to JPG, and the mobile phone out of the photo extension is jpg. So, the conclusion is A PNG image of the JPG extension is taken by the phone
Because the extension can be arbitrarily modified, it is not very able to guarantee the accuracy of the information of the file, so we used the getimagesize function to obtain the file type.
Get the real picture Type list ($width, $height, $type, $attr) = getimagesize ($this->sur_file);    Switch ($type) {case          1:              $img _type = ' gif ';              break;          Case 2:              $img _type = ' jpeg ';              break;          Case 3:              $img _type = ' png ';              break;          Case:              $img _type = ' wbmp ';              break;          Default:              return false;      }  


Third, generate thumbnail class
The following is the modified generation of thumbnails of the class paste out, for everyone to correct ~
/** * PHP generated thumbnail class * modifier dot drizzle * Article source: http://blog.csdn.net/diandianxiyu_geek * 2014-07-23 resolves an issue where the picture type is not recognized correctly */class thumb { Public $sur _file; Read the original picture public $des _file; Generate the target picture public $tem _file;  Temporary picture public $tag;  Thumbnail label 0, default, by a fixed high-width build 1, by the ratio column or fixed maximum length of 1, by a width or a height to reduce the public $resize _width;  $tag is 0 o'clock, the target file is wide public $resize _height; $tag is 0 o'clock, the target file is high public $sca _max;  $tag is 1 o'clock, <0$sca_max<1 is reduced, $SCA is the maximum length at _max>1 (maximum of high or wide) public $type;  Picture type public $width;  Original picture wide public $height;     Original picture high public $size; Original size//constructor Public function __construct ($surpic, $reswid, $reshei, $despic, $mark, $scamax) {$this->sur        _file = $surpic;        $this->resize_width = $reswid;        $this->resize_height = $reshei;        $this->tag = $mark;        $this->sca_max = $scamax;        List ($width, $height, $type, $attr) = getimagesize ($this->sur_file);    Switch ($type) {Case 1: $img _type = ' gif ';            Break                Case 2: $img _type = ' jpeg ';            Break                Case 3: $img _type = ' png ';            Break                Case: $img _type = ' wbmp ';            Break        Default:return false; } $this->type = $img _type; Gets the picture type $this->init_img (); Initialize picture $this->des_file = $despic;        Destination image address $this->width = $width;        $this->height = $height;        $this->size = filesize ($surpic);        $this->new_img ();    Imagedestroy ($this->tem_file); }//Picture initialization function Private Function init_img () {if ($this->type = = ' jpeg ') {$this->tem_file = ima        Gecreatefromjpeg ($this->sur_file);        } elseif ($this->type = = ' jpg ') {$this->tem_file = imagecreatefromjpeg ($this->sur_file); } elseif ($this->type = = ' gif ') {$this->tem_file = imagecreatefromgif ($this-> sur_file);        } elseif ($this->type = = ' png ') {$this->tem_file = imagecreatefrompng ($this->sur_file);        } elseif ($this->type = = ' bmp ') {$this->tem_file = imagecreatefrombmp ($this->sur_file);//bmp.php contains        }}//Picture generation function Private Function new_img () {$ratio = ($this->width)/($this->height);//original ratio $resize _ratio = ($this->resize_width)/($this->resize_height); After the thumbnail scale $newimg = Imagecreatetruecolor ($this->resize_width, $this->resize_height); Generate new picture imagealphablending ($newimg, false);        This is important, meaning that the color is not merged, directly replaced with $img image color, including transparent color;        Imagesavealpha ($newimg, true); if ($this->tag = = 0) {//By fixed high-width intercept thumbnail $newimg = Imagecreatetruecolor ($this->resize_width, $this->resize_ height); Create a new picture if ($ratio >= $resize _ratio) {//-proportional, the thumbnail's height is longer than the original, so the imagecopyresampled is high ($newimg, $th Is->tem_file, 0, 0, 0, 0, $this->resize_width, $this-> Resize_height, (($this->height) * $resize _ratio), $this->height); } elseif ($ratio < $resize _ratio) {//-proportional, the width of the thumbnail is longer than the original, so the width is constant imagecopyresampled ($newimg, $this->tem_fil            E, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize _ratio));                }} elseif ($this->tag = = 1) {//decrease by a fixed or maximum length if ($this->sca_max < 1) {//proportionally reduced $newimg = Imagecreatetruecolor ((($this->width) * ($this->sca_max), (($this->height) * ($this->sca_ma x)); Generate new picture imagecopyresampled ($newimg, $this->tem_file, 0, 0, 0, 0, (($this->width) * ($this->sca_max            ), (($this->height) * ($this->sca_max)), $this->width, $this->height);  } elseif ($this->sca_max > 1) {//by a maximum length to narrow if ($ratio >= 1) {//Width ratio to length $newimg = Imagecreatetruecolor ($this->sca_max, ($this->sca_max/$ratio));     Create a new picture               Imagecopyresampled ($newimg, $this->tem_file, 0, 0, 0, 0, $this->sca_max, ($this->sca_max/$ratio)                , $this->width, $this->height);                    } else {$newimg = Imagecreatetruecolor (($this->sca_max * $ratio), $this->sca_max);//Create new picture  Imagecopyresampled ($newimg, $this->tem_file, 0, 0, 0, 0, ($this->sca_max * $ratio), $this->sca_max,                $this->width, $this->height); }}} elseif ($this->tag = =-1) {//by a width or a certain height to narrow if ($resize _ratio >= 1) {//new high is less than the width, then the picture is pressed New width Reduction $newimg = Imagecreatetruecolor ($this->resize_width, ($this->resize_width/$ratio)); Generate new picture imagecopyresampled ($newimg, $this->tem_file, 0, 0, 0, 0, $this->resize_width, ($this->resi            ze_width/$ratio), $this->width, $this->height); } elseif ($resize _ratio < 1) {//The new width is smaller than the new height, the picture is scaled down $newimg = Imagecreatetruecolor ($this-&Gt;resize_height * $ratio), $this->resize_height); Generate new picture imagecopyresampled ($newimg, $this->tem_file, 0, 0, 0, 0, ($this->resize_height * $ratio), $th            Is->resize_height, $this->width, $this->height); }//Output new picture if ($this->type = = ' jpeg ' | | $this->type = = ' jpg ') {imagejpeg ($newimg,        $this->des_file);        } elseif ($this->type = = ' gif ') {imagegif ($newimg, $this->des_file);        } elseif ($this->type = = ' png ') {imagepng ($newimg, $this->des_file);  } elseif ($this->type = = ' bmp ') {imagebmp ($newimg, $this->des_file),//bmp.php contains}} #function New_img () End}




"PHP thumbnail class" Phone photos do not generate thumbnail problems and solutions

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.