Write a perfect GIF picture in PHP, etc than the Zoom class, with the zoom black background removed

Source: Internet
Author: User
Tags transparent color
Write a perfect GIF picture in PHP, etc than the Zoom class, with the zoom black background removed

The file type is not judged here, because PNG and JPEG do not have a black background after zooming, so it is not possible to separate GIF.

Checked the information found a perfect solution, accompanied by a detailed ...

Now write things like encapsulation into class ..... Let's call it a bit. I'm not going to tell you how to call.

The effect is as follows:

 O_img_file= $oldfile;       $this->o_img_width= $width;       $this->o_img_height= $height;   $this->n_img_file= $newfile; }//proportional scaling and solve the problem of GIF transparent color black background function get_resize_scaling_img () {$this->n_img_width= $this->o_img_width* $this       ->o_to_n_per;       $this->n_img_height= $this->o_img_height* $this->o_to_n_per;             Equal scale picture (algorithm) if ($this->n_img_width && ($this->o_img_width < $this->o_img_height)) {       $this->n_img_width = ($this->n_img_height/$this->o_img_height) * $this->o_img_width; } else {$this->n_img_height = ($this->n_img_width/$this->o_img_width) * $this->o_img       _height;       } $this->o_img_source=imagecreatefromgif ($this->o_img_file);       Create a scaled-size canvas $this->n_img_source=imagecreatetruecolor ($this->o_img_width, $this->n_img_height); Landscaping: Remove black opaque background $trans _init=imagecolortransparent ($this-&GT;O_IMG_SOURCe);           Look for transparent color and determine if in total color if ($trans _init>=0 && $trans _init < Imagecolorstotal ($this->o_img_source)) {           If so, search for the RGB hue of this color $trans _index=imagecolorsforindex ($this->o_img_source, $trans _init); Once found, create such a color $trans _new=imagecolorallocate ($this->n_img_source, $trans _index["Red"], $trans _index["green"),           $trans _index["Blue"]);           Then we use this color to fill in the new Image Imagefill ($this->n_img_source,0,0, $trans _new);       Then we set the fill color to transparent imagecolortransparent ($this->n_img_source, $trans _new); }//Copy the original image onto the new artboard imagecopyresized ($this->n_img_source, $this->o_img_source,0,0,0,0, $this->n_img_width, $t       His->n_img_height, $this->o_img_width, $this->o_img_height);   return $this->n_img_source;       }//Final Destroy resource function __destruct () {Imagedestroy ($this->o_img_source);          Imagedestroy ($this->n_img_source); }}

Description: Because I didn't think so much before, I declared a lot of private internal variables to call ... The program looks clumsy ...

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