public marks

PUBLIC MARKS from imelgrat with tag "Sample Code"

September 2007

The JavaScript Event Model (part 1)

(via)
JavaScript comes with a fairly well-thought-out event model, one which can be exploited by a clever developer to perform fairly complicated tasks on the client side. This event model is one of the most fundamental things about the language - and, over the next few pages, we’re going to show you the basics of how it works. Keep reading. Simply put, the event model provides a way for a user to interact with JavaScript. It consists of two basic components, events and event handlers, which together define what happens when the user performs a particular action.

July 2007

Resize image proportionally with optional background color using PHP and GD

(via)
This is an update of a previous post, which described a PHP function for resizing images using the GD library and some clever math calculations to determine the optimal width and height. This function resizes a picture to the chosen size while maintaining the original image’s proportions, thus avoiding image distortion. In the previous version, the function always used a black background, so images that needed to be cropped would sometimes display black bars on the sides. This updated function allows the user to select the background color, passed as an array of 3 integer numbers (RED, GREEN, BLUE). This makes it eay to “blend” the resized images to the page background, without any black bars on the sides. This new version has been fully documented (using PHPDoc-style comments).

Sending XML files to a Webservice (Using cURL)

a PHP function that hides all the necessary logic from the user and handles the posting of the XML document and returns whatever the server responds. It relies on PHP’s cURL library (so you need it properly configured on your server in order to work). All you need to do is create the XML document, choose the URL (and port) to which you want to post the XML document and the function takes care of the rest. Below is the function code. As you can see, the function can handle SSL-enabled servers, something that provides a great advantage, since many Web services run on HTTPS.

Resize image proportionally (cropping it if necessary)

This function will resize an image to the specified width and height without distorting it and in most cases without adding black regions to the image (by cropping the image to the desired size). It is very useful for creating picture thumbnails of a given size, regardless of the image’s original dimensions.

Convert a Webpage to Plain Text

This function takes a URL and returns a plain-text version of the page. It uses cURL to retrieve the page and a combination of regular expressions to strip all unwanted whitespace. This function will even strip the text from STYLE and SCRIPT tags, which are ignored by PHP functions such as strip_tags (they strip only the tags, leaving the text in the middle intact).

Using Regular Expressions to Find RSS Links on a Page

the class performs three main steps. First, the cURL library is used to fetch the content pointed to by the URL the user passed.. Second, since PHP doesn’t have an SGML parser built in like Python seems to, so getting all the "link" tags has to be done manually. A few regular expressions and some simple string splitting made it all real easy. Last but not least, the function goes through all the links found, figures out which ones belong to RSS feeds, resolves them to absolutes URL if necessary, and stores them on an array, making sure the link isn’t already listed to prevent duplicate links (e.g., the RSS appears more than once in the page).