David Hurth's blog

Making a Server Side Image Cache in PHP

Tagged:  

Yesterday, I wrote about an application that I wrote called scovry and I mentioned that in writing the application I used a lot of caching. This included caching of images from an external thumbnail service server.

I thought that I would post the code that I am using to do this and quickly go over how it works. This code might be useful to any application that loads images from another server, to lessen the dependency on the other server. Below is the code that caches the images.

<?php
$imageurl =urldecode( $_GET['imageurl']);

$cachefile = 'caching_folder/'.urlencode($imageurl).'.jpg';
$cachetime = 604800;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
    header("Content-type: image/jpeg");
    readfile($cachefile);
    exit;
}
ob_start(); // Start the output buffer

$imginfo = getimagesize($imageurl);
header("Content-type: image/jpeg");
readfile($remoteImage);

// Cache the output to a file
$fp = fopen($cachefile, 'wb');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush(); // Send the output to the browser
?>

The above code is used like an image proxy, but it adds the benefit of doing the image caching. So, the first thing to notice is that in my application the imageurl variable that is passed in through the url query string is an encoded url, so we decode it. This variable will hold the url encoded url of the image that we want to load. You may also notice that this code assumes that the image is a JPEG image, because that is what I was always going to be dealing with. So, if you use this code, you may need to add code to handle other image types.

The next import variable is the $cachetime as it sets how long (in seconds) the image will be cached before the cache is refreshed. The if statement checks if it should load the image on disk. The important part here is that it sets the document header content type to be an image and then loads the image using the readfile() method.

Now If the file does not exist or if it needs to be re-cached it loads the file from the server and writes it to the output buffer. Once again the document header content type is set to an image value and it gets the file via the readfile() method. In addition to this it writes the file to disk using the fopen() and fwrite() methods.

That is basically all there is to it. You can take a look at it in action by checking out scovry.

What I Did on My Winter Holiday

This winter break (or as we used to call it when I was still in school, Christmas vacation), I worked on a little web application. The application was going to be for an automotive related blog that I write on, but once I started working on it I realized that the application was better if it wasn't restricted to just automotive information.

The application is called scovry and it is a web discovery application. You may notice that we at Ajaxonomy love spy applications (notice that in Ajaxonomy labs we wrote delicious Spy and TubeSpy). scovry is kind of a blend of these spy applications along with gathering information from many other social media sites and social networks. Beyond this the site adds a social element by allowing comments and easy sharing of items found on the site.

scovry

From a programming side, in order to load all of the data and keep the server happy I used a lot of caching. The caching code I used was based on the Easy Server Side Caching in PHP article that I wrote a while back. With some small changes, the biggest of which was using readfile() instead of include(). I even modified the caching script to cache images that are loaded from the thumbnail service to make loading much faster (not to mention to reduce requests for images).

You can check out scovry here.

Getting a Google+ Invite

Tagged:  

Right now It seems that everyone that is not on Google+ wants to be on Google+. I personally just received an invite and have been playing with the service this morning.

So far I have found Google+ to be a good social network, but I'm not sure yet if it will be a Facebook or Twitter killer. I think it might be incredible, but as we all know the most important thing in a social network is the size of the network. So, we won't really know how well Google+ will do until it is released to the entire public.

In the mean time everyone wants to be in the fairly exclusive club of people that have received invites. If you happen to be in the tech industry then you probably know someone that has an account. The key to getting an invite (outside of trying one of the many hacks that has been published) is simply to ask all of your friends. The chances are that after you check with all of your friends that someone will at least know someone that has an account and can be your ticket to an invite. So, before you can get on this social network (at least in it's early form), you will need to be social and network.

Generating Email with View Engines

Tagged:  

Recently in working on a C# MVC project I had an issue with how outgoing e-mails where formed. The e-mails where all formed through a large string that contained all of the formatting XHTML. This began to become very messy and very hard to deal with. Since the project was a MVC project I thought it would be cool if I could just use a View for the e-mail template.

Well, over at Channel 9 Andrew Davey has a good presentation about solving this very problem. Below you can watch the presentation.

jQuery Dialog Box Tutorial

Tagged:  

Recently on a coding project for my day job I had a requirement to display a dialog box to confirm that when someone clicked a cancel link that they meant to do the cancel action. The dialog box was on a page that built a list of links with multiple cancel links, each with a different url. Since we are using jQuery and since I didn't want a browser dialog box to display I decided to use the jQuery dialog box.

Since I am a firm believer in the first rule of programming (never re-write something that has already been written) I did a quick Google search and found a good example of the jQuery dialog box for when a form is submitted at jensbits.com. The code was close to what I wanted to do, but not exactly. So, I took the code and modified it to work for me.

Below is the portion of the jQuery code that I changed from the code in the jensbits.com post.


$(function(){
// jQuery UI Dialog

$('#dialog').dialog({
autoOpen: false,
width: 400,
modal: true,
resizable: false,
buttons: {
"Yes": function() {
$(this).dialog("close");
$(location).attr('href',$(this).dialog('option', 'anchor'));
return true;
},
"No": function() {
$(this).dialog("close");
return false;
}
}
});

$('.closebutton').click(function(){
$('#dialog').dialog('option', 'anchor', $(this).attr('href'));
$('#dialog').dialog('open');
return false;
});
});

My first modification was to have the dialog open when someone clicks on a link that has a closebutton class assigned to it. I also attached an anchor option to the dialog box that holds the href attribute value of the link.


$('.closebutton').click(function(){
$('#dialog').dialog('option', 'anchor', $(this).attr('href'));
$('#dialog').dialog('open');
return false;
});

The below code shows the setup of the buttons and the major change is that the "Yes" button sets the location attribute to the anchor option value that we set in the code above.


buttons: {
"Yes": function() {
$(this).dialog("close");
$(location).attr('href',$(this).dialog('option', 'anchor'));
return true;
},
"No": function() {
$(this).dialog("close");
return false;
}
}

Below is the code in the body of the html that sets up the links and the dialog box. Notice that each link has the class set to closebutton.


<a id="closebutton1" href="http://ocell.us" class="closebutton">Close</a><br />
<a id="closebutton2" href="http://wastingtimegames.com" class="closebutton">Close</a><br />
<a id="closebutton3" href="http://theporscheguys.com" class="closebutton">Close</a>
<div id="dialog" title="Cancel"><p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 0 0;"></span> Are you sure you want to do that?</p><p>
If you are sure, click Yes.</p><p>If not click No.<p></div>

You can see the full demo code here. It is a text file for easy code viewing, but you can save it to your computer if you want to see it in action.

Is PHP Good Enough for Your Startup?

Tagged:  

When it comes to web development people are in quite a few camps when it comes to web development languages. For the most part the major camps right now are .NET, Java, Ruby, Python and PHP. For many PHP is the lowest web development language on the list and some developers are somewhat embarrassed to admit that they program in it (many think it is a language for hacks).

I personally think that if PHP is done properly, it is a very good viable web development language. The question is if you think that PHP is good enough for your startup. Would you be embarrassed when others ask what your site or application is written in, or would you proudly proclaim that you used PHP?

The startup ChartBoost that deals in on-line advertising has written a good post about this subject. They use PHP and it is interesting to read their thoughts.

Below is an excerpt from the post.

Running a Modern Startup on PHP

In the modern world of agile startups and silicon valley, the buzz is all about Ruby, Python, and whatever the latest cool programming language or framework to come out is. Older technologies don’t get much love, and PHP especially has a bad reputation. In this post, I’m gonna go over why and how we use PHP as a modern technology, and the various other tools, techniques and development methodologies we employ to run as agilely and elegantly.

You can read the full post here.

So, what web development language is good enough for your startup?

JSON 3D - The Time is Near!

3D on the web is getting very close to being a reality. With the release of Google Chrome 9 and Firefox 4, that uses WebGL (a subset of the OpenGL library) coupled with HTML 5 and JavaScript, 3D on the web could soon become used in many websites.

With 3D likely becoming mainstream on the web within the next few years, I thought that now is the time to build tools to model 3D models using a small easy to handle format that could be easily used in JavaScript.

You may remember my post from a while back about JSON 3D. I feel that using JSON for 3D modelling would be the best format to handle 3D models on the web. This would allow for quick Ajax calls to allow loading 3D models as needed on a page and the format is about as small as you can get on the web for holding this type of data. Additionally JSON already has a huge footprint in Ajax development and has virtually replaced XML in being used for most Ajax calls.

You can read my original post on JSON 3D here. My hope is that some of you will find this useful and will begin to write the tools that will be used to have 3D available all over the web.

Also, if you are looking to have some fun, you can view a bunch of Chrome WebGL 3D demos here.

PR Hack Writes The Most Amazing Press Release Ever Featuring an Actual Product or Service

Tagged:  

About a week ago there was a very funny press release that appeared on TechCrunch. I thought the idea was great, but I noticed that the press release didn't actually mention any product. Because of this, I thought I would try to write my own Press Release that actually mentioned a product or service and a big feature release. The service is calledocell.us (I wrote about the service that I worked on a couple of posts ago) and the new feature is the ability to have Twitter tweets that contain links automatically bookmarked on ocell.us (with no need to login to ocell.us).

Below you can read the full press release.

PR Hack Writes The Most Amazing Press Release Ever Featuring an Actual Product or Service

PR Hack writes the most amazing Press Release that includes a Product Ever, according to David Hurth. First he grabs you with the subject and then throws in a great quote about a product.

ocell.us is the best social bookmarking site in the universe”, according to creator and PR Hack David Hurth.

To cement this Press Releases place in history David adds the below quote about an actual product!

“ocell.us is so awesome that it even allows you to bookmark your links from Twitter, so you don’t even have to visit the site!” David continues his quote with the following, “we don’t care about traffic, we just want to store your links for when you need them later, how awesome are we now!”

Not only is this the most amazing Press Release that includes a product ever (History has already recorded that Mitch Delaplane wrote the The Most Amazing Press Release Ever Written, not containing a Product or Service), but it should end now before the world explodes from the awesomeness of this Press Release!

If you you do not agree that this Press Release is “The Most Amazing Press Release Ever Featuring an Actual Product or Service”, then write your own. Otherwise start using ocell.us and you too can be this awesome!

You can read the original post here.

Let me know what you think of the press release and if you think this is the way that press releases should be.

Facebook is NOT Shutting Down

Tagged:  

You may have heard reports that Facebook is going to close down on March 15th. In case you where wondering, Facebook has confirmed that they are not closing down the site. The site that originally reported this is one of those sites that reports things like the President surrenders to aliens from Mars.

The most interesting thing about this is how fast fake news can spread with social media. I guess this is the worst part of social media.

Are Delicious and Digg both Dying?

Tagged:  

A few years ago it would have been hard to believe that both Digg and Delicious (a.k.a. del.icio.us) could ever be on life support. However, that appears to be what may be happening. It looks like both may be dying, but it is happening in much different ways.

In my last post I reported that Yahoo! was killing Delicious. Yahoo! has since released a statement that says that the site will instead be sold off, which still puts it's future in jeopardy. Once someone buys Delicious we will have to see what changes happen to the service. The main mistake that I feel that Yahoo! made with Delicious, is never putting advertising on the site. With a social network advertising is one of the few proven ways to make money. Since Yahoo! is having so many financial problems they are needing to get rid of anything that doesn't make a profit.

With the uncertain future of Delicious, I have exported all of my bookmarks and have imported the to a new service that is looking to take over for Delicious (more about the service later in this post).

With Digg the issue is much different. The problem that they are having is one of many people leaving the site due to the latest version of the site. The latest version is vastly different from how Digg started out. In fact many of the changes are more likely to help SEO and spammer types than those who are engaged in the social network. Because of this many users have started using other social networks and one in particular seems to be making a big splash (more on the service in a little bit).

This pivotal time for the two services has opened the door for a few services that seek to gain users from the networks as their ships may be sinking. Two of these services are Old Dogg, which is an old school Digg like site and ocell.us, which is a Delicious site that is much like the original del.icio.us.

With Old Dogg, many previous Digg users have joined the network and the site is thriving. The site seems to have more of the early Digg feel that made users fall in love with Digg in the first place. I personally like the site and think it has the potential to be the final nail in Diggs coffin.

With ocell.us, you have a simple site that gets to the heart of social bookmarking. The site is very new, but appears to be gaining quite a bit of traction with users looking for a site that promises to be around for a long time. In fact the about page states that the site was started because of the "death" of del.icio.us and guarantees to keep the site running for a very long time.

Below is what the site says about why it was started.

ocell.us is a social bookmarking site started in the wake of the death of del.icio.us. The site allows you to import all of you del.icio.us bookmarks and of course to store new bookmarks. The main reason that the site was started was to give a site that would allow for social bookmarking, without the fear that it would ever be taken down (we have a very low overhead and are able to afford to keep running for a very long time).

So, only time will tell if Digg and Delicious are able to stay alive and on top. Otherwise Old Dogg and ocell.us may do to Digg and Delicious what Facebook did to MySpace.

-Important note: I am creator of the ocell.us site, so my be a bit conflicted in my interest towards the site.

Syndicate content