Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been working on a website used to promote Tumblrs. I just created my first PHP Image Server script, so that we can save Tumblr Avatars and reduce those pesky Tumblr API calls.

I have it working on the site now, however there is an odd glitch that's arising. When the page is opened, there's almost always 1 or 2 of the avatars that are showing up as broken images.

Page with behavior: http://www.tumblrlink.com/

I know it's not specific to a particular account, since refreshing the page will usually show a different set of images broken.

This is the Image Server Script I built:

<?php

if (!isset($_GET['uri']))
    exit();

$url = urldecode($_GET['uri']);

// Check to make sure API URL is passed.
if (strpos($url,'http://api.tumblr.com/') === FALSE)
    exit();

// Get the User's Tumblr "Short Name"
$shortName = $url;
$shortName = str_replace('http://api.tumblr.com/v2/blog/','',$shortName);
$shortName = str_replace('/avatar/40','',$shortName);

$avatarLoc = '../../media/avatars/' . $shortName . '.png';

// Save the Avatar to the server for use.
if (!file_exists($avatarLoc)) {
    copy($url, $avatarLoc);
}

//  Write out the contents of the Avatar Image.
header("Content-Type: image/png");
header("Content-Length: " . filesize($avatarLoc));
readfile($avatarLoc);

exit();

?>

Now, as you can see on the link, the images are saved and served successfully... but only 90% of the time.

Is there some kind of programmed timing issue going on here, or could this be an issue with the server / environment? Any help would be appreciated.

share|improve this question
1  
Any answer we could provide would be pure speculation. The best way to find the problem is to update your code to log every step of the the process so that you can see when and where it fails. You can then consult the logs when you observe a failure to identify a common factor. –  George Cummins Jun 17 '13 at 20:01
    
try using file_get_contents OR cURL to fetch the image data... and see wassup... –  aSeptik Jun 17 '13 at 20:02
    
@GeorgeCummins I know that there's the probability that there's no way to figure this out without access to the server. I was just wondering if anyone in the community has had a similar experience with, say, the readfile() function, and knows a work around. –  TbWill4321 Jun 17 '13 at 20:09
    
@aSeptik Thanks for the suggestion. I replaced readfile() with file_get_contents() and echoing the result, however there was no change. –  TbWill4321 Jun 17 '13 at 20:12
    
also, i see you are reading the entire content while it's already saved locally via copy() function, why you just don't output it with an echo '<img src="'.$avatarLoc.'" />'; tag? –  aSeptik Jun 17 '13 at 20:22
show 1 more comment

4 Answers

You're forcing a PNG. Are you sure it's a png always? Try commenting out the header calls and see if it works then.

share|improve this answer
    
The content type must be set. Output is sent as text/html by default, so sending binary data without an appropriate content type will cause the content to appear corrupt. –  George Cummins Jun 17 '13 at 20:04
    
The Tumblr API always serves a PNG. I did try commenting it out, to check your suggestion, but unfortunately it still occured. Any ideas? –  TbWill4321 Jun 17 '13 at 20:04
add comment

I wish I could just add a comment instead of an answer, since this isn't a solution, but my rep isn't high enough. Anyway, here are some things to try for debugging purposes.

  1. There's a lot going on on this page. Trying making a separate page that only loads a series of images via serve.php and see if you have the same problem.

  2. You're saving these files locally. Look at the files you've saved on the server and see if they all contain the real image, or if some of them are blank or just won't render.

  3. If the files are being saved into a directory that's accessible from the Web, try making the script just redirect directly to the image. You obviously don't want to go live this way, but it might help you find out if it's an issue with the PHP script reading and serving the image files.

Wish I could be more help.

share|improve this answer
add comment

At first I had the problem you described, but after a while it went away and now all the photos load. Maybe clear your cache?

share|improve this answer
    
I managed to find a complete workaround. I never did manage to get it fixed, so now the page itself checks for the file first before calling the save/serve. I'll close out the question. –  TbWill4321 Jun 18 '13 at 16:39
add comment

Fixed the issue by simply not calling the save / serve script so often, so at this point it's rare enough to be a non issue.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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