- // Image cropping and scaling functions
- // $ Filepath: Image path, $ percent scaling percentage
- Function imagepress ($ filepath, $ percent = '0. 5 '){
- // Image type
- Header ('content-Type: image/jpeg ');
- // Obtain the new image size
- List ($ width, $ height) = getimagesize ($ filepath );
- $ New_width = $ width * $ percent;
- $ New_height = $ height * $ percent;
- // Re-sample
- $ Image_p = imagecreatetruecolor ($ new_width, $ new_height );
- $ Image = imagecreatefromjpeg ($ filepath );
- Imagecopyresampled ($ image_p, $ image, 0, 0, 0, 0, $ new_width, $ new_height, $ width, $ height );
- // Output
- Return imagejpeg ($ image_p, null, 100 );
- }
SOURCE image: : Example:
// $ Filepath: specifies the image path, $ new_width indicates the new width, and $ new_height indicates the new height.
- Function imagepress ($ filepath, $ new_width, $ new_height)
- {
- $ Source_info = getimagesize ($ filepath );
- $ Source_width = $ source_info [0];
- $ Source_height = $ source_info [1];
- $ Source_mime = $ source_info ['Mime '];
- $ Source_ratio = $ source_height/$ source_width;
- $ Target_ratio = $ new_height/$ new_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 ($ filepath );
- Break;
- Case 'image/jpeg ':
- $ Source_image = imagecreatefromjpeg ($ filepath );
- Break;
- Case 'image/png ':
- $ Source_image = imagecreatefrompng ($ filepath );
- Break;
- Default:
- Return false;
- Break;
- }
- $ Target_image = imagecreatetruecolor ($ new_width, $ new_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, $ new_width, $ new_height, $ cropped_width, $ cropped_height );
- Header ('content-Type: image/jpeg ');
- Imagejpeg ($ target_image );
- Imagedestroy ($ source_image );
- Imagedestroy ($ target_image );
- Imagedestroy ($ cropped_image );
- }
|