Home >Backend Development >PHP Tutorial >A perfect GIF proportional scaling class for PHP, with the removal of black background when scaling_PHP Tutorial
Nowadays, when writing things, I like to encapsulate them into classes... Just call them. I won't say how to call them
//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)){
);
"]);
//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
}
// 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...