i'm on twitter

Inline Images With Data URLs

August 11, 2010 No comments yet

This particular trick comes in very handy for the mobile platforms. Instead of linking to your CSS and your images, you can use this method to embed the images inline within the CSS. This keeps the browser from having to make multiple calls, thus speeding up the load time of your application. You can also use this method within an IMG tag as well.

	/* ***********************************************************
	* START :::
	*	64bit encodes images for CSS embedding
        *      Alternatively, if you are not using PNGs, you can specify the Mime Type
        *      PNG is the default mime
	*********************************************************** */
	function data_url($file, $mime='') {  

		$mime = empty($mime) ? 'image/png' : $mime;

		$contents = file_get_contents($file);
		$base64   = base64_encode($contents);
		return ('data:' . $mime . ';base64,' . $base64);
	}
	/* ***********************************************************
	* END :::
	*********************************************************** */

In the CSS style sheet

You would need to either tell your server to process files with the .css extension or save your CSS files with the .php extension. It’s your call.


 body { background-image: url('<?php echo data_url('http://domain.com/images/file.png'); ?>') no-repeat; }

/* output code would be something like */
body { background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFQAAABUCAIAAACTCYeWAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAI5JREFUeNrs2sENACEIRUF1LY0mqRbb2OQPHUwexIu7qlbq3JmJxZ8VPMorr7zy8NZeeXhrrzw8vJtXHh4eHh7eU6c8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8fMj4kAQPDw8P76lTHh7ezSsPb+2Vh7f2yiv/7/mS8dHlo2d3dyz+CTAAYEXI+4xG4igAAAAASUVORK5CYII=') no-repeat; }

Within HTML img tags


<img src="<?php echo data_url('http://domain.com/images/file.png'); ?>" border=0 />

<!-- output code would be something like --> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFQAAABUCAIAAACTCYeWAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAI5JREFUeNrs2sENACEIRUF1LY0mqRbb2OQPHUwexIu7qlbq3JmJxZ8VPMorr7zy8NZeeXhrrzw8vJtXHh4eHh7eU6c8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8fMj4kAQPDw8P76lTHh7ezSsPb+2Vh7f2yiv/7/mS8dHlo2d3dyz+CTAAYEXI+4xG4igAAAAASUVORK5CYII=" border=0 />

Make print_r() Pretty

August 11, 2010 1 comment

This is literally print_r() dressed up. I normally run this within my general $app object so to call it, I would use $app->debug($var). As with print_r(), it loops indefinitely using lists to format a readable layout in HTML. This helps the developer avoid having to view source and scroll…and scroll…and scroll to find the output of an array or object.

View Source

Error Handling

August 11, 2010 No comments yet

In the world of the Internet, as a developer, we need to protect ourselves from prying eyes. One of the easiest ways a hacker can crack your code is if you tell them what they did wrong. For that reason, it’s important to suppress errors from the public. But, this doesn’t mean we should eliminate the knowledge when an error occurs and what triggered it.  For that reason, I have written a fairly simple class that will allow for error suppression, error debugging and error reporting.

View Source

Database PHP Class

June 15, 2010 1 comment

I’m back with another chunk of code. This time, it’s my database class.

I got sick of coming across classes that didn’t have everything I needed, or didn’t do it correctly. One of my biggest problems was an inability to nest query calls within another query result’s output.

View Source

(UPDATED 08-10-2010) Forms Class for easy and consistant forms

March 31, 2010 No comments yet

UPDATED 08-10-2010:

  • I’ve cleaned up a lot of code and organized the code a little better.
  • One major improvement is the passing of element attributes. You can now pass an unlimited number of attributes. If there are any you want omitted from the actual element tag, you can add it to the $attributes_not_allowed array.
  • I’ve added notes everywhere and hopefully they will assist you. If you find any bugs, feel free to post in comments and will fix them ASAP

As it states in the notes of the script, this is not for auto-generated forms. This is simply a means of developing forms with consistency. There are methods for parsing the $_POST array and injecting the $_POST variables and values in the $form object. It will auto-generate the variables given in a SQL statement inject those into the $form object. It will also insert a posted form into a specified database and table.

This is a continual work in progress as I create forms under different conditions. Check back as I will update the code as needed

If you have any problems or suggestions, leave comments.

You will see references to the a $db object in the code below. Go to the Database PHP Class to view the code.

View Source