PHP is a perfect gif like the Zoom class, with the removal of scaling black background _php instances

Source: Internet
Author: User
Tags transparent color
Now write things like encapsulation into class ..... Let's call it a bit. I'm not going to tell you how to call.

Copy the Code code as follows: 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 zoom ratio

Initializing internal variables
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;
}

//proportional scaling and resolving a problem with 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;
//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->o_img_source);
Look for the transparent color and determine whether it is in the total color
if ($trans _init>=0 && $trans _init < Imagecolorstotal ($this->o_img_source)) {
If it is, search for the RGB hue of this color
$trans _index=imagecolorsforindex ($this->o_img_source, $trans _init);
Create a color after you find it
$trans _new=imagecolorallocate ($this->n_img_source, $trans _index["Red"], $trans _index["green", $trans _index[" Blue "]);
And then we use this color to fill in the new image.
Imagefill ($this->n_img_source,0,0, $trans _new);
And then we're setting 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, $this->n_img_ Height, $this->o_img_width, $this->o_img_height);
return $this->n_img_source;
}
Eventual destruction of resources
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.