PHP Arbitrary Image cropping to a fixed size

Source: Internet
Author: User
Tags imagecopy imagejpeg

When you create a homepage call image, you often need to obtain a fixed-size image, because the image position of the homepage is usually specified by the designer. If you want to call the latest published image, because the customer does not know what proportion of the image will be uploaded, sometimes there is no way to determine the proportion of the image, the front-end page writers usually adopt a fixed img element height and width method to control image overflow. However, if the image proportion is not the necessary proportion, it will cause deformation after the image is called, this greatly affects the appearance of the page. The solution is to scale the page according to the original image proportion. The scaled image will inevitably be blank and the blank space will be filled with color, in this way, although the image is not deformed, there will be many problems. For example, if a user sends an image with a high image width but a general image width, if the image is compressed into a image, after compression, the image is basically invisible.
The solution here is that any image is cropped to a fixed size, the image is not deformed, the blank space is stretched and filled, and the image is always full without blank space. Friends who have used bcastr should know, bcastr ensures that the image call is not distorted. For a fixed size output image frame, the source image has the following situations:
1: the height and width of the source image to be output are both small. Write $ new_width <$ src_width & $ new_height <$ src_width
2: The Aspect Ratio of the image to be output is large in the height and width of the source image. Write $ new_width> $ src_width & $ new_height> $ src_width
3: exclude the second and second types, that is, if one side is enlarged and the other side is scaled down, add an equal judgment.
For 1, 2, the function processing code is identical, so it can be summarized into a processing statement.

Php implementation code

<? Php
/*
* Description: The function is to crop an image of any size without deformation.
* Parameter description: Enter the file name of the image to be processed, generate the save file name of the new image, generate the width of the new image, and generate the height of the new image.
* Written by smallchicken
* Time
*/
// Obtain an image of any size. The image is stretched out in a few places without deformation or blank space.
Function my_image_resize ($ src_file, $ dst_file, $ new_width, $ new_height ){
If ($ new_width <1 | $ new_height <1 ){
Echo "params width or height error! ";
Exit ();
}
If (! File_exists ($ src_file )){
Echo $ src_file. "is not exists! ";
Exit ();
}
// Image type
$ Type = exif_imagetype ($ src_file );
$ Support_type = array (IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF );
If (! In_array ($ type, $ support_type, true )){
Echo "this type of image does not support! Only support jpg, gif or png ";
Exit ();
}
// Load image
Switch ($ type ){
Case IMAGETYPE_JPEG:
$ Src_img = imagecreatefromjpeg ($ src_file );
Break;
Case IMAGETYPE_PNG:
$ Src_img = imagecreatefrompng ($ src_file );
Break;
Case IMAGETYPE_GIF:
$ Src_img = imagecreatefromgif ($ src_file );
Break;
Default:
Echo "Load image error! ";
Exit ();
}
$ W = imagesx ($ src_img );
$ H = imagesy ($ src_img );
$ Ratio_w = 1.0 * $ new_width/$ w;
$ Ratio_h = 1.0 * $ new_height/$ h;
$ Ratio = 1.0;
// The Aspect Ratio of the generated image is small or big. The principle is to enlarge the image by a large proportion and reduce the image by a large proportion (the scaled down ratio is relatively small)
If ($ ratio_w <1 & $ ratio_h <1) | ($ ratio_w> 1 & $ ratio_h> 1 )){
If ($ ratio_w <$ ratio_h ){
$ Ratio = $ ratio_h; // Case 1: The width ratio is smaller than the height direction. Crop or zoom in according to the height ratio standard.
} Else {
$ Ratio = $ ratio_w;
}
// Define an intermediate temporary image. The Aspect Ratio of the image meets the target requirements.
$ Inter_w = (int) ($ new_width/$ ratio );
$ Inter_h = (int) ($ new_height/$ ratio );
$ Inter_img = imagecreatetruecolor ($ inter_w, $ inter_h );
Imagecopy ($ inter_img, $ src_img, 0, 0, 0, $ inter_w, $ inter_h );
// Generate a temporary image with the maximum edge length as the size of the target image $ ratio
// Define a new image
$ New_img = imagecreatetruecolor ($ new_width, $ new_height );
Imagecopyresampled ($ new_img, $ inter_img, 0, 0, 0, $ new_width, $ new_height, $ inter_w, $ inter_h );
Switch ($ type ){
Case IMAGETYPE_JPEG:
Imagejpeg ($ new_img, $ dst_file, 100); // store images
Break;
Case IMAGETYPE_PNG:
Imagepng ($ new_img, $ dst_file, 100 );
Break;
Case IMAGETYPE_GIF:
Imagegif ($ new_img, $ dst_file, 100 );
Break;
Default:
Break;
}
} // End if 1
// 2. If one side of the target image is greater than the source image and the other side is smaller than the source image, the image is enlarged and then cropped.
// = If ($ ratio_w <1 & $ ratio_h> 1) | ($ ratio_w> 1 & $ ratio_h <1 ))
Else {
$ Ratio = $ ratio_h> $ ratio_w? $ Ratio_h: $ ratio_w; // The value with a large proportion.
// Define a large middle image with the same height or width as the target image, and then enlarge the source Image
$ Inter_w = (int) ($ w * $ ratio );
$ Inter_h = (int) ($ h * $ ratio );
$ Inter_img = imagecreatetruecolor ($ inter_w, $ inter_h );
// Scale and crop the source Image
Imagecopyresampled ($ inter_img, $ src_img, 0, 0, 0, $ inter_w, $ inter_h, $ w, $ h );
// Define a new image
$ New_img = imagecreatetruecolor ($ new_width, $ new_height );
Imagecopy ($ new_img, $ inter_img, 0, 0, 0, $ new_width, $ new_height );
Switch ($ type ){
Case IMAGETYPE_JPEG:
Imagejpeg ($ new_img, $ dst_file, 100); // store images
Break;
Case IMAGETYPE_PNG:
Imagepng ($ new_img, $ dst_file, 100 );
Break;
Case IMAGETYPE_GIF:
Imagegif ($ new_img, $ dst_file, 100 );
Break;
Default:
Break;
}
} // If3
} // End function
?>

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.