PHP methods for image cropping and scaling-PHP source code

Source: Internet
Author: User
Tags crop image imagecopy imagejpeg
The size of the image is specified and php-related functions are used to crop the image. The js effect in the previous section is not described here. You can find a lot of such code on the Internet, I only sorted out the php operations. The size of the image is specified and php-related functions are used to crop the image. The js effect in the previous section is not described here. You can find a lot of such code on the Internet, I only sorted out the php operations.

Script ec (2); script

Method 1

The Code is as follows:

The image cropping and scaling function supports square and custom coordinate cropping.
/**
* The image cropping function supports the specified fixed-point cropping and orientation cropping modes.
* @ Param $ Src_file original image path
* @ Param $ New_width: width of the cropped image (when the width of the original image exceeds the width of the original image)
* @ Param $ New_height: Specifies the height of the cropped image. When the width of the cropped image exceeds the width of the original image, remove the original image height)
* @ Param $ Type: crop in 1-azimuth mode and crop in 0-fixed point mode.
* @ Param $ Start orientation of the pos orientation mode during cropping (this parameter does not work when the fixed point mode is selected for cropping)
* 1 is the top left, 2 is the top center, and 3 is the top right;
* 4: center left, 5: center, and 6: center right;
* 7 indicates that the bottom is left, 8 indicates that the bottom is center, and 9 indicates that the bottom is right;
* @ Param $ Start_x start position X (this parameter does not work when the orientation mode is selected for cropping)
* @ Param $ Start_y start position Y (this parameter does not work when the orientation mode is selected for cropping)
* @ Return Crop image storage path
*/
Function thumb ($ src_file, $ new_width, $ new_height, $ type = 1, $ pos = 5, $ start_x = 0, $ start_y = 0 ){
$ Pathinfo = pathinfo ($ src_file );
$ Dst_file = $ pathinfo ['dirname']. '/'. $ pathinfo ['filename']. '_'. $ new_width. 'X '. $ new_height. '. '. $ pathinfo ['extension'];
If (! File_exists ($ dst_file )){
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
$ Img_type = exif_imagetype ($ src_file );
$ Support_type = array (IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF );
If (! In_array ($ img_type, $ support_type, true )){
Echo "only jpg, png, and gif image cropping is supported ";
Exit ();
}
/* Load the image */
Switch ($ img_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 "image loading error! ";
Exit ();
}
/* Obtain the width and height of the source image */
$ Src_width = imagesx ($ src_img );
$ Src_height = imagesy ($ src_img );
/* Calculate the width and height of the cut image */
$ Mid_width = ($ src_width <$ new_width )? $ Src_width: $ new_width;
$ Mid_height = ($ src_height <$ new_height )? $ Src_height: $ new_height;
/* Initialize the coordinates of the starting position of the source image cropping */
Switch ($ pos * $ type ){
Case 1: // 1 is the top left
$ Start_x = 0;
$ Start_y = 0;
Break;
Case 2: // 2 center the top
$ Start_x = ($ src_width-$ mid_width)/2;
$ Start_y = 0;
Break;
Case 3: // 3: Top right
$ Start_x = $ src_width-$ mid_width;
$ Start_y = 0;
Break;
Case 4: // 4 is left in the middle
$ Start_x = 0;
$ Start_y = ($ src_height-$ mid_height)/2;
Break;
Case 5: // 5 center in the middle
$ Start_x = ($ src_width-$ mid_width)/2;
$ Start_y = ($ src_height-$ mid_height)/2;
Break;
Case 6: // 6 is the center and right
$ Start_x = $ src_width-$ mid_width;
$ Start_y = ($ src_height-$ mid_height)/2;
Break;
Case 7: // 7 is left at the bottom
$ Start_x = 0;
$ Start_y = $ src_height-$ mid_height;
Break;
Case 8: // 8 is centered at the bottom
$ Start_x = ($ src_width-$ mid_width)/2;
$ Start_y = $ src_height-$ mid_height;
Break;
Case 9: // 9: Right
$ Start_x = $ src_width-$ mid_width;
$ Start_y = $ src_height-$ mid_height;
Break;
Default: // random
Break;
}
// Create a background canvas for the cut Image
$ Mid_img = imagecreatetruecolor ($ mid_width, $ mid_height );
// Copy the cut image data to the canvas to generate the cut Image
Imagecopy ($ mid_img, $ src_img, 0, 0, $ start_x, $ start_y, $ mid_width, $ mid_height );
// Create a background canvas for the cropped Image
$ New_img = imagecreatetruecolor ($ new_width, $ new_height );
// Copy and cut the image to the background canvas and crop it proportionally.
Imagecopyresampled ($ new_img, $ mid_img, 0, 0, 0, 0, $ new_width, $ new_height, $ mid_width, $ mid_height );
/* Save As image by format */
Switch ($ img_type ){
Case IMAGETYPE_JPEG:
Imagejpeg ($ new_img, $ dst_file, 100 );
Break;
Case IMAGETYPE_PNG:
Imagepng ($ new_img, $ dst_file, 9 );
Break;
Case IMAGETYPE_GIF:
Imagegif ($ new_img, $ dst_file, 100 );
Break;
Default:
Break;
}
}
Return ltrim ($ dst_file ,'.');
}

The attachment is a test demo with a sample image.
However, this function does not support dynamic GIF image cropping and scaling.

Method 2,

The Code is as follows:

List ($ src_w, $ src_h) = getimagesize ($ src_img); // obtain the source image size
$ Dst_scale = $ dst_h/$ dst_w; // aspect ratio of the Target Image
$ Src_scale = $ src_h/$ src_w; // aspect ratio of the source Image

If ($ src_scale> = $ dst_scale)
{
// Too high
$ W = intval ($ src_w );
$ H = intval ($ dst_scale * $ w );
$ X = 0;
$ Y = ($ src_h-$ h)/3;
}
Else
{
// Too wide
$ H = intval ($ src_h );
$ W = intval ($ h/$ dst_scale );
$ X = ($ src_w-$ w)/2;
$ Y = 0;
}
// Crop
$ Source = imagecreatefromjpeg ($ src_img );
$ Croped = imagecreatetruecolor ($ w, $ h );
Imagecopy ($ croped, $ source, 0, 0, $ x, $ y, $ src_w, $ src_h );
// Zoom
$ Scale = $ dst_w/$ w;
$ Target = imagecreatetruecolor ($ dst_w, $ dst_h );
$ Final_w = intval ($ w * $ scale );
$ Final_h = intval ($ h * $ scale );
Imagecopysampled ($ target, $ croped, 0, 0, 0, $ final_w, $ final_h, $ w, $ h );
// Save
$ Timestamp = time ();
Imagejpeg ($ target, "success ");
Imagedestroy ($ target );
?>

Method 3


Crop and scale the source Image Based on the height-to-width ratio of the custom target image.

Input parameters:

The Code is as follows:

$ Source_path string source image path

$ Target_width integer Target Image Width

$ Target_height integer Target Image Height

Supported image types: image/gif, image/jpeg, and image/png.

Function imagecropper ($ source_path, $ target_width, $ target_height)
{
$ Source_info = getimagesize ($ source_path );
$ Source_width = $ source_info [0];
$ Source_height = $ source_info [1];
$ Source_mime = $ source_info ['mime '];
$ Source_ratio = $ source_height/$ source_width;
$ Target_ratio = $ target_height/$ target_width;

// The Source image is too high.
If ($ source_ratio> $ target_ratio)
{
$ Cropped_width = $ source_width;
$ Cropped_height = $ source_width * $ target_ratio;
$ Source_x = 0;
$ Source_y = ($ source_height-$ cropped_height)/2;
}
// The Source image is too wide
Elseif ($ source_ratio <$ target_ratio)
{
$ Cropped_width = $ source_height/$ target_ratio;
$ Cropped_height = $ source_height;
$ Source_x = ($ source_width-$ cropped_width)/2;
$ Source_y = 0;
}
// Moderate source Image
Else
{
$ Cropped_width = $ source_width;
$ Cropped_height = $ source_height;
$ Source_x = 0;
$ Source_y = 0;
}

Switch ($ source_mime)
{
Case 'image/gif ':
$ Source_image = imagecreatefromgif ($ source_path );
Break;

Case 'image/jpeg ':
$ Source_image = imagecreatefromjpeg ($ source_path );
Break;

Case 'image/png ':
$ Source_image = imagecreatefrompng ($ source_path );
Break;

Default:
Return false;
Break;
}

$ Target_image = imagecreatetruecolor ($ target_width, $ target_height );
$ Cropped_image = imagecreatetruecolor ($ cropped_width, $ cropped_height );

// Crop
Imagecopy ($ cropped_image, $ source_image, 0, 0, $ source_x, $ source_y, $ cropped_width, $ cropped_height );
// Zoom
Imagecopyresampled ($ target_image, $ cropped_image, 0, 0, 0, 0, $ target_width, $ target_height, $ cropped_width, $ cropped_height );

Header ('content-Type: image/jpeg ');
Imagejpeg ($ target_image );
Imagedestroy ($ source_image );
Imagedestroy ($ target_image );
Imagedestroy ($ cropped_image );
}

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.