Subesh Pokhrel

Magento Developers Blog

Latitude and Longitude of a Place in Google Maps Using Zip Code

I have already posted similar post for finding out the latitude and longitude of a place using Google maps, but when I studied the traffic details and found out that when people visit that post, they sometime come with keywords like zipcode+latitude and longitudes, so I thought it would be better to them to post another article, giving them what they exactly want. Before I write down the codes there is something we should know. Google provides a service of returning CSV (Comma Seperated Values) when requested to http://maps.google.com/maps/geo?q=$query&output=csv&key=$key. The $query may contain address, zip code, city and country. The service also outputs in other formats. But we have choosed CSV format because it would be easy for us to explode the values separated by commas. The output type is defined by the output parameter in the URL. The output or response we then receive are in the format of geocode, accuracy, latitude, longitude. $key is the Google maps API key. [sourcecode=”php”] gKey) > 1) { $q = str_replace(’ ‘, ‘_’, $this->address.’,’.$this->zip.’+’.$this->city.’,’.$this->country); if ($d = @fopen(“http://maps.google.com/maps/geo?q=$q&output=csv&key=”.$this->gKey, “r”)) { $gcsv = @fread($d, 30000); @fclose($d); $output=array(); $tmp = explode(“,”, $gcsv); // $this->code = $tmp[0]; // $this->Accuracy = $tmp[1]; $output[0]=$this->latitude = $tmp[2]; $output[1]=$this->longitude = $tmp[3]; return $output; } else { $error = “NO_CONNECTION” ; } } else { $error = “No Google Maps Api Key” ; } } } ?> [/sourcecode] The above class can be implemented as following. [sourcecode=”php”] $obj_google=new googleRequest; $obj_google->zip=35005; /* alternate uses $obj_google->country=”Country Name Here”; $obj_google->city=”City Name Here”; $obj_google->address=”address”; */ $obj_google->gKey=”ABQIAAAAPHLcOOGHX2-uLk3K8q1nMRTkUAbhgKwL1jWWfpv-KGJeCrct7hTsLLnZdnZjzehmRIkaePagQvKNbw”; $latlng=$obj_google->GetRequest(); var_dump($latlng); [/sourcecode] The above returns latitude and longitude of place with zip code 35005 as array(2) { [0]=> string(9) “33.592857” [1]=> string(10) “-86.994015” }

Checking if the Domain Is Registered or Not ?

On doing one of my project, I came across this situation when I need to find out whether the domain name supplied by the user is available or not? I then came up with a solution, here’s is what I did. But before that let me explain somethings. There is something called WHOIS LOOKUP. WHOIS LOOKUP is action relating to look on official WHOIS database server for the information related to domain name. According to wikipedia WHOIS “WHOIS is a TCP-based query-response protocol for querying an official database in order to determine the owner of a domain name, an IP address, or an autonomous system number on the Internet”. ICAAN is presently undertaking the task for managing the WHOIS information. For querying WHOIS servers here is the PHP code. [sourcecode=”php”] function check_domain($domain,$ext) { /************************ SERVER DEFINITIONS ************************************/ $serverdefs= array( “com” => array(“whois.crsnic.net”,”No match for”), “net” => array(“whois.crsnic.net”,”No match for”), “org” => array(“whois.pir.org”,”NOT FOUND”), “biz” => array(“whois.biz”,”Not found”), “info” => array(“whois.afilias.net”,”NOT FOUND”), ); /*********************** END SERVER DEFINITIONS *********************************/ $server = $serverdefs[$ext][0]; $nomatch = $serverdefs[$ext][1]; $output=”“; if(($sc = fsockopen($server,43))==false){return 2;} fputs($sc,”$domain.$ext\n”); while(!feof($sc)){$output.=fgets($sc,128);} fclose($sc); //compare what has been returned by the server if (eregi($nomatch,$output)){ return true; // if matched }else{ return false; } } [/sourcecode] Code #04 - #13 defines the array of official WHOIS server for respective extensions of domains and the respective responses (a chunk of) we get after querying the database servers. For example if we query whois.crsnic.net we get response which contains the phrase ”No match for” if the domain does not exists. Code #19 - #23 requests the respective server on the basis of extension and gets the response in form of string on variable $output. Then the response is matched with the respective value of response defined in the array (which contains the phrase when the domain does not exists). Then it outputs the boolean value accordingly if the domain does not exists or can be registered it returns true, false otherwise.

How to Generate Thumbnail of HTML Pages

I was just adding my site on Alexa and in some minutes Alexa generated a thumbnail of my html site. And again it was same with blog catelog. So I thought is there any PHP code that crawled the site and generates its image thumb? After a lots of research I came out with these. Remember, this only works in windows server. There is one thing called IECapt.exe ( A Internet Explorer Web Page Rendering Capture Utility). This renders the page and generate thumbnail of it. You can download it from IECapt.exe. And to run that through php here’s the code. This is the class file that generates thumb images from the output of IECapt.exe. Save it as image.class.php [sourcecode=”PHP”] image_type == 1) && !function_exists(‘imagegif’)) $this->image_type = 3; switch ($this->image_type) { case 1: if ($this->save_to_file) { $res = ImageGIF($im,$filename); } else { header(“Content-type: image/gif”); $res = ImageGIF($im); } break; case 2: if ($this->save_to_file) { $res = ImageJPEG($im,$filename,$this->quality); } else { header(“Content-type: image/jpeg”); $res = ImageJPEG($im,”,$this->quality); } break; case 3: if ($this->save_to_file) { $res = ImagePNG($im,$filename); } else { header(“Content-type: image/png”); $res = ImagePNG($im,”,$this->quality); } break; } return $res; } function ImageCreateFromType($type,$filename) { $im = null; switch ($type) { case 1: $im = ImageCreateFromGif($filename); break; case 2: $im = ImageCreateFromJpeg($filename); break; case 3: $im = ImageCreateFromPNG($filename); break; } return $im; } // generate thumb from image and save it function GenerateThumbFile($from_name, $to_name) { // get source image size (width/height/type) // orig_img_type 1 = GIF, 2 = JPG, 3 = PNG list($orig_x, $orig_y, $orig_img_type, $img_sizes) = GetImageSize($from_name); // cut image if specified by user if ($this->cut_x > 0) $orig_x = min($this->cut_x, $orig_x); if ($this->cut_y > 0) $orig_y = min($this->cut_y, $orig_y); // should we override thumb image type? $this->image_type = ($this->image_type != -1 ? $this->image_type : $orig_img_type); // check for allowed image types if ($orig_img_type < 1 or $orig_img_type > 3) die(“Image type not supported”); if ($orig_x > $this->max_x or $orig_y > $this->max_y) { // resize $per_x = $orig_x / $this->max_x; $per_y = $orig_y / $this->max_y; if ($per_y > $per_x) { $this->max_x = $orig_x / $per_y; } else { $this->max_y = $orig_y / $per_x; } } else { // keep original sizes, i.e. just copy if ($this->save_to_file) { @copy($from_name, $to_name); } else { switch ($this->image_type) { case 1: header(“Content-type: image/gif”); include($from_name); break; case 2: header(“Content-type: image/jpeg”); include($from_name); break; case 3: header(“Content-type: image/png”); include($from_name); break; } } return; } if ($this->image_type == 1) { // should use this function for gifs (gifs are palette images) $ni = imagecreate($this->max_x, $this->max_y); } else { // Create a new true color image $ni = ImageCreateTrueColor($this->max_x,$this->max_y); } // Fill image with white background (255,255,255) $white = imagecolorallocate($ni, 255, 255, 255); imagefilledrectangle( $ni, 0, 0, $this->max_x, $this->max_y, $white); // Create a new image from source file $im = $this->ImageCreateFromType($orig_img_type,$from_name); // Copy the palette from one image to another imagepalettecopy($ni,$im); // Copy and resize part of an image with resampling imagecopyresampled( $ni, $im, // destination, source 0, 0, 0, 0, // dstX, dstY, srcX, srcY $this->max_x, $this->max_y, // dstW, dstH $orig_x, $orig_y); // srcW, srcH // save thumb file $this->SaveImage($ni, $to_name); } } ?> [/sourcecode] Here’s the code to run it. Save this as webthumb.php. [sourcecode=”php”] image_type = $image_type; $img->quality = isset($_REQUEST[‘q’]) ? intval($_REQUEST[‘q’]) : $image_quality; $img->max_x = isset($_REQUEST[‘x’]) ? intval($_REQUEST[‘x’]) : $max_x; $img->max_y = isset($_REQUEST[‘y’]) ? intval($_REQUEST[‘y’]) : $max_y; $img->save_to_file = false; $img->cut_x = $cut_x; $img->cut_y = $cut_y; // generate thumbnail and show it $img->GenerateThumbFile($cached_filename, ”); ?> [/sourcecode] The codes are self explanatory. Just be sure you have placed all the three one one folder or managed the path accordingly.

My Site’s Alexa Ranking Took 24 Million Giant Leap

I could not stop myself from posting this blog post, because it has been over four weeks I launched this site and its Alexa ranking has improved by 24 million in three days interval. As I first started it was around 29 Million. After a week it went to 25 million figure, it was 25,214,769 on 5th June to be specific . And today on 8th June it has 1,996,306 ranking. So in three days the ranking took a 24 million leap. I don’t know if its natural or “alexaral” but there is no any other pleasure for those people, who keep on looking for their page rank or alexa rank of there site like me, to have such a giant leap.

Calculating the Difference Between Timestamps in PHP

Most of us, programmer have conditions of calculating the difference between timestamps, i.e the difference between now and the timestamp from mysql result. The logic for this is pretty simple. Just convert the two timestamps to Unix time format and take the difference between the two. Next convert the difference into corresponding days,hour,minutes or in any unit you want to. Here’s the source code for finding out the difference between two timestamps. [sourcecode lang=”PHP”] [/sourcecode] [sourcecode lang=”PHP”] [/sourcecode] There are two functions here expiredTime ( ) and countdown ( ). The input for the expiredTime is the timestamp from the mysql result. This function evaluates the value of year,month,hour,minutes and seconds of the input parameter and then calls the next function countdown ( ), with those evaluated values as inputs. The countdown function then calculates the time now and the difference code block 2 #08. Then the difference is divided accordingly, with appropriate denominators to find out the days,hour,minutes,seconds passed the input time, untill now. Code block 2 #10-13 is simple mathematics. This function then returns the difference value in days,hour,minutes and seconds.

Facebook 1yr Old and F8 - Applications 1,000,000,000!

On the first year of facebook platform released (May 24th) it has been announced that 1,000,000,000; yes, 1 Billionth application has been installed. No doubt it is the most powerful channel for software distribution.

Do You Know Which Sites Have 10 Page Rank (PR)?

It is no less gift for a Google Page Rank toolbar viewer to see their PR go up. My page is just starting and you should know that my PR is 0 :(. So now a days I googled a lot regarding PR and SEO and all those stuffs. While browsing I thought of what would a PR-10 site would be. On Googling (I often use Googlling for Searching !),I came up with PR-10 sites according to the latest change in Page Rank algorithm (April 30th 2008). Thought I should share, for those who may be keen like me. Here’s the list. If you want to view the PR-10 archieve of the past then you should find this http://www.searchenginegenie.com/pagerank-10-sites.htm very interesting.

How to Get Latitude and Longitude of a Place Using Google Maps

I was so frustrated during one project where I need to find out the geological latitude and longitude of the place, with the help of Google Maps. So I googled a lot and came up with a function function (overlay,point).On calling this function on mouse click event the latitude and longitude are returned as point.lat() for latitude and point.lng() for longitude. This is the source code just the javascript part. [UPDATE DEMO] [sourcecode lang=”javascript”]