Home  >  Article  >  Backend Development  >  A perfect GIF proportional scaling class for PHP, with the removal of black background when scaling_PHP Tutorial

A perfect GIF proportional scaling class for PHP, with the removal of black background when scaling_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:34:58909browse

Nowadays, when writing things, I like to encapsulate them into classes... Just call them. I won't say how to call them

Copy code The code is 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 resource
private $n_img_file;//New image resource
private $n_img_source;//New image resource
private $o_to_n_per=0.5;//Image scaling ratio

//Initialize 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;
}

// Scale proportionally and solve the problem of GIF transparent color with 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;
//Scaling the image (algorithm)
if ( $this->n_img_width && ( $ this->o_img_width <$this->o_img_height))
                                                          o_img_width;
                                                                                      _img_height;
}
$this->o_img_source=imagecreatefromgif($this->o_img_file);
//Create a proportionally scaled canvas
$this->n_img_source=imagecreatetruecolor($this-> ;o_img_width,$this->n_img_height);

//Beautification: Remove the black opaque background
=0 && $trans_init < imagecolorstotal($this->o_img_source)){
                                                                                                                                                                                                                                                              ​ );

//Create such a color after finding it

"]);
//Then we use this color to fill the new image
imagefill($this->n_img_source,0,0,$trans_new);
//Then we fill the new image with the fill color Set to transparent ImageColortransparent ($ this-& gt; n_img_source, $ transfle);
}
// copy the original image
ImageCopyResized ($ This- & GT; 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;
}
//Finally destroy the resource
function __destruct(){
imagedestroy($this->o_img_source);
imagedestroy($this->n_img_source);

}

}


Explanation: Because I didn’t think so much before, I declared a lot of private internal variables for calling... The program looks clumsy...

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/748162.htmlTechArticleNowadays, people like to encapsulate things into classes... Just call them... I won't say it How to call the copy code is as follows: ?php class resize_image{ private $o_img_width;//Original image...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn