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.
readfile()
function, and knows a work around. – TbWill4321 Jun 17 '13 at 20:09readfile()
withfile_get_contents()
and echoing the result, however there was no change. – TbWill4321 Jun 17 '13 at 20:12