PHP, SEO, web design

PHP programming, web design, SEO and other web development topics.

Saturday, October 10, 2009

Prevent specific sites from hotlinking using .htaccess

When I first created AvatarsDB site, my idea was to allow people to simply link to individual avatar images without need to search for image hosts or download images to their computers. However, after some time there were people who started to abuse this service. I had cases when people built entire sites and blogs just by linking to thousands of images from my site. No need to mention that this wasted gigabytes of my bandwidth and sent hundreds of thousands of useless request to my server without providing even single link back. I wanted some solution which will allow legitimate users to use this service and block bandwidth leeching.

Solution is very simple and it involves editing your .htaccess file and creating replacement image which you will show to visitors of offending site. This can also bring some traffic to you if you put name or address of your site. Simply put this code in directory where you keep your images or in root if you wish to prevent hotlinking in all directories:


RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?evilhotlinker\.net/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?leecher\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?annoyance\.net/ [NC]
RewriteRule .*\.(jpg|gif|bmp|png)$ images/nohotlink.gif [L]


You can put as many sites in this list as you like, just make sure lines before last one on the list end with [NC,OR] which tells Apache to continue matching. If you want to save bandwidth, you can also send error 403 Forbidden by editing last line to this:


RewriteRule .*\.(jpg|gif|bmp|png)$ - [F]


You can also block all hotlinking and allow only your site:


RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://yourdomain.com.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yourdomain.com.*$ [NC]
RewriteRule .*\.(jpg|gif|bmp|png)$ images/nohotlink.gif [L]


You should put all these codes before you do any URL rewriting or they might not work. When you're done editing, make sure you upload it in text mode or it also may not work.

Sunday, September 13, 2009

Calculate image aspect ratio in PHP

Calculating image aspect ratio in PHP is very simple. First you need to find greatest common divisor(GCD). You can do that by using following function:

function GCD($a, $b) {
while (
$b != 0)
$remainder = $a % $b;
$a = $b;
$b = $remainder;
}
return
abs ($a);
}


If you compiled your PHP version with GMP functions, you can also use following:

$gcd = gmp_gcd($a, $b);

After finding GCD, simply divide both sides with it:

$a = 2048; // screen width
$b = 1152; // screen height
$gcd = GCD($a, $b);
$a = $a/$gcd;
$b = $b/$gcd;
$ratio = $a . ":" . $b;

WallpapersDB - another DB project


After more than three and a half years of no new projects, finally next site in my planned DB series - WallpapersDB - Desktop Wallpapers is ready to meet the world. As its name may suggest, this one is meant to help you in your efforts to beautify your computer screens.

Like with my previous avatars site(actually like with all sites I built in last years), this one is built around PHP/MySQL technology. I used my new favourite two column CSS layout with 310 pixels wide right column which can hold medium rectangle ad and plenty of info. For thumbnail generation and other image manipulation I used powerful Imagemagick. I find it easier to use and results are very good. All image manipulations are cached on server to speed up whole process.

Like with all sites these days; you can register, comment, upload, share and do all kind of neat stuff new web offers. You can browse wallpapers by category or native resolution. Thumbnails are big and when you choose wallpaper, script will generate image optimized for your screen size.

Thursday, May 04, 2006

AvatarsDB - my latest project


I spent last few weeks working on my latest project - Avatars Database or shorter AvatarsDB. As you probably guessed correctly, purpose of this site is distribute highest quality avatars optimized for use as forum signatures, in instant messengers (as buddy icons) and even on web sites.

When I started with development, my main goal was to create a web site with very simple design and fast executing scripts. Of course, I decided to have PHP and MySQL at backend since these technologies have proven to be fastest and easiest to maintain (at least in my experience). I created a custom CMS for this project with very simple, but powerful, admin interface which allows me to create and edit new categories, edit category descriptions (images without text is not very good SEO) and batch upload new content.

Since this project uses single PHP page to generate all content pages, it would be very difficult for me to rank in search engines (yea, imagine thousands of crappy pages that look like index.php?id=234&cat_id=33). To solve this issue, I decided to use Apache mod_rewrite module to rewrite all URL which now look like this http://www.avatarsdb.com/art/hr-giger/ (very neat, don't you think?). No need to mention that every page has it's own title, heading text, description text and keywords.

In the next few days, I'm planning to add several more features like internal counter, which will count number of visits to each particular avatar (this data will be used to generate most popular avatars list), editor picks, and color picker which will allow my visitors to test how their selected image looks against different backgrounds. I'm open for suggestions, so if you have some time, I would like to hear your opinion.

Site is hosted on Dreamhost shared hosting. They offer incredible 1000 GB of bandwidth use per month, which allows me to expand my business and offer a few services to my visitors like remotely hosted avatars.

Friday, April 28, 2006

Babel Fish on Yahoo!


The oldest and the most popular online translation tool "Babel Fish" is now active on Yahoo! (http://babelfish.yahoo.com/). It allows you to translate across 38 different language pairs such as English -> Chinese, Korean->English, English -> Russian and much more. Main difference between Yahoo! and AltaVista version of this tool is two additional languages (Simplified Chinese and Traditional Chinese) and integration with other Yahoo! services (Search and Toolbar). Make sure you give it a try.