PHP's perfect GIF proportional scaling class, with the removal of the zoom black background. Now everything you write is encapsulated into a class... you just need to call it. I won't say how to call the Copy code as follows :? Phpclassresize_image {private $ o_img_width; the original image writes like encapsulation into a class now... you just need to call it... I won't say how to call it.
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; // The width of the new image.
Private $ n_img_height; // The height of the new image.
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;
}
// Proportional scaling to solve the problem that the GIF transparent color is 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;
// Proportional scaling image (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 canvas with proportional scaling
$ This-> n_img_source = imagecreatetruecolor ($ this-> o_img_width, $ this-> n_img_height );
// Beautify: remove the black and opaque background
$ Trans_init = imagecolortransparent ($ this-> o_img_source );
// Find the transparent color and determine whether it is in the total color
If ($ trans_init> = 0 & $ trans_init <imagecolorstotal ($ this-> o_img_source )){
// If yes, search for the RGB color.
$ Trans_index = imagecolorsforindex ($ this-> o_img_source, $ trans_init );
// Create such a color after finding it
$ Trans_new = imagecolorallocate ($ this-> n_img_source, $ trans_index ["red"], $ trans_index ["green"], $ trans_index ["blue"]);
// Then we use this color to fill the new image
Imagefill ($ this-> n_img_source, 0, 0, $ trans_new );
// Set the fill color to transparent.
Imagecolortransparent ($ this-> n_img_source, $ trans_new );
}
// Copy the original image to the new drawing board
Imagecopyresized ($ this-> n_img_source, $ this-> o_img_source, 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 );
}
}
Note: Because I didn't think so much, I declared a lot of private internal variables to call... the program looks clumsy ......
The pipeline code is as follows :? Php class resize_image {private $ o_img_width; // original image...