Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

PHP "getimagesize" function gives error WordPress


In my WP theme, I'm needing, via PHP to find out the image size of a .jpg image.

Some sample code:

$file_path = parse_url("");

$file_path = ltrim( $file_path['path'], '/' );
// result is: themeforest/test/wp-content/uploads/2011/12/home18.jpg
// note: I also tried this: $file_path = $file_path['path'];

$thesize = getimagesize( $file_path );

Here is the error I get:

getimagesize(themeforest/test/wp-content/uploads/2011/12/home18.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /usr/www/users/buchmann/themeforest/test/wp-content/themes/Automotiv_TimthumbReplacement/index.php on line 136

The image does exist:

I have to parse the URL because I get another error saying I can't use a path with "http://" in it, unless I edit a php.ini file. (or something to that effect)

Thanks for the help!

Answers (4)


Ross Wilson answers:

What about this?
$thesize = getimagesize( ABSPATH . $file_path );

John Buchmann comments:

I still get a similar error:

getimagesize(/usr/www/users/buchmann/themeforest/test//themeforest/test/wp-content/uploads/2011/12/home18.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /usr/www/users/buchmann/themeforest/test/wp-content/themes/Automotiv_TimthumbReplacement/index.php on line 136

Ross Wilson comments:

This should work then
$file_path = 'wp-content'. end(explode('wp-content',""));
$thesize = getimagesize( ABSPATH . $file_path);

John Buchmann comments:

This works, thanks! Gives the same end results as Jeffri, which give the answer first, but is a little more complex. I will split the reward between you two. :)


Jeffri Hong answers:

You will need to get the absolute path. We can check the URL againts the siteurl configured. Try this code:

$file_path = parse_url("");
$siteurl_path = parse_url(site_url());

$file_path = ltrim(str_replace($siteurl_path['path'], '', $file_path['path']), '/');
$thesize = getimagesize( ABSPATH. $file_path );

John Buchmann comments:

Thanks, this works! But the first poster to my question also gave a different but correct answer (less code than yours). I will split the reward between you two. Thanks again.


idt answers:

I think the image path needs to be full url.

Please try:

$file_path = "";

$thesize = getimagesize( $file_path );

John Buchmann comments:

When I use a full path, I get a different error:

http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /usr/www/users/buchmann/themeforest/test/wp-content/themes/Automotiv_TimthumbReplacement/index.php on line 134


Luis Abarca answers:

Try this

$file_path = parse_url("");

$file_path = str_replace('themeforest/test/wp-content', '', ltrim( $file_path['path'], '/' ) );
$file_path = WP_CONTENT_DIR . $file_path;

$thesize = getimagesize( $file_path );

Its like Ross Wilson said, but your are duplicating the path.