Monday, February 28, 2011

Simple home made captcha

Hi,

In this post I will give a very basic sample of creating your own captcha, with font, shape and size of your own choice. To achieve this we need the GD library to be enabled for you apache server. In this you need to create two php files and use a ttf file.

First we create the index.php file as follows:



<?php
session_start();
if(isset($_POST['captcha']))
{
if($_POST['captcha']==$_SESSION['captcha_code']){
echo "<font color='green'>Captcha code matched</font>";
}else{
echo "<font color='red'>Incorrect captcha code</font>";
}
echo "<br/>";
}

?>
<form action="" method="post">
<img id="captcha" src="captcha.php" /><br/>
<a href="javascript: void(0)" onclick="document.getElementById('captcha').src='captcha.php'">choose a different word</a>
<br/>
<input type="text" name="captcha" />
<input type="submit" value="Submit">
</form>


Now the second file you need is the captcha.php, which is as follows:

Thursday, February 24, 2011

Save image from a image URL

Hi,

Yet again a GD library based post. You can use simple CURL and few GD library function to extract an image from a given URL. The basic concept in achieving this to extract the content of the file using CURL. This may sound complicated but it is not. CURL is used to extract the page content of an external URL. In our case the URL is an image. So it will extract the content of the image. After we have that we use a php function and few other GD functions to save that as an image file:

Here is how I had done that:

Double click on any text in page for its definition and search

Hi,

There are many plugins available for various browsers which does that, I have come up with a custom solution. What is happening here is, considering a web page which has textual content. Whenever a user selects a word by double clicking it, a small popup comes up with the definition of that word. Now in my case i have opted to show the definition of the selected word, you can put your functionality for that word, e.g. a site search with that word.

Since I do not have a word dictionary so I am getting the definition of the word from definr.com and parsing it. You can change this to meet you requirements.

For this you need two files, first being a simple HTML file, "index.html" with the following:

Watermark on image


Hi,

With GD library in PHP, you can easily add a watermark to any image. The idea of this is to merge two image on top of one another. This possibly is the one of the easiest things that can be achieved by GD library. But watermark literally means a small semi transparent image will be placed on another one. For this you need to have a semi transparent image. Which will preferably be a PNG file.

Use the following code to achieve watermarking on image:

$stamp = imagecreatefrompng(PATH_OF_THE_SEMITRANSPARENT_IMAGE);
$im = imagecreatefromjpeg(PATH_OF_THE_ACTUAL_IMAGE);

Wednesday, February 23, 2011

Image upload without page refresh

If you are uploading a file without refreshing a page, you do not need any ajax or any flash uploader for such a realtime uploader. This can be easily achieved by simple conventional HTML and javascript. The solution is too simple to believe it exist. The idea is to use a iframe and redirect the upload destination page to the iframe instead of the current page.

This is achieved by the "target" attribute. You can use this attribute to plain hyperlinks as well. For example if you have <a href="mypage.html" target="iframe_1">Click</a> and you have a iframe whose id is "iframe_1", then the page mypage.html will open in the iframe instead of the current page. So the same concept applies for file upload without refreshing current page.

You need to have 2 php files, let the first file be, "upload.php" with the following:

Convert an image to gray scale

PHP provides a very strong support for image manipulation. You can convert any image into its grey scale equivalent easily. The only thing you need to know apart from the given code is how to generate a black and white version of a particular color.

What I mean to say is, take any color for example. May be red. We have to know the rgb equivalent of it. In our case the rgb equivalent of red is rgb(255,0,0). Now the grey scale equivalent of that is as follows:

Grey Scale:
Red = (255 + 0 + 0) /3
Green = (255 + 0 + 0) /3
Blue = (255 + 0 + 0) /3
that is
  rgb(85,85,85)= grey scale of red

Anyways, here's the code ;)

IP to location

If you are looking for a solution for converting IP address into its relevant location then here it is. I had done this conversion in 2 ways.

Method #1: First is purchasing an IP to location database from maxmind.com. In this you just need to dump the database in your server and use sql queries to get the location details of a particular IP. This perhaps, I hope to discuss in my future blogs in details.

Pros: The data retrieval is fast as it is in your database.
Cons: You need to update your purchased database from time and again for new IP's in the long run


Method #2: This is relatively much similar approach. You can refer to online API's. I used ipinfodb.com. In this you first need to get a api key. This feature is added new to the website. You need to register and they will mail you a API key. Use the API key in the following URL to get the location details in XML format:

Tuesday, February 22, 2011

Address to Latitude / Longitude


In PHP you can easily find the latitude and longitude of a particular address, which is in simple text format. All you have to do is to copy and paste the code which is provided at the end of this post. In this the address is converted by the use of a google API.

Now for that first you need to get a registered API key. You can signup for the API key here.

Paste the following code in your .php file and execute:

Javascript Activity Flipper

I am not sure what to call this, but you will get to see this in twitter, foursquare etc, where the activities roll on. It is similiar to marquee effect but better than that. Here is a snapshot of what I am talking about:



So I have come up with my own solution for this. Pretty simple and neat. Just copy the following and paste in a .html file and view in your browser:

Saturday, February 19, 2011

Simple Query Caching

Hi folks,

In my previous post I had discussed about Memcache in CakePHP. In this I have come up with my own solution of caching queries. It took me around 30 minutes to come up with the following class, so you can imagine it might not be the best one. This exactly is not memcaching, but simply query caching. But it will speed up your queries anyways. This following class can be implemented with simple PHP. For other frameworks or open sources you need to customize the class accordingly.

So here is the class,"class.memcache.php":

Memcache with CakePHP

Hi,

Memcache simply means caching your query. This means that if you are caching your query then you do not need to make a database call every time. You need to run the database query once and set a refresh time. The next time, instead of the database the query will be fetched from the cache, thus improving the performance of the site. The refresh time is the frequency of refresing the content of the cache.

For achieving this you need to install memcache in the server. But CakePHP has a inbuilt caching system, which is similiar to conventional memcaching. Here I will discuss CakePHP's inbuilt memcaching system. In this you do not need to install memcache in your server.

Assuming you are using a model, Customer. The query you are using is:

CSS & Javascript Compression

Hi,

Sometimes it is necessary to load js and css which are huge in size, which at the end of the day effects the websites performance. This post will help you in compressing your css and js files on the go. The methods of coversion is basically the same for both, but i will still give the required code indivisually.

CSS Compression:

Friday, February 18, 2011

FFMPEG for Windows

Hi,

FFMPEG has been a very interesting topic for a very long time. This post will guide you for enabling ffmpeg and all its functionality under Windows environment. I have been using XAMPP as my local server and this following method works like charm to me. But if you are using anything else, then you need to make the changes accordingly (e.g. the path of php/extension's direcory)

So here is the trick:

Thursday, February 17, 2011

Facebook TAB with iFrame and flash embed

fHi,

In my previous post I had explained how to create a facebook page and adding a static TAB to it. Now managing the content of the tab is not easy enough. Specially when trying to embed a iFrame or a SWF file. This post will help you do that.

1. Loading a iFrame
For loading an iFrame this is what I had done. In the Edit page of the FBML App:


Facebook TAB

Hi,

Creating a business or group or any page in facebook is a fast spreading trend. Along with, adding custom tabs in those page. If your tab content is static then creating one is just a matter of minutes.

1. First add the FBML application to your custom page(if you don't know how to create a custom page click here).
2. After creating the page and adding the FBML to it, go to edit page option, there you will find:

Drupal Module 2

Hi,

Drupal provides an excellent multiligual support with minimum effort. I came across two such modules:

1. Google Translate
2. Language Icon

These are a whole bunch of them out there. But I liked these two. Google Translate uses conventional translation method for converting the entire page. It fetches data from google itself for the translation, so you cannot edit them. But in Language Icon you can specify translation of each word in Drupal dictionary. You can also create a language if you want. The only problem is you need to sit down and make the entire translation manually. But both of them rocks.

Other interesting modules are:

Wednesday, February 16, 2011

Animated GIF with PHP

Hi,

This is another intersting topic. If you want to make a frame based animated GIF file using PHP, then this post is exactly what you need. First of all you need to include() the class file in your .php file. The class is attached at the end of this post. The basic idea of creating a framed based animation is to create to arrays. the first array will contain the path of the frames, which basically are simple image file and the second array will define the duration one particular frame will have. e.g.

if ( $dh = opendir ( "frames/" ) ) {
 while ( false !== ( $dat = readdir ( $dh ) ) ) {
  if ( $dat != "." && $dat != ".." ) {
  $ext=substr($dat,-3,3);
   if(strtolower($ext)=="gif"){
    $frames [ ] = "frames/$dat";
    $framed [ ] = 5;
   }
  }
 }
 closedir ( $dh );
}


In the above code $frames[] holds the path of each image file which are in "frames/" directory, and $framed  has the duration of each frame respectively. You can customize the above code as per your need.

The main trick of creating the animated single GIF file is:

Control Download Speed

Hi,

If you are creating a media repository and want to control the download speed of files for different users, then that can be easily achieved by PHP. The basic idea is when you are downloading a MP3 or any other format file, make the download via a .php file (Force Download). Now in force download you change the page header with respect to the file extension.

For downloading in force download you need to the read the content of the file and print it. So now we have the header and the echo or print command for displaying the content of the file. This will trigger the download functionality for any file.

The trick of controlling the download speed is, when you are reading and printing the file content don't read the entire file at a time, but read it "x" bytes at a time in a file reading loop. This "x" is the kbps speed you are downloading the file.

To make it simple here is the code:

Drupal Module 1

Hi,


This is my first post in the blog. I would like to share a Drupal 6.2 modules which come in very handy on a daily basis. In a Drupal powered website it is very critical to manage different layout for different pages. But basic idea is, for every layout you just need to create seperate themes. Now if you do not know how to create one, this link is just what you need: