Sunday, May 19, 2013

How to use getimagesize() of image upload?

How to use getimagesize() of image upload?

I need to check the resolution of an image I want to upload via media box, and then upload it into a custom directory.
As a starting point I used part of the code from How Can I Organize the Uploads Folder by Slug (or ID, or FileType, or Author)?
And a part of it looks like this:
function wpse_25894_custom_upload_dir($path)
{  

    $use_default_dir = ( isset($_REQUEST['post_id'] ) && $_REQUEST['post_id'] == 0 ) ? true : false;
    if( !empty( $path['error'] ) || $use_default_dir )
        return $path; //error or uploading not from a post/page/cpt

    //THIS LINE PRODUCE ERROR. HERE I NEED TO GET WIDTH AND HEIGHT OF IMAGE I WANT TO UPLOAD
    list($width, $height) = getimagesize($path.'/'.$_POST['name']);
    $customdir = '/'.$width.'x'.$height; //uplaod folder will be for example '640x480'

    $path['path']    = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month)
    $path['url']     = str_replace($path['subdir'], '', $path['url']);     
    $path['subdir']  = $customdir;
    $path['path']   .= $customdir;
    $path['url']    .= $customdir; 

    return $path;
}
$_POST['name'] contains file name of image, like 'myimage.jpg'.

No comments:

Post a Comment