Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




wp_crop_image [ WordPress Function ]

wp_crop_image ( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false )
Parameters:
  • (string|int) $src 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.
Returns:
  • (string|WP_Error|false) New filepath on success, WP_Error or false on failure.
Defined at:



Crop an Image to a given size.

Source


<?php
function wp_crop_image$src$src_x$src_y$src_w$src_h$dst_w$dst_h$src_abs false$dst_file false ) {
    if ( 
is_numeric$src ) ) { // Handle int as attachment ID
        
$src_file get_attached_file$src );
        if ( ! 
file_exists$src_file ) ) {
            
// If the file doesn't exist, attempt a url fopen on the src link.
            // This can occur with certain file replication plugins.
            
$post get_post$src );
            
$image_type $post->post_mime_type;
            
$src load_image_to_edit$src$post->post_mime_type'full' );
        } else {
            
$size = @getimagesize$src_file );
            
$image_type = ( $size ) ? $size['mime'] : '';
            
$src wp_load_image$src_file );
        }
    } else {
        
$size = @getimagesize$src );
        
$image_type = ( $size ) ? $size['mime'] : '';
        
$src wp_load_image$src );
    }

    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$dsttrue );

    
imagecopyresampled$dst$src00$src_x$src_y$dst_w$dst_h$src_w$src_h );

    
imagedestroy$src ); // Free up memory

    
if ( ! $dst_file )
        
$dst_file str_replacebasename$src_file ), 'cropped-' basename$src_file ), $src_file );

    if ( 
'image/png' != $image_type )
        
$dst_file preg_replace'/\\.[^\\.]+$/''.jpg'$dst_file );

    
// The directory containing the original file may no longer exist when
    // using a replication plugin.
    
wp_mkdir_pdirname$dst_file ) );

    
$dst_file dirname$dst_file ) . '/' wp_unique_filenamedirname$dst_file ), basename$dst_file ) );

    if ( 
'image/png' == $image_type && imagepng$dst$dst_file ) )
        return 
$dst_file;
    elseif ( 
imagejpeg$dst$dst_fileapply_filters'jpeg_quality'90'wp_crop_image' ) ) )
        return 
$dst_file;
    else
        return 
false;
}
?>

Examples [ wp-snippets.com ]

Top Google zoekresultaten

Meer ...

Gebruikersdiscussies [ wordpress.org ]

0 User Note(s)

Nog geen één. Wees de eerste!

Nieuw toevoegen ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics