Posts Tagged ‘PHP’

Re-introducing Eventageous for PHP

March 3rd, 2010

This is an old project of mine but I decided to remake it bigger, stronger and faster. And it seems, I never actually released it before when I thought I did. I’ve completely rewritten the code and put it up on google code.

So what is it? It is basically a glorified event logger for PHP. Information can be logged to files (and auto-rotated), output to screen, extended with custom events, and even use it with your PHP ajax.

Click through for details.

» Read more: Re-introducing Eventageous for PHP

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.

More examples of image manipulation with PHP

January 14th, 2010

All of these examples use the PHP simple image manipulation class that I created.

Every now and then I write a batch image converter snippet that others might find useful. I figure I’ll post them here for my benefit and yours. These snippets will also serve as a way to better understand the class. As I come up with these in life, I’ll update this post.

All of these examples assume that you’ve already included the class in your file.

» Read more: More examples of image manipulation with PHP

Simple Image Manipulation in PHP (Rotate, Resize, Crop, Flip and Mirror, Thumbnails square and regular)

December 31st, 2009

UPDATE: I’ve added support for PNG alpha transparency which makes this class significantly more useful. Changelog in the class code below.

I keep misplacing this class so I figured I’d post it here so that I know where it is.

I wrote/assembled this class because I wasn’t happy with any of the image manipulation libraries out there. They are either way too complex with a steep learning curve or didn’t work all that great. (I didn’t see any reason to load up a class with 3,000 lines of code just to resize or rotate an image.)

So here it is, a simple class that allows you to open an image, perform common operations, and save. There are no bells and whistles or anything exciting here. Just a collection of commonly used image manipulation functions: Rotate, Flip, Resize, Thumbnail (square and regular), and Crop.

A lot of the class code is original but some of it has been gathered over my life as a programmer from long lost sources. If you recognize your work, leave a comment and I’d be happy to cite you!

» Read more: Simple Image Manipulation in PHP (Rotate, Resize, Crop, Flip and Mirror, Thumbnails square and regular)

Repeatable Random: An even distribution of pseudo-randomness

December 2nd, 2009

duck_goose_medium

Does the concept of true randomness really exist?  To tackle that question we must first ask ourselves, does free will exist, or are all our lives pre-determined based on some cosmic event that happened billions of years ago?  That is a question I’m not going to even try to answer in this post. Instead, I will provide you with a simple PHP function that can provide you with a repeatable random number generator.

OK, maybe “random number generator” isn’t an accurate name for it. Perhaps hasher-to-evenly-distributed-values generator would be a much better title since that is exactly what it does.

Why would you need such a thing?

Random numbers are great. They allow you to give your web applications some personality. Let’s say you only want to display a message on a web page ~10% of the time. Random numbers allow you to do this. You would simply do something like: if ( rand ( 1, 10 ) == 5 ) echo ‘This is my message. I display about 10% of the time!’;

That’s all well and good, but what if I want to display a random message to my visitors AND make sure they see the exact same one each time? Not possible with plain ol’ random.

However, it is possible with this very simple repeatable_random PHP function.

» Read more: Repeatable Random: An even distribution of pseudo-randomness

Detecting IE with PHP to enable display:table-cell CSS

October 30th, 2009

An ugly hack for an ugly web browser.

I love the display:table-cell.  It gives you the ability to vertically align stuff.  It’s great.  With display:table-cell, you don’t ever need table layouts again.

The problem?  I’m sure you already know or you wouldn’t be reading this.  Internet explorer, the bastard son of the web browsing world, doesn’t support it.  (IE8 does, but IE8 still sucks for other reason.)

My solution involves  (the normally bad idea of) changing output based on user agent strings.

» Read more: Detecting IE with PHP to enable display:table-cell CSS

Getting a weighted random value in PHP

July 15th, 2009

I am mainly posting this code here because I keep misplacing it on my own hard drive.  And that usually leads to it being forgotten and lost…  if there is anything confusing here, don’t hesitate to ask in the comments–I wrote this a bit fast.

What is a weighted random value?  Take a look at this (poor) example…

You have an array of shades of grey in hex:

$colors = array('000','1f1f1f','3f3f3f','666','999','ccc','eaeaea','f0f0f0');

You have a table of data and you want to give each row a random background color that is one of those shades.

Simple, right?  echo $colors[array_rand($colors)];

But, what if you want to return the colors around #999 more often than the rest?  » Read more: Getting a weighted random value in PHP

CacheMogul – Disk caching in PHP made simple

May 5th, 2009

I’ve finally gotten around to formerly putting many of my projects online.  This is the first part in a series of many, detailing those projects.

Up until recently, this project didn’t have a name.  This is basically a more polished version of the caching that Facelift uses.

When accepting uploads from a community of users, you need to store that data someplace.  You can’t store it in a database unless you want to end up pulling your hair out a few months down the line when you are trying to backup an 8GB database regularly. So what do you do?

» Read more: CacheMogul – Disk caching in PHP made simple

A couple hack fixes for the WP Twitter Updater plugin

March 16th, 2009

I needed a plugin that would simply send a tweet to twitter every time I posted a new article on one of my sites.  The answer I found was Twitter Updater.

The problem with the plugin is some pretty huge bugs. For example, at the very least, it will post 2 duplicate tweets for your post.  Depending on how you have it configured, it could make up to 5, 6, or even 7.  Pretty annoying and unusable.

I’m not a WP junkie and have no plans of becoming one, but I did create a quick and dirty hack to fix this behavior in Twitter Updater.  It does nothing to fix the real bug. » Read more: A couple hack fixes for the WP Twitter Updater plugin

Quickly Determine Local or Remote Copy of Your Website (with PHP)

September 15th, 2008

I do a lot of web development locally on my computer and I am always switching between local and remote servers using entries in the hosts file.  Because of this, it is hard to know if I’m actually looking at the correct version of my site.  Opera and Firefox both cache DNS to various degrees which makes it difficult to know if that change to the hosts file really did take effect.  I know you can disable the cache in Firefox — and probably Opera too — but I think this method is still useful.
» Read more: Quickly Determine Local or Remote Copy of Your Website (with PHP)