Posts Tagged ‘Snippet’

Separate RGB Channels from an Image in PHP

January 20th, 2010

Divide an conquer! That’s what I always say. But how do you go about dividing an image up into it’s separate channels–like Photoshop does it?

There are actually two ways to go about doing it. First, is displaying the actual amount of red, green, or blue that is contained in each pixel which results in images that are made up entirely of shades of the color channel that you are viewing. The second is to to simply display the information in an easily understood visual representation by using shades of grey. Both are detailed below.
» Read more: Separate RGB Channels from an Image in PHP

pr() a print_r enhancement for your HTML web page

January 18th, 2010
function pr ($mixed)
{
	echo '<pre>' . htmlentities( utf8_encode( print_r( $mixed, true ) ), ENT_QUOTES, 'utf-8' ) . '</pre>' ;
}

When writing PHP, I often find that I need quick debug access to an Object or Array to see what it contains. PHP provides print_r, which works great–except that the data appears without all the tabbing in regular ol’ HTML. This is annoying for large Arrays or complex Objects.

That is why I created the pr() function. It basically just wraps the output of print_r in pre tags and then your formatting is back.

Depending on the encoding of the data you are passing to and getting from print_r, you may run across problems when combining print_r and htmlentities (with utf-8). If the encodings don’t match, it may return nothing at all! This could either mean that the variable is some kind of empty, or it could mean that htmlentities didn’t like the encoding that was returned by print_r, and crapped all over itself.

So, you first have to convert the data returned by print_r to utf8 (or whatever the encoding of your web page is) before passing it along to htmlentities. Since I try to keep every web page I work on in utf8, I keep this function set up to compliment that.

As long as you have a working utf8_encode, this function should always output pretty, utf-8 encoded data in HTML. If you need to convert to some other encoding beside utf8, you can use mb_convert_encoding.

An Easy Fix for a Minor Annoyance When Creating Widgets or Shareable Content

October 11th, 2008

For the last couple days I’ve been dabbling with creating a Javascript that will allow visitors to one of my websites to embed my content in their sites.  (It’s done and you can check it out at http://www.angieshealth.com/article-widget/)

To make it easier on my visitors when copying and pasting the code I decided to use Javascript to select all the code when they click on the textbox.
» Read more: An Easy Fix for a Minor Annoyance When Creating Widgets or Shareable Content