Definition:
function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {}
Crop an Image to a given size.
Parameters
- string|int $src_file: The source file or Attachment ID.
- int $src_x: The start x position to crop from.
- int $src_y: The start y position to crop from.
- int $src_w: The width to crop.
- int $src_h: The height to crop.
- int $dst_w: The destination width.
- int $dst_h: The destination height.
- int $src_abs: Optional. If the source crop points are absolute.
- string $dst_file: Optional. The destination file to write to.
Return values
returns:New filepath on success, WP_Error or false on failure.
Defined filters
- jpeg_quality
apply_filters( 'jpeg_quality', 90, 'wp_crop_image' )
Source code
function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) { if ( is_numeric( $src_file ) ) // Handle int as attachment ID $src_file = get_attached_file( $src_file ); $src = wp_load_image( $src_file ); if ( !is_resource( $src ) ) return new WP_Error( 'error_loading_image', $src, $src_file ); $dst = wp_imagecreatetruecolor( $dst_w, $dst_h ); if ( $src_abs ) { $src_w -= $src_x; $src_h -= $src_y; } if (function_exists('imageantialias')) imageantialias( $dst, true ); imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); imagedestroy( $src ); // Free up memory if ( ! $dst_file ) $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file ); $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file ); if ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) ) return $dst_file; else return false; }
3525
No comments yet... Be the first to leave a reply!