« October 2006 | Main | December 2006 »

Creating Image Thumbnails with PHP

Today I have been working on my butterfly tattoos website. There is a lot more to come on that website, I have ideas for Flickr integrations and more. At the moment I am getting c. 500 unique users a day passing through the site and I have made 0 changes since 2004. This site needs to be brought up to date. One way I am looking to do this is through enabling users to simply upload their own photographs (this will help me too since I have about 50 tattoos sitting ready to be uploaded). A key part of this is to do all the image handling on the server with pre-existing PHP functions. While working on this idea I found the following code source "create square image thumbnails with PHP".

This code uses the simple but very powerful GD graphics utilities that come with PHP 4 and 5. I have used them a lot and I won't reproduce his code here however I will show my use of the code. I create a function which I store in a seperate include and can be called any time. The function will return "none" if there has been no error or will return an error like "I don't support images of the type image/bmp support png, jpg and gif try converting to that and uploading" if an image format I don't support is sent.

function createThumb($source,$dest,$mimetype)
{
    $thumb_size = 150;
    $size = getimagesize($source);
    $width = $size[0];
    $height = $size[1];
    if($width> $height)
        {
        $x = ceil(($width - $height) / 2 );
        $width = $height;
        }
    elseif($height> $width)
        {
        $y = ceil(($height - $width) / 2);
        $height = $width;
       }
    if ($mimetype == "image/jpeg" || $mimetype == "image/jpg")
        {
        // set up the image (note imagecreatetruecolor doesn't work for gif)
        $new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
        // what is the input format, create from that
        $im = imagecreatefromjpeg($source);
        imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
        // the 100 is the quality here, this is quite high
        imagejpeg($new_im,$dest,100);

        $error = "none

       return $error;

        }

   elseif ($mimetype == "image/png")
        {
        // set up the image (note imagecreatetruecolor doesn't work for gif)
        $new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
        // what is the input format, create from that
        $im = imagecreatefrompng($source);
        imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
        imagepng($new_im,$dest);

        $error = "none

       return $error;

        }

     elseif ($mimetype == "image/gif")
        {
        // set up the image (note imagecreatetruecolor doesn't work for gif)
        $new_im = ImageCreate($thumb_size,$thumb_size);
        // what is the input format, create from that
        $im = imagecreatefromgif($source);
        imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
        imagegif($new_im,$dest);
        $error = "none

       return $error;

        }

   else

        {

        $error = "I don't support images of the type $mimetype but I do support png, jpg and gif try converting to that and uploading";

       return $error;

        };
}

The best bit of this code in my opinion is the ability to crop the image to a square while maintaining aspect ratio. This is managed by the imagecopyresampled() php function.

The Power of Focussing on Something

Today's post is something that is very interesting to me. How you can acheive results by just shifting your focus. I have 3 main websites: cocktail making, paper airplanes and butterfly tattoos. A great set of comments on this can be found in the awesome book good to great in which particularly the discussion on Chrysler Acquiring Gulfstream is particularly enlightening. If you deviate from your core, if you look somewhere else than 100% at what is most important to you it will not succeed as well as when you have 100% focus on your most important project.

For me an example of this can be seen in the image below which is the % year on year growth rates for my website traffic and website revenue (I have removed actual amounts) and this graph is mid way through Nov. hence Nov 2006 not looking so great.

When I looked at this graph I was shocked (I first looked at it in March 2006) to see my revenue growth year on year had gone negative in Nov and Dec 2005 (actually first time in 10yrs of running my own websites). This corresponded with me planning and executing emigrating to the US in December 2006. After settling into the US things clearly recovered until March 2006. This was a huge month for me with my first major international summit (I was jointly responsible for running), the first class I taught at our marketing college (on internet marketing... it was awesome ;) ) and my parents visiting the US. My site growth then decelerated as did my revenue growth until the second summit of the year happened in July 2006. I took the second half of July 2006 off to enjoy myself and work on things I enjoyed. Since that date I have worked on my personal projects at home and not taken much work home with me. I have focussed at work more on 2 key projects which really matter to me (and I can't talk about here) and my site revenue growth (see blue bar above), my site traffic growth (see red bar) and my projects at work are going really well and even reaccelerating.

I did a lot of things to produce this reacceleration (I launched a cocktail widget, I totally recoded my cocktail site, I added videos to my paper airplanes site and more) but the key point is the focus and this is what I want to say. When you can contribute significant focus to something it will go better than with partial focus. I am now thinking about that a lot as I work and play.

Cocktail Recipe Spelling Correlated to Cocktail Strength

So today I was browsing around a few of the sites that are hosting my cocktail of the day widget and had a few awesome anecdotes but first I want to point to the traffic light cocktail on my website because wow is it a favourite. This is a long juicy cocktail with plenty of kick and extremely sweet. I would definitely suggest you give it a go.

So today I was browsing around the sites showing the widget and came across an article about how the quality of spelling on my cocktails was strongly correlated to the strength of the cocktail! I sadly can't find the article (if anyone can point it out to me I will link to it). Either way from now on when you see the cocktails on my site or the widget around the web, look at the recipe and think about the hours of research that went into finding you that perfect recipe ;)

PHP mySQL close connection

So I have been creating lots of new features for me to maintain my cocktail making website. One feature has involved me setting up a new mySQL 5.0 database (with cool stored procedures I am only starting to understand now) in order to do some tracking of what is happening on the site and with my widget. This has required me to use two databases at once for the first time. One to check if a user is signed in with admin rights and the other to check the tracking out of the other database (php security is really tricky so I am slightly nervous posting even this here :s ).

However I did want to note how you close a connection with a mysql database using PHP in order to open one with another mysql database since it's really simple but I found it hard to get info. Here is an example (with $hostname, $username, $password and $databasename containing what they are named):

<?php$link = mysql_pconnect("$hosturl", "$username", "$password")

               or die("Could not connect: " . mysql_error());

$db = mysql_select_db($databasename,$link)

               or die ("Couldn't select database");

mysql_close($link);

?>

So when you open databases try closing them, I know I will in future for good practice, it makes everything much simpler if you add another database at a later stage.

Graphing Search Result Numbers Over Time

So I have had a random idea for a little project that could be fun. We all know about Google Trends, eBay Pulse, the Overture Search Suggestion tool. Now I think all of these are truly amazing tools. Something we don't neccessarily know about is the supply side of this equation. All of a sudden that is really interesting me. It would be relatively simple to set up a CRON job that runs once daily and pulls the abundance through the various API's available. It could also be really fun to see how trends shift over time (perhaps looping in Technorati and a couple of other sources). It could even be a fun tool for an eBay seller to help me monetize it a little bit... hmm I think I may build this at the weekend and if I do I will post the resulting graphs on this blog as well as on a little site I recently purchased with 1 and 1. YAY!

Fun Blogs using the Cocktail Widget

It has been quite an amazing experience delving into the world of widgets. One which is costing me money in bandwidth charges and my own time and (as yet) I see no real way to make money out of. Money is usually a key driver for me to expand my new ventures. Yet I still want to proceed further with creating the widget and getting people to use it. Behind the scenes I have created a little dashboard to allow me to manage most of the functionality around the cocktail making site and to the right is a screen dump of the widget dashboard midway through Sunday Nov 12th in the US (although all days and times are UK).

At the bottom of the page you can see a section entitled "Top Domains Calling this Widget". I now tend to take a look daily at the blogs hosting my widget since (a) I want to make sure it's working right for everyone (b) I am excited that people would want to feature my stuff. For this blog post I wanted to feature a couple of the fun blogs.

The first one I want to feature is by a nice lady called Amy Ferguson who has named her blog "Chef Amy Ferguson's Travel Blog". She is evidently a chef from Texas who is heading back for a holiday in Paris (where she studied 31yrs ago) and telling us all about it. I love the passion in the blog and hope she keeps it up, I will be reading.

The next one I am posting since it's a fun one for me as a bloke. The blog is called "H's Doghouse" which is a fun name and best of all the strap line to the blog says "H's Doghouse provides the blogger with the most important information in the universe. And if you don't think so......too damn bad". There's a cool post at the moment about his favourite beers although bluntly he has never tasted Old Peculiar from the theakston's brewery or abbott ale from green king if he thinks that :). Have a read and I am sure you will enjoy the random musings. Also note my widget in prime position (top left hand corner), I was pretty chuffed when I saw the widget there and I'll probably be pointing people at this blog just to see it :)

Well that's enough for now but I suspect I will be reading enough random blogs thanks to the widget that I will be posting a fair few on here.

embedding videos on your website with revver

I like embedding videos on my paper airplanes site. I have from time to time used revver and used you tube for these videos and both have their pros and cons. Here is the step by step guide to revver embedding which I personally prefer.

The first thing you do is visit Revver.com and sign up to their service including payment details for having money sent to your paypal account. Once you have done this click on the UPLOAD video link on the left hand navigation panel. That will take you to the page below.

The great thing here that beats YouTube is that you can upload multiple videos at once (in this example I am uploading four planes). The uploader will then scroll through them in it's own time uploading them to Revver. This is great, since you can leave the computer, go to a different browser session and not have to keep going back in order to start the next upload as you do with YouTube. Once you have uploaded all the videos your "my video's section" will have a link at the bottom asking you to add meta data. This is great since you can go away and then reaccess your revver account from another computer (eg. at starbucks on wifi) to add your meta data. Below is an example of the Meta data for my Rapier Paper Airplane (this is actually editting the meta data not adding it first time around but the process of adding it is very similar).

Once this meta data is in place the Revver team will review the meta data and the video and approve it if they consider it ok. You receive messages in your my video hub page to let you know it has been approved. Below is the "my video's" hub page for me - a week after I started with Revver. As you can see they are really easy to browse.

If you then click on one of the videos revver then opens up the video you wish to view within the my videos section. Here I am viewing Origami Plane III. To the right of the III the icons in order mean "edit" "grab this video" "delete this video".

If you click on the green grab this video icon to the right of origami plane III you come to the page below where you get the code to insert into your HTML.

 

I find this process really easy, really intuitive and actually in a lot of ways a great deal better than YouTube where I have also hosted my videos (170k video views in a little over a month). The bonus of getting paid for the video views too is amazing and I only hope this site gets the support it needs to be huge. Stephen Colbert's show on the YouTube rip off is my favourite, I do believe my content is valuable and I should be paid for creating it. I like, no I love, the way Revver doesn't treat me as free fuel.

Dennis rocks

and so does his blog... if you aren't reading it yet you definitely should start too. Dennis Goedegebuure

Ideas for future blogs

So this is more a post for me to remind myself what I have to talk about and what I should be posting about, I get a few hundred visitors through here a week so if you would like to comment on what you want to hear about that would be cool, if not I will try to get to writing some interesting blog posts on these over the next few months.

Self publishing - I have written a book, 150k copies were sold at 10GBP a pop... 1,500,000GBP of revenue for marks and spencers and I made 3000GBP. I can write about this, how to avoid falling in the trap yourself and about potentially taking a foray into self publishing.

User generated content - have been running a website built on user generated content for 4 yrs. What are the pitfalls what are the benefits, what have I learnt... I should share that here.

Mini Turing tests - looking into this a lot lately and have thought about it hard I could write blog posts about this

PHP coding for uploading images - looking into this for butterfly tattoos why not share the knowledge?

Recording videos for the web - I run my paper airplanes site now on videos (huge numbers of my users watch them) and have gained some experience about this. Posts on it may well be a good thing to put online

The trends in online advertising - having been making money off the web in largish amounts now since 2000 (before that was tiny) some of those learnings may be really interesting for visitors... posts in this area would be cool

Cocktail making seasonality - what are the most popular ingredients searched for by month, what are the most popular months of the year, what cocktails are most popular by month of year/day of week/week of year. I run a cocktail site I have this data from 100ks visitors... might be fun to share. This could make a great PR article to drive traffic to the site.

Bubble 2.0 - I made a little money off bubble 1.0 and am now making more off bubble 2.0 this is definitely a potential story....

Database structure - I have made sooooooooooooooooo many mistakes in building databases that are just not queryable in a scalable way to do what I want. Perhaps I could share that... stories about summarizing data, caching data, structuring data, creating sensible time columns... etc... etc...

Ah well those are all ideas. If only I could find time to post them. The life of a small webmaster eh! ;)

How to use the Typepad Widget API with PHP

The Typepad Widget API is awesome. The concept is that you have a great widget you want users to add to their sidebar. Typepad want this to be a small self contained piece of DHTML code and want to be able to do all the verification of the user on their side without giving the widget owner any access to the user's account.

Browser based authentication used at both the eBay and Yahoo! developer programs (and explained beautifully in diagram form on the Yahoo! site) simply won't work for this since it gives the programmer access to your eBay/Yahoo/Typepad account (were they to use it). So Typepad found a solution to the problem that is really simple and elegant.

All data is sent to the API in a POST command along with redirecting the user themselves (rather than just a server to server POST call). Typepad's Widget API requires fields identifying you as a developer, your application, what it is called, verifying you are who you say you are (with a token) and finally containing the FULL HTML you want inserted in the user's sidebar to be sent to them through the POST command.

The user then lands at a Typepad log in page and all the data is carried through with them to a page where they can effectively suggest whether or not to keep the Typepad widget. GREAT!

I love this method, it's secure, clever, simple and behaves exactly as you would expect... Having worked with Google AdWords/Base/Adsense APIs, Yahoo! APIs, eBay APIs and a fair few others I have to say this Typepad Widget API has been the simplest yet. Well done Typepad!

My Photo
Blog powered by TypePad

my flickr tag cloud