Skip to content


Positioning text vertically and horizontally using gd

I was working on a PHP script yesterday for creating a couple of graphs which required lots of text in various places aligned to text on the same line. Some on the x axix and some on y. Finding no inbuilt function in gd on php, I created this simple function to align text vertically and horizontally as required based on the parameters passed to it.


function positiontextinimage($img, $font, $size, $color, $string, $x,$y, $horiz, $vert)
{
$bounds=imageftbbox  ( $size  , 0  , $font  , $string);
switch($horiz){
case "left":
break;
case "right":
$x=$x-$bounds[4];
break;
case "center":
case "centre":
$x=$x-($bounds[4] -$bounds[0])/2;
break;
}
switch($vert){
case "top":
$y=$y-$bounds[5];
break;
case "bottom":
break;
case "middle":
$y=$y+($bounds[1]-$bounds[5])/2;

break;

}
imagefttext  ( $img  , $size  , 0  , $x, $y, $color, $font  , $string);
}

Posted in Troubleshooting.

Tagged with , , , .


PHP Proxy script to solve javascript and jquery cross domain issues

I just finished a project in which a jquery dialog used a jquery.ajax call using get to obtain details of a transaction from a url on another domain, effectively a cross domain ajax request. Since firefox 3.0 does not allow itself to be set to allow cross domain requests, I had to use some other way. I could have just used an iframe, but that is another story in itself.

Here is the bit of javascript code.


function refaxorder(){
//idfield=$("#idfield").val();
query="urlid=1&inOID=" + idfield + "&inKey=" + key + "&inAction=reFax";
$("#actiondiv").html("<div id=actiondivin><div>Loading...</div></div>");
$("#actiondivin").dialog({autoOpen:false, modal:true, title:"ReFax", buttons:{OK: function(){$(this).dialog('destroy').remove(); }}});;
$("#actiondivin").dialog("open");

$.ajax({type:"GET", url: "proxy.php", data: query,  dataType:"html", success:function(msg){
$("#actiondivin").html(msg);
}
});
}

To get around this, I implemented a couple of lines as proxy.php to act as a proxy. Please note that I am only allowing it to be passed the urlid instead of a url to prevent it from becoming an open proxy. Also IP based filtering could be implemented to make it more safer. Have a look, maybe it will come in useful for you too.


<?php
isset($_GET["urlid"])?$urlid=$_GET["urlid"]:$urlid=1;
is_numeric($urlid)?true:$urlid=0;
$url[0]='http://www.url1.com/test/test.php';
$url[1]='http://www.url2.com/test/testactions.php';
foreach($_GET as $key=>$value){
if ($key!='url') $query[]=$key."=".$value;
}
if (count($query)) $querystring=implode("&",$query);
$fullurl=$url[$urlid]."?".$querystring;

$fp=fopen($fullurl,'rb');
$page = '';
if ($fp===false) die("Unable to open");
while (!feof($fp)) {
$page .= fread($fp, 8192);
}
fclose($fp);
print $page;
?>

Posted in Troubleshooting.

Tagged with , , , , , .


Unable to uninstall Audio Driver – Realtek, Sound not working

A system at the office was having the Realtek audio control panel icon showing up in the system tray, with the 3d test working, but windows wasn’t showing the audio device in the sound and audio control panel applet. Instead windows said that not audio playback device was found. As a result no applications were able to play audio.

This post at tomshardware.com provided an easy solution.

Just open regedit and delete the key folder //HKEY_LOCAL_MACHINE/SYSTEM/CONTROLSET001/ENUM/HDAUDIO and restart your computer. It should prompt you to install the hardware again.

That fixed the hardware issue and the user was able to view www.abcnews.go.com/whatwouldyoudo happily. :)

Posted in Troubleshooting.

Tagged with , , , .


Python script to get addresses from google maps

A simple python script to get addresses of businesses in a city. Just a quick demo for a client I wrote in an hour.

import urllib;
def getdata(idstr, matchstr):
	matchstrlen=len(matchstr)
line=idstr[idstr.find(matchstr,0)+matchstrlen:idstr.find("\"",idstr.find(matchstr,0)+matchstrlen)]
	return line
url="http://maps.google.ca/maps"
data="f=q&source=s_q&output=js&hl=en&geocode="
location="&q=" + urllib.quote("airport loc: New Delhi, India") + "&btnG=" + urllib.quote("Search Maps")

fp=urllib.urlopen(url + "?" + data + location)
filecontents=''
for line in fp.readlines():
	filecontents=filecontents + line
morecontent=True
startloc=0
while morecontent==True:
	startpos=filecontents.find("id:", startloc)
	if startpos>-1:
		endpos=filecontents.find("}}}", startpos)
		if endpos>-1:
			startloc=endpos+1
			section=filecontents[startpos:endpos]
			sxti=getdata(section, "sxti:\"")
			sxsn=getdata(section, "sxsn:\"")
			sxst=getdata(section, "sxst:\"")
			sxpr=getdata(section, "sxpr:\"")
			sxpo=getdata(section, "sxpo:\"")
			sxph=getdata(section, "sxph:\"")
			actual_url=getdata(section, "actual_url:\"")
			print sxti + ", " + sxsn + ", " + sxst + ", " + sxpr + ", " + sxpo + ", " + sxph + ", " + actual_url
		else:
			morecontent=False
	else:
		morecontent=False

Posted in Tutorial.

Tagged with , , .