kylereicks / picturefill.js.wp

A WordPress plugin to use Picturefill.js for image loading.
106 stars 10 forks source link

getimagesize - Connection refused #58

Closed iandevlin closed 9 years ago

iandevlin commented 9 years ago

First of all I am not sure if this is an issue in general, it might be something to do with our Wordpress installation, but I am mentioning it here as something to perhaps look into, as I have fixed it in my local version so would appreciate your thoughts.

I have installed the plugin, and despite having the correct permissions set, I was constantly getting the following errors:

Warning: getimagesize(IMAGE_URL): failed to open stream: Connection refused in /var/www/wp-content/plugins/picturefillwp/inc/class-model-image-picturefill-wp.php on line 169

(where IMAGE_URL is the actual URL of the requested image).

line 169:

$image_size = getimagesize($attachment_data['url']);

But I changed this to do the following:

$parsed_data = parse_url($attachment_data['url']);
$image_size = getimagesize($_SERVER['DOCUMENT_ROOT'] . $parsed_data['path']);

And now it works fine.

Any thoughts would be appreciated, thanks in advance.

tychay commented 9 years ago

Lolz, the plugin developer didn't consider what happens when you have a non-publically accessible version of the site.

Here's my guess of what's going on: Your dev site is locked in some way from itself. For example you have a HTTPAuth in front (requiring user password), or you have locally modified the /etc/hosts file to point to a VM instance (however the instance doesn't have an /etc/hosts).

Let's take the latter case. For instance, let's say you are using VVV to create a vagrant instance of WordPress. You are connecting to http://wordpress.dev/, and VVV has modified your dev box hosts file to map that onto a local port where your virtual box is running. However on the virtual box, it doesn't know wordpress.dev is itself so it starts calling the internets looking for the image. BAM! Connection refused.

I think a better change might be…

      if(ini_get('allow_url_fopen')){
        $image_size = @getimagesize($attachment_data['url']);
        if (!$image_size) { …your code… }

and then instead of the else below it, falling back to that last if there is no image size. I don't know because I personally think using the URL to extract attachment images is a terrible idea and the code should prefer the attachment id in the rel tag over trying to extract poorly written information from wordpress's image insert code.

Glancing at the plugin code, the reason it wants to run is to get the real image dimensions in case the <img /> tag was referencing an intermediate image. However, WordPress stores the sizes of all generated images buried in its metadata wp_get_attachment_metadata() It'd be better to use the attachment id and compare the basename() to that list. But what do I know, I don't store images in WordPress. :-(

iandevlin commented 9 years ago

Sounds about right, localhost is indeed pointing to a vagrant instance, thanks for that.

I have given up on this plugin though, I initially failed to see that the author isn't supporting it anymore.