PHP A perfect GIF zoom class, with the removal of the zoom black background

Source: Internet
Author: User

  I do not have a judgment file type here, because a black background will not appear after PNG and JPEG scaling, but not on a single GIF. Check the data found a perfect solution, with a detailed explanation

Now write things like packaging into classes ... Let's call it a minute. I'm not going to say how to call the   code as follows: <?php class resize_image{   private $o _img_width;//original image width    private $o _img _height;//Original Image height    private $n _img_width;//New Image width    private $n _img_height;//new Image height     Private $o _img_file;//Original image file    private $o _img_source;//original image resources    private $n _img_file;//New Image Resources    private $n _img_source;//New image resources    private $o _to_n_per=0.5;//image scaling ratio      //initializing internal variables   & Nbsp;function __construct ($oldfile, $newfile) {       list ($width, $height) =getimagesize ($oldfile );         $this->o_img_file= $oldfile;         $this->o_img_width= $width;         $this->o_img_height= $height;         $this->n_img_file= $newfile;   &NBSP}      //scaling and resolving issues with GIF transparent color as black background    function get_resize_scaling_img () {  & nbsp     $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;        //scaling (algorithm)        if ($this->n_img_width && ($this->o_im G_width < $this->o_img_height))        {              $this-&G T;n_img_width = ($this->n_img_height/$this->o_img_height) * $this->o_img_width;       &NBSP        else        {        &NBS P   $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 proportional scaled canvas         $this->n_img_source=imagecreatetruecolor ($ This->o_img_width, $this->n_img_height);          //beautification: Remove black opaque background         $trans _init=imagecolortransparent ($this- >o_img_source);        //looking for transparent color and determining whether in the total color        if ($trans _init>=0 && $trans _init < Imagecolorstotal ($this->o_img_source)) {           //If so, search this color for RGB hue   & nbsp         $trans _index=imagecolorsforindex ($this->o_img_source, $trans _init);            //found after the creation of such a color             $trans _new=imagecoloral Locate ($this->n_img_source, $trans _index["Red"], $trans _index["green"], $trans _index["Blue");            //Then we use this color to populate 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); &nBsp     &nbsp        //copy original image to new artboard on        imagecopyresized ($this->n_ Img_source, $this->o_img_source,0,0,0,0, $this->n_img_width, $this->n_img_height, $this->o_img_width,$ This->o_img_height);         return $this->n_img_source;   &NBSP    //Final destruction resource    function __destruct () {       imagedestroy ($this-& Gt;o_img_source);        imagedestroy ($this->n_img_source);     &NBSP}         Description: Because we didn't think so much earlier, we declared a lot of private internal variables to call ... The program looks awkward.

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.