Embedd Javascript in AJAX Response

Hi everyone…

I came across a problem few days back, it was about calling or embedding javascript result/response in AJAX response. I was working on one of the feature of PHP based site http://www.PingMyCompany.com

I found that, javascript can’t be called in AJAX response, possible reason being, AJAX response is build on server side while javascript is simply client side (I may be wrong here, OR there can be some other reason for this as well).

So what to do, what if we have to embed the output of some external JS file in AJAX response as below???

/****AJAX response START****/

/**********Some Response*******************/
/********** Some Response ******************/
/**********Response from some JS file*********/
<script src=”http://www.crunchbase.com/cbw/company/zigron.js&#8221; type=”text/javascript”>
</script>
/**********Some Response*******************/
/********** Some Response ******************/

/****AJAX response END****/

Now the response from JS file will not get embedded with AJAX response. So what I did to solve this issue is that I got the output of JS file in a PHP variable using file_get_content function as follows.

$contents = file_get_contents (“http://www.crunchbase.com/cbw/company/zigron.js&#8221;);

Alternatively you can do the same using cURL but file_get_content is really smart solution (if the JS is file is not suspicious…).

Once done, you must know the format of contents being returned by the JS file. Now you can do simple string manipulation on the contents to mangle it according to the requirements and just echo it where you want to embed it in AJAX response. I have removed document.write from the start of the string and escaped few of whitespace characters and that’s it….

****AJAX response START****

/**********Some Response*******************/
/********** Some Response ******************/
/**********Response from some JS file*********/
$content = file_get_contents (“http://www.crunchbase.com/cbw/company/zigron.js&#8221;);
$content = substr($content,16);
$content = substr($content,0, strlen ($content)-2);
$content = str_replace(“\\n”,””,$content);
$content = str_replace(“\\t”,””,$content);
$content = str_replace(‘\\”‘,””,$content);
echo $content;
/**********Some Response*******************/
/********** Some Response ******************/

/****AJAX response END****/

You can see the working example by clicking here. Click on “CrunchBase Profile” tab button which show ajax response built using remote JS file.

Feel free to discuss..

Thanks.

Keep Smiling 🙂

Advertisements
Posted in PHP. Tags: , , . 5 Comments »

SEO – Another Pain for Developers

Hi Everyone…

With so many developers’ pains around, SEO is the buzzing now days. Though it is more related to internet marketing of your sites but at the end of the day your site can’t be best marketed until or unless developer put his efforts in this department in terms of making it SEO friendly.

Static site involves a little or no SEO programming at all but things become worst when the sites are complex and involves dynamic contents (shopping carts, portals with RSS feeds and other dynamic contents etc…).

In making site SEO friendly, developer needs to take care of things that he feels are meaningless. One of them is ‘page titles’. Other could be ‘meta keywords’ that even don’t get showed up anywhere. Hfff….

He also has to compromise on his favorite dynamic query string and make the site links all static. This compromise is a real pain, not in terms that it is harder to tackle, but developers are in habit of using query string for data transfer.

Having a little experience of making site SEO friendly (On Page Optimization), I will suggest few key points that a developer should keep in mind before development.

  1. Thinking of static URLs. (URL Rewriting)
  2. Dynamic Page titles & keywords in meta tags
  3. Dynamic Sitemap
  4. Javascript Organization

I will discuss these key points in detail in next post and will try to provide cure to this pain using PHP in a simplest possible way and will try to convert this pain to handy skill that a developer can have.

Regards,

Posted in SEO. Tags: , , . 6 Comments »

Creating Thumbnails in PHP

Hi Everyone…!!!

Before starting make sure that the following line is uncommented in php.ini file

extension=php_gd.dll

Following is the function which creates 3 copies of image of different size i.e., thumbnail (approx 100*100), small (approx 250*250) and medium (approx 400*400).

You just have to pass it the complete path of the original image and it will create three different images for you.

function CreateThumbnail ($imagePath)
{
//Create your custom size and names
$sizes = Array (100, 250, 400);
$names = Array (“thumbnail”, “small”,”medium”);

//if passed image exists or not
if (file_exists ($imagePath))
{
//extract Image name and path
$imageName = substr (strrchr($imagePath, “/”), 1);
$imagePath = substr ($imagePath, 0, strripos ($imagePath,”/”)+1);

//create all my custom images
for ($i=0; $i<count($sizes); $i++)
{

//Get the dimensions of original image
$sourceImage = imagecreatefromjpeg(“$imagePath”.”$imageName”);
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);

//Dimensions of target image (one by one)
$newWidth = $newHeight = $sizes[$i];

//Create Target image with target dimensions
$targetImage = ImageCreateTrueColor ($newWidth, $newHeight);

//Shrink the image to target image and create it physically
imagecopyresampled($targetImage,$sourceImage,0,0,0,0,$newWidth,

$newHeight,$sourceWidth, $sourceHeight);

imagejpeg($targetImage, “$imagePath”.$names[$i].”_$imageName”);
}
return true;
}
else
return false;
}

A simple call to this function could be

CreateThumbnail (“images/example.jpg”);
It will create three file namely, thumbnail_example.jpg, small_example.jpg, medium_example.jpg


Keep Smiling 🙂

Posted in PHP. Tags: , . Leave a Comment »

Creating Thumbnails in .NET

Hi everyone….!!!

Following is the code which can creates the thumbnails of images with no hardwork….

System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imagePath); System.Drawing.Image thumbnailImage = originalImage.GetThumbnailImage(90, 90, null, IntPtr.Zero);

thumbnailImage.Save(thumbnailPath);

See how easy it is to create thumbnail in .net

Keep Smiling 🙂

Increase PHP File Upload Size Limit

Hi Everyone…

I was working on final year project which was web based file manager. In that I was writing script to upload files. The problem i faced was that I can only

upload files of size <= 2MB. I changed the max file upload size in php.ini but it was useless and after lot of search i came to know about .htaccess and it solved my problem. Let me explain this step by step.

1. First of all make sure that your file upload controls allows you to upload files of you requirement. You can change the below highlighted value according to your requirment.

<form enctype=”multipart/form-data” action=”uploader.php” method=”POST”>
<input type=”hidden” name=”MAX_FILE_SIZE” value=”100000” />
Choose a file to upload: <input name=”uploadedfile” type=”file” /><br />
<input type=”submit” value=”Upload File” />
</form>

2. By default you can upload a file of max size 2M. To increase this open the file php.ini and look for the following lines.

; Maximum allowed size for uploaded files.
upload_max_filesize = 2M

and change the ‘2M’ to your required max size. Restart your apache server and go ahead.

3. If you don’t have access to php.ini file (may be because of shared webserver) or if above step doesn’t work then look for a file .htaccess and find the following lines.

RewriteEngine On
php_value upload_max_filesize 2M

again change the ‘2M’ to your required max size. Restart you apache server try your luck now though I was lucky after this step.. 😉

For very basic tutorial on file uploading see http://www.tizag.com/phpT/fileupload.php


Keep Smiling 🙂

Get Directory size Recursively in PHP

Hi everyone…

Following is the function to get the size of directory whose path is passed to this function. This function recursively finds the size of all the subdirectories and files.

function GetDirSize ($directoryPath)
{
$delimeter = DIRECTORY_SEPARATOR;
$size = 0;
if (is_dir($directoryPath))
{
if ($dh = opendir($directoryPath))
{
while (($file = readdir($dh)) != false)
{
if($file != “.” && $file != “..”)
{
if(is_dir($directoryPath .$delimeter. $file))
{
$size += GetDirSize ($directoryPath.$delimeter.$file);
}
else
{
$size += filesize($directoryPath.$delimeter.$file);
}
}
}
}
}
else
$size += filesize($directoryPath);
@closedir($dh);
return $size;
}
Keep Smiling 🙂