neongreen.com > tutorials > textamerica

the great textamerica grab and stash

We all love textamerica, right? They offer a great free product and even serve your latest image to any site that you desire. This is great, except that for various reasons I wanted to host the latest image on my own server. Let's examine how to do that using PHP.

Please note that this tutorial covers how to grab only the most recent image from textamerica, overwriting your local version each time. Archiving is possible, but since textamerica's recent image is cropped, there is not much incentive to save it.

When you sign up for a Textamerica account, you will sooner or later stumble across some code to put your most recent image on your own site:

<script language="javascript" type="text/javascript" src="http://domain.textamerica.com/mypage.asp"></script>

Extract the web address:

http://domain.textamerica.com/mypage.asp

Enter the discovered URL in your web browser's address bar. With any luck you'll be viewing a completely blank page. View the source code for the page anyway and you'll find your current image path. It will be something like:

http://domain.textamerica.com/recent.aspx?height=&width= alt=" domain.textamerica.com

Ignore the height, width and alt properties and just take note of this much:

http://domain.textamerica.com/recent.aspx

The next step is to give full permissions to a folder that will hold this image. For security reasons I didn't want to give full permissions to a folder within my public web directory.

I created a folder outside of my public site with full permissions. This step will vary based on your hosting environment and is outside the scope of this tutorial. To get you started, here is a brief tutorial on using WS_FTP or Dreamweaver to change folder or file permissions.

Please be careful with full permissions and be sure that you understand what you are doing. You may want to ask your web hosting company for help with this if you are not comfortable with it.

Now that the writable directory in place, I needed a script to get and place the file.

<?php
//the web path of the image you want to get
$source_file = "http://domain.textamerica.com/recent.aspx";

//the absolute file path you are writing to
$destination_file = "/rootfolder/site/safeDirectory/pic0.jpg";

//read the source file into buffer
ob_start();
readfile($source_file);
$filecontents = ob_get_contents();
ob_end_clean();

//move its contents
//this overwrites the destination file!
if($fp = fopen($destination_file,'w+')) {
   fwrite($fp,$filecontents);	
   fclose($fp);
}
?>

Paste all of the above code in a new text document, being sure to replace the destination file and source file paths with your working file paths. Note that the $destination_file must be the absolute path to the directory on your web server.

Save this file as ta.php and place it in any directory you desire. I personally place mine in an administrative area and execute the script via a link whenever I decide to grab the latest image.

Now that the image is in place, we need to display it. I chose to not link directly to the directory that contains the image, although doing that is an easy way to display the image. Instead, I link to a php file that returns the image for me:

<img src="/anyDirectory/image.php" alt="ta image" border="0" />

The file image.php contains the following code:

<?php
ob_start();
header("Content-Type: image/jpeg\n");
//the absolute file path to return
$image = "/rootfolder/site/safeDirectory/pic0.jpg";
readfile($image);
?>

That's it. If you have questions or suggestions, please contact me.

Last modified April 9, 2004

Many thanks to the fine folks over at the Twin Cities PHP Users Group and the SitePoint Forums for helping me out on this.

A thank you also goes to Jeremy for helping me organize my thoughts.

RSS 2.0 Feed

flickr