Why does the upload image have a transparent channel and the image becomes black?

Source: Internet
Author: User
Tags transparent color
P.S. Change the image upload class original address: http://blog.csdn.net/a957651480/article/details/23124257

 Chk_err (-1); No incoming control name}else{$this->inputname = $inputName;} if (!empty ($allowType)) $this->allowtype = $allowType, if (!empty ($allowSize)) $this->allowsize = $allowSize; Empty ($saveDir)) $this->savedir = $saveDir, if (!empty ($isRename)) $this->isrename = $isRename;}    function is_uploaded () {if (Empty ($_files[$this->inputname][' name ')) {$this->chk_err (4); No files were uploaded}else{if (is_uploaded_file ($_files[$this->inputname][' tmp_name ')) {return true;}       else{$this->chk_err (-2); File Upload illegal}}}function chk_type () {if (!in_array ($_files[$this->inputname][' type '], $this->allowtype)) {$this-         >chk_err (-3); The uploaded file type is not allowed}else{return true;}}          function Chk_size () {if ($_files[$this->inputname][' size ') > $this->allowsize) {$this->chk_err (-4); The uploaded file is too large}else{return true;}} function move_uploaded () {//Move upload file if (! $this->is_uploaded ()) {return false;} if (! $this->chk_size ()) {return false;} if (! $this->chk_type ()) {return false;} Rename if ($this-> isrename) {$arrTmp = PathInfo ($_files[$this->inputname][' name '); $extension = Strtolower ($arrTmp [' extension ']); $file _newname = $this->filename; Rename new file}else{$file _newname = $_files[$this->inputname][' name '];}    if (!file_exists ($this->savedir)) {//Determine if the Save directory exists mkdir ($this->savedir,0777,true); Create a Save directory}//move File $result = Move_uploaded_file ($_files[$this->inputname][' tmp_name '), $this->savedir. " /". $file _newname), if ($result) {$path = $this->savepath = $this->savedir." /". $file the successful save path to the _newname;//file return $path;} else{$this->chk_err ($_files[$this->inputname][' error ');}} Error message function Chk_err ($errID) {$this->errid = $errID; switch ($this->errid) {case-4: $this->errmsg = ' Only images within 1M can be uploaded | [Return] '; break;case-3: $this->errmsg = ' This is not a picture | [Return] '; break;case-2: $this->errmsg = ' file Upload not valid | [Return] '; break;case-1: $this->errmsg = ' No control name passed in | [Return] '; Break;case 1: $this->errmsg = ' The uploaded file exceeds the maximum value of upload_max_filesize set in PHP.ini | [Return] '; Break;case 2: $this->errmsg = ' upload fileThe size exceeds the value specified by the MAX_FILE_SIZE option in the HTML form | [Return] '; Break;case 3: $this->errmsg = ' file only partially uploaded | [Return] '; Break;case 4: $this->errmsg = ' Please select a picture | [Return] '; break;default:break;} return false;}  function get_errmsg () {echo $this->errmsg; Output error message}/** +----------------------------------------------------------* Get image information * +----------------- -----------------------------------------* @static * @access public +---------------------------------------- ------------------* @param string $image image file name +----------------------------------------------------------*        @return Mixed +----------------------------------------------------------*/function Getimageinfo ($img) {        $imageInfo = getimagesize ($img);            if ($imageInfo!== false) {$imageType = Strtolower (substr (Image_type_to_extension ($imageInfo [2]), 1));            $imageSize = FileSize ($img); $info = Array ("width" = = $imageInfo [0], "height" => $imageInfo [1], "type" = = $imageType, "size" + = $imageSize, "mime" = = $ima            Geinfo[' MIME '],);        return $info;        }else {return false; }}/** +----------------------------------------------------------* Generate thumbnails +------------------------------- ---------------------------* @static * @access public +------------------------------------------------------ ----* @param string $image original * @param string $type image format * @param string $thumbname thumbnail filename * @param stri ng $maxWidth Width * @param string $maxHeight Height * @param string $position thumbnail Save directory * @param boolean $interlace Enable Interlaced * @param boolean $is _save whether to keep the original +----------------------------------------------------------* @return void +----------------------------------------------------------*/function thumb ($image, $is _save=true, $suofa ng=0, $type = ", $maxWidth =500, $maxHeight =500,$Interlace=true) {//Get the original information $info = $this->getimageinfo ($image);            if ($info!== false) {$srcWidth = $info [' width '];            $srcHeight = $info [' height '];            $type = Empty ($type)? $info [' type ']: $type; $type = Strtolower ($type); $interlace = $interlace?            1:0;            Unset ($info);                if ($suofang ==1) {$width = $srcWidth;            $height = $srcHeight; } else {$scale = min ($maxWidth/$srcWidth, $maxHeight/$srcHeight);//Calculate scaling if ($scale >=1                    {//above the original size no longer abbreviated $width = $srcWidth;                $height = $srcHeight;  }else{//thumbnail size $width = (int) ($srcWidth * $scale);//147 $height = (int) ($srcHeight * $scale);//199}}//Load the original $createFun = ' Imagecreatefrom '. ($type = = ' jpg '? ')            JPEG ': $type); $srCIMG = $createFun ($image); Create thumbnails if ($type! = ' gif ' && function_exists (' Imagecreatetruecolor ')) $THUMBIMG = Imagecre            Atetruecolor ($width, $height);            else $thumbImg = Imagecreate ($width, $height); Copy Picture if (Function_exists ("imagecopyresampled")) imagecopyresampled ($thumbImg, $srcImg, 0, 0, 0            , 0, $width, $height, $srcWidth, $srcHeight);            else imagecopyresized ($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight); if (' gif ' = = $type | | ' png ' = = $type) {//imagealphablending ($thumbImg, false);//cancels the default process-color mode//imagesavealpha ($thumbIm  G,true)//set to save complete alpha channel information $background _color = Imagecolorallocate ($thumbImg, 0,255,0);  Assign a green imagecolortransparent ($THUMBIMG, $background _color); Set to Transparent color, if the line is commented out the output green graph}//JPEG graphics set interlaced if (' jpg ' = = $type | | ' jpeg ' = = $type) Imageinterlace ($THUMBIMG, $interlace);            $gray =imagecolorallocate ($THUMBIMG, 255,0,0);            Imagestring ($THUMBIMG, 2,5,5, "thinkphp", $gray); Generate Picture $imageFun = ' imagejpeg '; $length = strlen ("00.").            $type) * ( -1); $_type = substr ($image, -4); $length = ($type! = $_type? $length + 1: $length); Crop if ($suofang = = 1) {//$thumbname = Substr_replace ($image, "01."). $type, $length);//Big head like $THUMBNAME02 = Substr_replace ($image, '. gif ',-8);//small head//$imageFun ($THUMBIMG, $thumbname 01,100)                ; $imageFun ($THUMBIMG, $thumbname 02,100);                $THUMBIMG: Imagecreatetruecolor (190,195); Imagecopyresampled ($THUMBIMG, $THUMBIMG, 0,0,$_post[' x '],$_post[' y '],190,195,$_post[' W '],$_post[' h ']);                THUMBIMG02 = Imagecreatetruecolor ($this->imgsize, $this->imgsize); Imagecopyresampled ($THUMBIMG, $THUMBIMG, 0,0,$_post[' x '],$_post[' y '), $this->imgsize, $this->imgsize,$_ post[' W '],$_post[' h ']);//$imageFun ($THUMBIMG, $THUMBName01,100), $imageFun ($THUMBIMG, $thumbname 02,100), unlink ($image);//imagedestroy ($THUMBIMG); Imagedestroy ($ THUMBIMG02); Imagedestroy ($THUMBIMG); Imagedestroy ($SRCIMG); return Array (/* ' big ' = $thumbname, */' small ' = = $ THUMBNAME02);//Returns an array}else{if ($is _save = = False) containing the size of the head path {//thumbnail overlay original, thumbnail path or original path $imagefun ($THUMBIMG, $image, 100); }else{$thumbname $image;//thumbnail and original image exist simultaneously, $imageFun ($THUMBIMG, $thumbname 03,100); Imagedestroy ($THUMBIMG);    Imagedestroy ($SRCIMG); return $thumbname 03;//Returns the path of the thumbnail, string}}} to false; }}


Reply to discussion (solution)

Uploading does not change the picture, you can paste the picture here
For GD2 thumbnails should be imagecreatetruecolor, imagecopyresampled
If you have a transparent color, you also need to imagecolortransparent

Put a picture on it and see

Put a picture on it and see





is a plain picture with a transparent channel ...


Put a picture on it and see





is a plain picture with a transparent channel ...


How did you upload the call, I just tried to put down your picture, I finished this pass no problem

The pictures produced by Imagecreate and Imagecreatetruecolor are black (note: not transparent)
Your picture is transparent, and naturally the background is revealed.
Which means the picture is on a black background.
So the scaled picture also needs to set the transparent color

$url = ' http://img.bbs.csdn.net/upload/201404/15/1397545309_839045.gif '; $sm = imagecreatefromstring (file_get_ Contents ($url)); $DM = Imagecreatetruecolor (Imagesx ($SM), Imagesy ($SM));//$DM = Imagecreate (Imagesx ($SM), Imagesy ($SM ); $c = Imagecolorallocate ($DM, 1, 1); Imagefill ($DM, 0, 0, $c); Imagecolortransparent ($DM, $c); Imagecopyresampled ($ DM, $SM, 0, 0, 0, 0, Imagesx ($DM), Imagesy ($DM), Imagesx ($SM), Imagesy ($SM)), Imagegif ($DM);


I solved it myself. Thank you, I didn't fill it with Imagefill beforehand.

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