I used PHP to write image scaling and image proportional Scaling Functions for one night.

Source: Internet
Author: User
Tags imagejpeg

/*
----------------------------------------------------------------------
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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.