4

I have a plugin that generates a gallery of user photos. Currently, it works using img tags pointing to the user photo image. It attaches to the the_content filter.

I'm experimenting with converting to using data URLs. I'm able to generate them with no difficulty, but when I change the code to insert them into the page, requests to that page come back with zero content (i.e. zero length). However, I still get a 200 status code and I don't see any errors in my logs.

So, I'm wondering if WP has a limit on the size of page it will send. The data URLs add up to around 1MB, so I was wondering if that tripped it over some limit.

Any suggestions?

The salient code is:

$file = trailingslashit($upload_dir['basedir']) . 'userphoto/' . $image_file;
$fileContents = base64_encode(file_get_contents($file));
error_log($fileContents);
$src = "data:image/jpg;base64,$fileContents";
...
...
$img .= '<img src="' . htmlspecialchars($src) . '"';
...
$img .= ' />';

In case it's relevant, the content is in a page not a post and it would go here: http://nerdnite.com/nerds/

So, that page, but with data URLs instead of image source files.

EDIT

What I do now know is that it's not the generation of the content string by the filter. I can generate the string (which is of length 1229923), but if I pass a dummy string back, everything works well.

My memory usage is negligible and I'm nowhere near my PHP limit... so it seems to be something to do with WordPress trying to handle that huge string.

10
  • how do you generate a page? show your code Commented Feb 21, 2013 at 15:36
  • The code simply loops over all of the users and generates an img element. If I set the src attribute to the URL of the image, it works without any problems. If I switch to set the src to a data URL, I get a zero-length response. The only difference between the two is the code above.
    – Dancrumb
    Commented Feb 21, 2013 at 15:52
  • I added links to the code. This is the version that is working. The snippet in the question comes from line 167 in user_photo.php
    – Dancrumb
    Commented Feb 21, 2013 at 15:55
  • why don't you want to use URLs of images? it is better, because browser can cache your images and the page load time will be reduced. Commented Feb 21, 2013 at 16:07
  • I'm experimenting to determine which is better. Putting images in as data-urls means that when page is loaded it's done... no need to request any other assets. I'll be doing A/B testing to see which version my users prefer.
    – Dancrumb
    Commented Feb 21, 2013 at 16:12

1 Answer 1

1

Some of your user pictures are 30K characters long when encoded. Have you heard of URL length limit? See What is the maximum length of a URL?.

Also I believe there are some limits were put by browser manufacturers in spite of HTML 4-5 specifications does not put any limits on attribute value length (AFAIK).

5
  • Why would that result in WordPress returning a zero-length response?
    – Dancrumb
    Commented Feb 21, 2013 at 16:09
  • Possibly it has own limit too. 30K long URL does not seems to be legit.
    – Max Yudin
    Commented Feb 21, 2013 at 16:11
  • Since I see this problem in FF, I don't think that's it. FF limit is unlimited (developer.mozilla.org/en-US/docs/data_URIs). If there's a WP limit, I'd love to know where it is.
    – Dancrumb
    Commented Feb 21, 2013 at 16:20
  • May be you are right. I've found this answer: Data protocol URL size limitations. When I'll get some time I'll dig into WP core to see if there are some trimmings or limitations. But remember, I'm not a guru, just my pleasure to help!
    – Max Yudin
    Commented Feb 21, 2013 at 17:15
  • Firefox 19.0 on Ubuntu returns no HTTP headers. Just nothing. Chromium 24 on Ubuntu 12.04 says "(Failed)". See URI (not URL) lenght.
    – Max Yudin
    Commented Feb 21, 2013 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.