Storing Images with PHP & Mysql – The right way

April 22, 2010

I’ve in that stage of my latest project that I have to decide how to store images to the server, and how to use the database the best way to do this. I know that I’ve done this wrong a couple of times, but hopefully I’ve find a good practice of this now. Not quite sure if this is the best way, but this is how I think after looking into the matter:

There are several things I consider to be important:
1. SEO.
The filenames need to be descriptive and searchable.
2. Database
Images should not be stored in the database, but the filename and id should. You need to know what image goes with what movie. It is possible to store images into database, but it makes the database big and I can image working with the images becomes little more complicated. There are advantages to this too, but it is not the way I’m going to do it.
3. Thumbnails
It’s a good practice not to resize in the <img> tag, so a good system for thumbnails must be at hand.
4. URI
Where to put the files is also something you have to consider carefully. Is it best to put everything in one folder? Probably not. What about “movie-id/images/image.jpg” or “images/movie-id/image.jpg”? Much better.

Let’s take an example database with four tables: Movie, ImageType, MovieImage and Image

Movie:
Important part of movie is that it must have an id and a title.
ImageType:
Must have an id and a type, ex: cover, poster and screenshot
MovieImage:
Realtionship between the movie, image and type. Contains the foreign key to image, movie and imagetype, where one of them has to be unique.
Image:
The image should have an id to the image so that it can be connected to a movie and made unique. In addition it should contain a filename, that is generated in php from the id, title of movie and type of image.

Cooking it all together:
The filename must be SEO optimized, so the syntax to use should be like: id-type-movie-name.jpg. You could remove the type from the name, but it’s a good way to distinguish between the different image types, making the SEO better too. Movie name should be separated by a hyphen to be most SEO friendly. By storing filename once with id instead of generating it every time and out from id, type and title is good practice because if the title is changed (from “Lord of the rings” to the more accurate “The Lord of the Rings: Fellowship of the Ring”) the filename points to the right file – id-type-lord-of-the-rings.jpg. Generating thumbs could then follow a standard of adding a -thumb-width-height or something to the name – or simply create it on the fly with a php method, even thou I think the first option is more correct. When getting all images to a movie you can easily sort by type, and get the filename from database – and then show it on your webpage by creating the correct url, ex: images/movie-id/1923-cover-lord-of-the-rings.jpg.

By thinking like this, the image uploading and storing become much more simplified for me than how I’ve done it before.

Advertisements

6 tips for better SEO & traffic when starting a website

April 16, 2010

I’m not an expert in the field, but I have noticed some factors that play a big part when starting to build your site. Some of this is something I’ve probably read or heard about, but most are own experience and I’ve not used similar articles as reference, so these are points I came up with on the fly when I started writing this post.

1. Time your posts right!
Depending on your audience, you should time new entries at times when people are online and can pick up your entry on feeds or twitter. If you know your target group is English-speaking and mostly from USA, then don’t add a post after midnight, because when traffic is peaking, your post is long gone.

2. Follow news & trends
When you post a news story, be one of the first with a proper story. This can make you the sources to others blog posts and even the one post that everyone retweet. This goes before the timing of the post, because news need to be fresh – and news are always googled by people who are interested in views and more information. Old news is never very good news. Be aware of what is trending – and include it in your writing and site.

3. Google ad words campaign
A great way to notify Google on the content of your site is to run an ad words campaign if you have $100-$1000 USD to spare. This is mostly in the beginning, to get targeted readers and audience, and to kick of Google attention to your site. I ran a 3 days campaign (with a free coupon I got from Google) on my small site and unique visitors a day rose with about 100%. It was not a very big site, so the traffic was not very big to begin with, but the effect was noticeable and stayed that way.

4. Begin ready
Have content ready to be published for a good time when you start. Don’t start twittering when site is up, start long time before to build up your followers, and then tweet about your site when it’s up. This way you are sure to get more traffic than if not. Have everything – every static page with carefully chosen words to get proper SEO. This includes unique header tags, titles on pages, about us page etc. Have some content pre posted on the site when start. If a blog, post a handful of good articles from the start (and keep them coming). This is, when Google and other search engines pick your site up they pick up all keywords and material thoroughly in first try. If you edit later you probably have to wait a while for Google to change the information on the static content, because Google are most interested in new content.

5. Advertise your site freely
Get out there. Be ready with Twitter, Digg, and other social media. Use them. Use forums, have link to your site in your footer. Post comments on relevant blogs and sites so that link to your site comes up. Tip relevant blog masters or news sites of your site, if they like, they may recommend. Link to articles and news that have a pingback function to get free advertisement. Advertise your site as a great site, not a “new site”.

6. Know how social media works and use them properly!
Tag your site correctly on delicious and more people will do the same by your suggested tags. Tag your site right in StumbleUpon and you get stumbled upon by the right audience.


Debug PHP: print_r() the array

April 2, 2010

I’ve used print_r method in PHP to print the contents of an array or object for a long time. I know there are some debugging tools out there, but I like it real simple, and print_r is really all I have needed.

Well, the mistake I’ve done is that I write this snippet all over my code:
echo '<pre>';
print_r($array);
echo '</pre>';

The work in removing all them or commenting out is something I sometimes forget. That is why I made a new kind of method to make it easier.
function preWrap($array){
echo '<pre>';
print_r($array);
echo '</pre>';
}

I don’t know if this is a really good way of doing it, but it helps. To do it so that removing the preWrap() call, or commenting it out, is not always needed, you can add this to it:

In top of index.php or config.php file:
$debug=true;//change to false to turn of debug mode

And then in the preWrap method add this:
if($debug){
echo '<pre>';
print_r($array);
echo '</pre>';
}

That’s my simple way of doing it.