/*
----------------------------------------------------------------------
Function: Adjust the image size or generate a thumbnail.
Modified:
Return Value: True/False
Parameters:
$ Image: The Image to be adjusted (including the path)
$ Dw = 450 max width for resizing; absolute width for thumbnails
$ Dh = 450 max. height when adjusting; absolute height when thumbnails
$ Type = 1 1, adjust the size; 2, generate a thumbnail
Author: Seven (QQ: 9256114) WWW.7DI. NET */
Function Img ($ Image, $ Dw = 450, $ Dh = 450, $ Type = 1 ){
IF (! File_Exists ($ Image )){
Return False;
}
# If you want to generate a thumbnail, copy the source Image and assign a value to $ Image.
IF ($ Type! = 1 ){
Copy ($ Image, Str_Replace (".", "_ x.", $ Image ));
$ Image = Str_Replace (".", "_ x.", $ Image );
}
# Obtain the file type and create different Objects Based on Different Types
$ ImgInfo = GetImageSize ($ Image );
Switch ($ ImgInfo [2]) {
Case 1:
$ Img = @ ImageCreateFromGIF ($ Image );
Break;
Case 2:
$ Img = @ ImageCreateFromJPEG ($ Image );
Break;
Case 3:
$ Img = @ ImageCreateFromPNG ($ Image );
Break;
}
# If the object is not created successfully, it indicates that the object is not an image file.
IF (Empty ($ Img )){
# If an error occurs when generating a thumbnail, delete the copied file.
IF ($ Type! = 1) {Unlink ($ Image );}
Return False;
}
# If you want to adjust the size
IF ($ Type = 1 ){
$ W = ImagesX ($ Img );
$ H = ImagesY ($ Img );
$ Width = $ w;
$ Height = $ h;
IF ($ width> $ Dw ){
$ Par = $ Dw/$ width;
$ Width = $ Dw;
$ Height = $ height * $ Par;
IF ($ height> $ Dh ){
$ Par = $ Dh/$ height;
$ Height = $ Dh;
$ Width = $ width * $ Par;
}
} ElseIF ($ height> $ Dh ){
$ Par = $ Dh/$ height;
$ Height = $ Dh;
$ Width = $ width * $ Par;
IF ($ width> $ Dw ){
$ Par = $ Dw/$ width;
$ Width = $ Dw;
$ Height = $ height * $ Par;
}
} Else {
$ Width = $ width;
$ Height = $ height;
}
$ NImg = ImageCreateTrueColor ($ width, $ height); # create a true color canvas
ImageCopyReSampled ($ nImg, $ Img, $ width, $ height, $ w, $ h); # copy part of the image and resize it.
ImageJpeg ($ nImg, $ Image); # output the Image to a browser or file in JPEG format
Return True;
# If you want to generate a thumbnail
} Else {
$ W = ImagesX ($ Img );
$ H = ImagesY ($ Img );
$ Width = $ w;
$ Height = $ h;
$ NImg = ImageCreateTrueColor ($ Dw, $ Dh );
IF ($ h/$ w> $ Dh/$ Dw) {# relatively high
$ Width = $ Dw;
$ Height = $ h * $ Dw/$ w;
$ IntNH = $ height-$ Dh;
ImageCopyReSampled ($ nImg, $ Img, 0,-$ intnh/ 1.8, 0, 0, $ Dw, $ height, $ w, $ h );
} Else {# relatively wide
$ Height = $ Dh;
$ Width = $ w * $ Dh/$ h;
$ IntNW = $ width-$ Dw;
ImageCopyReSampled ($ nImg, $ Img,-$ IntNW/1.8, 0, 0, 0, $ width, $ Dh, $ w, $ h );
}
ImageJpeg ($ nImg, $ Image );
Return True;
}
}
$ A = Img ("./1.jpg"); # proportional Scaling
// $ A = Img ("./1.jpg", 150,230, 2); # generate a thumbnail
Echo $ a. Chr (10 );