I have not determined the file type here, because the png and jpeg images do not show a black background after scaling, And the gif image cannot be used independently .. I checked the information and found a perfect solution, with a detailed explanation. Now I like to encapsulate everything into a class ..... 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 ......