<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>mummey.org &#187; php</title>
	<atom:link href="http://www.mummey.org/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mummey.org</link>
	<description>Personal Website of Adrian Mummey</description>
	<lastBuildDate>Mon, 13 Sep 2010 18:12:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>YAML &#8211; My Favorite Non-Markup Language</title>
		<link>http://www.mummey.org/2009/04/yaml-my-favorite-non-markup-language/</link>
		<comments>http://www.mummey.org/2009/04/yaml-my-favorite-non-markup-language/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 12:02:53 +0000</pubDate>
		<dc:creator>Adrian Mummey</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[markup]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[yaml]]></category>

		<guid isPermaLink="false">http://www.mummey.org/?p=527</guid>
		<description><![CDATA[I have been using YAML for awhile and decided I’d like to write a post about it. YAML is an acronym for YAML Ain’t Markup Language and it is a very useful and concise serialization standard. I use YAML all the time in when I am coding PHP projects that require some kind of configuration files. ]]></description>
			<content:encoded><![CDATA[<p>I have been using <a href="http://www.yaml.org/" title="YAML" alt="YAML" target="_blank">YAML</a> for awhile and decided I&#8217;d like to write a post about it. YAML is an acronym for YAML Ain&#8217;t Markup Language and it is a very useful and concise serialization standard. I use YAML all the time in when I am coding PHP projects that require some kind of configuration files.
<div style="clear:both;"></div>
<p>Here is what <a href="http://en.wikipedia.org/wiki/YAML" title="wiki" alt="wiki" target="_blank">wiki</a> says about it:</p>
<blockquote><p>YAML syntax was designed to be easily mapped to data types common to most high-level languages: list, hash, and scalar. Its familiar indented outline and lean appearance makes it especially suited for tasks where humans are likely to view or edit data structures, such as configuration files, dumping during debugging, and document headers (e.g. the headers found on most e-mails are very close to YAML). Although well-suited for hierarchical data representation, it also has a compact syntax for a relational data as well.</p></blockquote>
<p>Here is a sample of what YAML looks like:</p>
<pre><code class="php">connection: "local_mysql"
widget:
  type: "Table"
drawProperties:
  title: "Syslog Table"
  showRowNumber: "true"
  allowHtml: "true"
  pageSize: null
  page: "disable"
#usually the table name
entityName: logs
entity:
  table: logs
  fields:
    id:
      field: id
      type: number
    counter:
      field: counter
      type: number
initialQuery: "select id, counter from logs order by id"</code></pre>
<p>One of the great things about YAML is that it is very easy to read and takes much less syntax markup than XML. This enables you to crank out tight, clean configuration files very easily. When I load the previous YAML file up into PHP, here is the resulting data structure:</p>
<pre><code class="php">array(6) {
  [&quot;connection&quot;]=&gt;
  string(11) &quot;local_mysql&quot;
  [&quot;widget&quot;]=&gt;
  array(1) {
    [&quot;type&quot;]=&gt;
    string(5) &quot;Table&quot;
  }
  [&quot;drawProperties&quot;]=&gt;
  array(4) {
    [&quot;title&quot;]=&gt;
    string(12) &quot;Syslog Table&quot;
    [&quot;showRowNumber&quot;]=&gt;
    string(4) &quot;true&quot;
    [&quot;allowHtml&quot;]=&gt;
    string(4) &quot;true&quot;
    [&quot;page&quot;]=&gt;
    string(7) &quot;disable&quot;
  }
  [&quot;entityName&quot;]=&gt;
  string(4) &quot;logs&quot;
  [&quot;entity&quot;]=&gt;
  array(2) {
    [&quot;table&quot;]=&gt;
    string(4) &quot;logs&quot;
    [&quot;fields&quot;]=&gt;
    array(2) {
      [&quot;id&quot;]=&gt;
      array(2) {
        [&quot;field&quot;]=&gt;
        string(2) &quot;id&quot;
        [&quot;type&quot;]=&gt;
        string(6) &quot;number&quot;
      }
      [&quot;counter&quot;]=&gt;
      array(2) {
        [&quot;field&quot;]=&gt;
        string(7) &quot;counter&quot;
        [&quot;type&quot;]=&gt;
        string(6) &quot;number&quot;
      }
    }
  }
  [&quot;initialQuery&quot;]=&gt;
  string(40) &quot;select id, counter from logs order by id&quot;
}</code></pre>
<p>The YAML file is actually parsed using a PHP YAML parser called <a href="http://spyc.sourceforge.net" title="Spyc" alt="Spyc" target="_blank">Spyc</a>. The syntax for Spyc is also quite simple:</p>
<pre><code class="php">$configArray = Spyc::YAMLLoad($yamlFile);</code></pre>
<p>I was first introduced to YAML files when I began a Symfony project. Symfony makes heavy use of YAML files for all of its configuration. I really like the way Symfony was so configurable and have since added YAML config files to just about every project I work on. They are far easier to edit than XML, there is less overhead when parsing and apparently the YAML 1.2 specification is compatible with JSON which is amazing.<br />
From <a href="http://en.wikipedia.org/wiki/JSON" title="wikipedia YAML JSON" alt="wikipedia" target="_blank">wikipedia</a>:</p>
<blockquote><p>Both functionally and syntactically, JSON is effectively a subset of YAML. Specifically, as of YAML version 1.2, &#8220;every JSON file is also a valid YAML file&#8221;</p></blockquote>
<p>Configuration files are a great way to make your life easier, and the life of anyone else who need to modify your code and YAML is a great place to start for creating these.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.mummey.org%2F2009%2F04%2Fyaml-my-favorite-non-markup-language%2F&amp;linkname=YAML%20%26%238211%3B%20My%20Favorite%20Non-Markup%20Language"><img src="http://www.mummey.org/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.mummey.org/2009/04/yaml-my-favorite-non-markup-language/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Finding a Category Ancestor / Descendant for WP Templates</title>
		<link>http://www.mummey.org/2009/03/finding-a-category-ancestor-descendant-wp-templates/</link>
		<comments>http://www.mummey.org/2009/03/finding-a-category-ancestor-descendant-wp-templates/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 06:01:02 +0000</pubDate>
		<dc:creator>Adrian Mummey</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[ancestor]]></category>
		<category><![CDATA[category]]></category>
		<category><![CDATA[children]]></category>
		<category><![CDATA[descendent]]></category>

		<guid isPermaLink="false">http://www.mummey.org/?p=365</guid>
		<description><![CDATA[Recently I had a problem where I needed to find out if a specific category was a descendant of another category, or if one category was a ancestor of another. The latest website I am working on has a top level category called &#8220;Trips we do&#8221; and below that are many sub-categories and sub-sub categories [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had a problem where I needed to find out if a specific category was a descendant of another category, or if one category was a ancestor of another. The latest website I am working on has a top level category called &#8220;Trips we do&#8221; and below that are many sub-categories and sub-sub categories etc. I wanted to use the same template on all of the categories that were descended from the &#8220;Trips we do&#8221;.<br />
Wordpress already has a function that returns the parent categories <a href="http://codex.wordpress.org/Template_Tags/get_category_parents">get_category_parents</a> but this returns a string more suited for breadcrumbs. I needed a yes or no answer that my category in question was in fact descended from some other arbitrary category. Here is the function I produced:</p>
<pre><code class="php">function is_ancestor_cat($ancestor, $descendant){
  $ancestor = (string) $ancestor;
  $desc_id = (string) $descendant;
  $child_cats = get_term_children( (string) $ancestor, &#39;category&#39;);
  if($ancestor == $descendant){
    return true;
  }
  else if(!count($child_cats)){
    return false;
  }
  else{
    $is_child = false;
    foreach($child_cats as $cat_id){
      if($cat_id == $desc_id){
        $is_child = true;
        break;
      }
      else{
        $is_child = $is_child || is_ancestor_cat($cat_id, $descendant);
      }
    }
    return $is_child;
  }
}</code></pre>
<p>What this code does is that is grabs all the children for the <em>ancestor</em> category then checks them against the <em>descendant</em> category, it will keep recursing until it hits the leaf nodes and returns a boolean value.<br />
Here it is in action in my template file <em>category.php</em>:</p>
<pre><code class="php">//Check if we are in a category
if(is_category()){
  $ancestor_cat_id = 43;
  //Lets get the current category we are in
  $cur_cat_id = get_cat_id( single_cat_title(&quot;&quot;,false) );
  $is_ancestor = false;
  //Check if we are in the ancestor category
  $is_ancestor = is_ancestor_cat($ancestor_cat_id, $cur_cat_id);

  //If it is an ancestor, use a specific template
  if($is_ancestor){
   include(TEMPLATEPATH.&#39;/category-43.php&#39;);
  }
  //Use the default template
  else{
   include(TEMPLATEPATH.&#39;/category-default.php&#39;);
  }

}</code></pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.mummey.org%2F2009%2F03%2Ffinding-a-category-ancestor-descendant-wp-templates%2F&amp;linkname=Finding%20a%20Category%20Ancestor%20%2F%20Descendant%20for%20WP%20Templates"><img src="http://www.mummey.org/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.mummey.org/2009/03/finding-a-category-ancestor-descendant-wp-templates/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Styling WordPress Dynamic Sidebar &#8211; dynamic_sidebar_params Filter</title>
		<link>http://www.mummey.org/2009/02/styling-wordpress-dynamic-sidebar-dynamic_sidebar_params-filter/</link>
		<comments>http://www.mummey.org/2009/02/styling-wordpress-dynamic-sidebar-dynamic_sidebar_params-filter/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 04:38:23 +0000</pubDate>
		<dc:creator>Adrian Mummey</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[dynamic sidebar]]></category>
		<category><![CDATA[dynamic_sidebar_params]]></category>
		<category><![CDATA[filter]]></category>

		<guid isPermaLink="false">http://www.mummey.org/?p=166</guid>
		<description><![CDATA[I have been working a lot lately on customizing my personal homepage theme. One thing I have not been happy about has been the right sidebar. The widget heading were just html text and didn&#8217;t have the rendered look that I wanted. I decided to reformat the sidebar and use images for the headings rather [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working a lot lately on customizing my personal homepage theme. One thing I have not been happy about has been the right sidebar. The widget heading were just html text and didn&#8217;t have the rendered look that I wanted. I decided to reformat the sidebar and use images for the headings rather than just text. I had done some custom sidebars before but I wasn&#8217;t sure how to style each widget independently. The problem is that WP gives a generic class to each sidebar widget and you are only able to style all of them to look the same.<br />
The first place I looked for a function to do this was in my template&#8217;s function.php file and I noticed this code:</p>
<pre><code class="php">
if ( function_exists(&#39;register_sidebar&#39;) )
    register_sidebar(array(
        &#39;before_widget&#39; =&gt; &#39;&lt;div class=&quot;sidebar-box&quot;&gt;&#39;,
    &#39;after_widget&#39; =&gt; &#39;&lt;/div&gt;&lt;/div&gt;&#39;,
 &#39;before_title&#39; =&gt; &#39;&lt;h2&gt;&lt;span&gt;&#39;,
        &#39;after_title&#39; =&gt; &#39;&lt;/span&gt;&lt;/h2&gt;&lt;div class=&quot;sidebar-box-inside&quot;&gt;&#39;,
    ));
</code></pre>
<p>This isn&#8217;t exacly what I wanted as it will apply this to all of my widgets. So after a little more snooping around the web I noticed a WP filter that does what I want. The fitler is called <em>dynamic_sidebar_params</em> and it is called like this:</p>
<pre><code class="php">
add_filter('dynamic_sidebar_params', 'my_function_callback');
</code></pre>
<p>Where <em>my_function_callback</em> is the name of your function. In my case I called it like this, in <em>functions.php</em>:</p>
<pre><code class="php">
add_filter('dynamic_sidebar_params', 'sidebar_styling');
</code></pre>
<p>Next I had to figure out a way to style each element uniquely. I decided that the widget name would be a good identifier. Luckily this filter will pass you a field called widget_id, which has a sanitized version of the name which can be used for your CSS identifier. Here is what the params variable looks like:</p>
<pre><code class="php">
Array
(
    [name] =&gt; Sidebar 1
    [id] =&gt; sidebar-1
    [before_widget] =&gt; &lt;div class=&quot;sidebar-box&quot;&gt;

    [after_widget] =&gt; &lt;/div&gt;&lt;/div&gt;
    [before_title] =&gt; &lt;h2&gt;&lt;span&gt;
    [after_title] =&gt; &lt;/span&gt;&lt;/h2&gt;&lt;div class=&quot;sidebar-box-inside&quot;&gt;
    [widget_id] =&gt; recent-posts
    [widget_name] =&gt; Recent Posts
)
</code></pre>
<p>Now that we have that ID, it is a simple function to write:</p>
<pre><code class="php">function sidebar_styling($params){
  $params[0][&#39;before_title&#39;] = &#39;&lt;h2 id=&quot;sidebar-&#39;.@$params[0][&#39;widget_id&#39;].&#39;&quot;&gt;&lt;span&gt;&#39;;
  return $params;
}</code></pre>
<p>Now WP will output the unique id for each heading and we can just style it appropriately. I used a big sprite to save resources:<br />
<div id="attachment_169" class="wp-caption aligncenter" style="width: 160px"><a href="http://www.mummey.org/wp-content/uploads/2009/02/dynamic-sidebar.png"><img src="http://www.mummey.org/wp-content/uploads/2009/02/dynamic-sidebar-150x150.png" alt="Dynamic Sidebar" title="dynamic-sidebar" width="150" height="150" class="size-thumbnail wp-image-169" /></a><p class="wp-caption-text">Dynamic Sidebar</p></div><br />
And then styled it like this, I am hiding the span so that we don&#8217;t see the text, but it will degrade nicely if you haven&#8217;t yet styled one of your widget classes.</p>
<pre><code class="css">
#sidebar-recent-posts span{display: none;}
#sidebar-recent-posts{
  background: transparent url(images/sidebar-titles/dynamic-sidebar.png) 0 0 no-repeat !important;
}
#sidebar-twitter-tools span{display: none;}
#sidebar-twitter-tools{
  background: transparent url(images/sidebar-titles/dynamic-sidebar.png) 0 -41px no-repeat !important;
}
#sidebar-categories span{display: none;}
#sidebar-categories{
  background: transparent url(images/sidebar-titles/dynamic-sidebar.png) 0 -82px no-repeat !important;
}
#sidebar-archives span{display: none;}
#sidebar-archives{
  background: transparent url(images/sidebar-titles/dynamic-sidebar.png) 0 -123px no-repeat !important;
}
#sidebar-links span{display: none;}
#sidebar-links{

  background: transparent url(images/sidebar-titles/dynamic-sidebar.png) 0 -164px no-repeat !important;
}
</code></pre>
<p>That is all there is, you can see the results on the page you are reading.<br />
I found these articles/sites helpful:</p>
<ul>
<li> <a href=" http://wordpress.org/support/topic/206194" title="Dynamic Widget Classes for use in CSS" alt="Dynamic Widget Classes for use in CSS">Dynamic Widget Classes for use in CSS</a></li>
</ul>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.mummey.org%2F2009%2F02%2Fstyling-wordpress-dynamic-sidebar-dynamic_sidebar_params-filter%2F&amp;linkname=Styling%20WordPress%20Dynamic%20Sidebar%20%26%238211%3B%20dynamic_sidebar_params%20Filter"><img src="http://www.mummey.org/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.mummey.org/2009/02/styling-wordpress-dynamic-sidebar-dynamic_sidebar_params-filter/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>WordPress &#8211; Use NextGen Gallery Code Inside an Excerpt</title>
		<link>http://www.mummey.org/2009/02/wordpress-use-nextgen-gallery-code-inside-an-excerpt/</link>
		<comments>http://www.mummey.org/2009/02/wordpress-use-nextgen-gallery-code-inside-an-excerpt/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 06:48:19 +0000</pubDate>
		<dc:creator>Adrian Mummey</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[excerpt]]></category>
		<category><![CDATA[gallery]]></category>
		<category><![CDATA[nextgen]]></category>

		<guid isPermaLink="false">http://www.mummey.org/?p=147</guid>
		<description><![CDATA[One problem with the ways that WordPress handles excerpt is that it always strips the HTML out. This is fine for most casual bloggers but when you are really trying to customize something for a client, it simply doesn&#8217;t cut it. What we wanted to do was use some of the NextGen Gallery markdown code [...]]]></description>
			<content:encoded><![CDATA[<p>One problem with the ways that WordPress handles excerpt is that it always strips the HTML out. This is fine for most casual bloggers but when you are really trying to customize something for a client, it simply doesn&#8217;t cut it. What we wanted to do was use some of the NextGen Gallery markdown code in the &#8220;Excerpt&#8221; box of the post and have WordPress render this in the category archive.</p>
<p>After some initial research I discovered that this markdown code is called &#8220;shortcode&#8221; in WP terminology. They have a whole <a href="http://codex.wordpress.org/Shortcode_API">Shortcode API</a>. Now it was just a matter of finding the correct functions to get the shortcode to render.</p>
<p>I wanted to create a special template for the category, essentially a <strong>photo blog</strong>, so that it would look different from the other categories. This is pretty straightforward, just create a new file called category-<em>id</em>.php, where <em>id</em> is the id number of your category. In my case it was category-40.php. The next step was to copy all the code from category.php and paste it into category-40.php.</p>
<p>When displaying the post&#8217;s excerpt, WordPress uses a function called <strong>the_excerpt()</strong>. Here it is in the default sandbox template:</p>
<pre><code class="php">
the_excerpt(''.__('Read More &lt;span class="meta-nav"&gt;&amp;raquo;&lt;/span&gt;', 'sandbox').'') ;
</code></pre>
<p>The problem with the_excerpt is that is echoes the excerpt directly to the screen. In order to return the excerpt value as a string we need to use <strong>get_the_excerpt()</strong>. Next after checking the shortcode API, I discovered a function that will run all the shortcodes on a given string and return the output as a string, <strong>do_shortcode()</strong>.</p>
<p>So all we need to do now is combine these two functions, and move it into the template:</p>
<pre><code>
echo do_shortcode(get_the_excerpt());
</code></pre>
<p>And there it is!</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.mummey.org%2F2009%2F02%2Fwordpress-use-nextgen-gallery-code-inside-an-excerpt%2F&amp;linkname=WordPress%20%26%238211%3B%20Use%20NextGen%20Gallery%20Code%20Inside%20an%20Excerpt"><img src="http://www.mummey.org/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.mummey.org/2009/02/wordpress-use-nextgen-gallery-code-inside-an-excerpt/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>cSprites &#8211; A Dynamic CSS Sprite Generator in PHP5</title>
		<link>http://www.mummey.org/2008/12/csprites-a-dynamic-css-sprite-generator-in-php5/</link>
		<comments>http://www.mummey.org/2008/12/csprites-a-dynamic-css-sprite-generator-in-php5/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 09:33:24 +0000</pubDate>
		<dc:creator>Adrian Mummey</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[sprite]]></category>

		<guid isPermaLink="false">http://www.mummey.org/?p=12</guid>
		<description><![CDATA[CSS Sprites &#8211; A Background The idea of combining images into a larger one and displaying this using CSS tricks has been around for awhile. There are a couple big benefits to doing this: First it reduces the number of HTTP requests that the browser has to make, by combining say 5 images into 1 [...]]]></description>
			<content:encoded><![CDATA[<h2>CSS Sprites &#8211; A Background</h2>
<p>The idea of combining images into a larger one and displaying this using CSS tricks has been around for <a href="http://www.alistapart.com/articles/sprites/">awhile</a>. There are a couple big benefits to doing this: First it reduces the number of HTTP requests that the browser has to make, by combining say 5 images into 1 big image, the number of image requests has been reduced by 4. This can <a href="http://css-tricks.com/css-sprites-what-they-are-why-theyre-cool-and-how-to-use-them/">increase the effeciency of page loading </a>and you can see gains in the speed at which your page is loaded. Second, when working with elements like rollover images, you can &#8220;preload&#8221; all of the states of the rollover and prevent flicker when users interact with these elements.</p>
<p>If used properly combining image elements in sprites can be very beneficial for web developers. However it is not without its problems. One of the most obvious issues is that the sprite needs to be generated before its component images can be used, <em>you need to know which images will be used beforehand.</em> This is fine for things like icons and buttons that won&#8217;t change very often, but is useless for any other images that will be displayed dynamically. Although it is good for very static elements like buttons and icons, if you need to make a change to any of these image elements, the entire sprite needs to be rebuilt, <em>the sprite AND CSS needs to be rebuilt to reflect any change to its component images</em>. Although there exist some scripts and websites that can handle this, it can still be a tedious process.</p>
<h2>cSprites &#8211; A New Way of Doing Things</h2>
<p>What cSprites aims to do is to solve these problems by generating CSS sprites on the fly. The general process of doing this consists of three steps:</p>
<ol>
<li>Tell cSprites which images you want to use</li>
<li>Process the sprites</li>
<li>Use cSprites functions to display the images and relevant CSS code</li>
</ol>
<p>Here is a sample of code that shows how these three steps are implemented:</p>
<pre><code class="html">
&lt;?php
//We need to include the main Sprite File which will setup the autoloading and config
require_once(&#39;Sprite.php&#39;);
Sprite::ppRegister(&#39;/images/image1.jpg&#39;);
Sprite::ppRegister(&#39;/images/image2.jpg&#39;);
Sprite::process(); //We MUST call the process function BEFORE we actually show any images
?&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=utf-8&quot;&gt;
        &lt;?php echo Sprite::getAllCssInclude(); /*This will output the complete css of all of the sprites*/ ?&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php echo Sprite::image_tag(&#39;/images/image1.jpg&#39;); /*outputs a pseudo img tag using css styles*/ ?&gt;
&lt;?php echo Sprite::image_tag(&#39;/images/image2.jpg&#39;); /*outputs a pseudo img tag using css styles*/ ?&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<h2>Get cSprites with SVN</h2>
<p><code class="simplecode">svn co https://csprites.svn.sourceforge.net/svnroot/csprites csprites </code></p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.mummey.org%2F2008%2F12%2Fcsprites-a-dynamic-css-sprite-generator-in-php5%2F&amp;linkname=cSprites%20%26%238211%3B%20A%20Dynamic%20CSS%20Sprite%20Generator%20in%20PHP5"><img src="http://www.mummey.org/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.mummey.org/2008/12/csprites-a-dynamic-css-sprite-generator-in-php5/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Transparent GIFs with PHP and GD</title>
		<link>http://www.mummey.org/2008/11/transparent-gifs-with-php-and-gd/</link>
		<comments>http://www.mummey.org/2008/11/transparent-gifs-with-php-and-gd/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 15:03:43 +0000</pubDate>
		<dc:creator>Adrian Mummey</dc:creator>
				<category><![CDATA[featured articles]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[GD]]></category>
		<category><![CDATA[GIF]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[imagecreatefromgif]]></category>
		<category><![CDATA[imagecreatetruecolor]]></category>
		<category><![CDATA[transparent gif]]></category>

		<guid isPermaLink="false">http://www.mummey.org/?p=5</guid>
		<description><![CDATA[During development of my cSprites application I ran across an interesting problem when generating my sprites with GD. When dealling with transparent GIFs, GD does not take care of processing by default you must enable some specific settings to get it done correctly. The default behavior is that imagecreatefromgif adds a black background to the [...]]]></description>
			<content:encoded><![CDATA[<p>During development of my cSprites application I ran across an interesting problem when generating my sprites with GD. When dealling with transparent GIFs, GD does not take care of processing by default you must enable some specific settings to get it done correctly. The default behavior is that imagecreatefromgif adds a black background to the transparent gif. The way I solved this was a lot of trial, error and research.<br />
<br class="children"/><br />
<img class="alignleft alignnone size-medium wp-image-6" style="float: left;" title="good-face" src="http://www.mummey.org/wp-content/uploads/2008/11/good-face.gif" alt="Original Image" width="128" height="128" /></p>
<p>Here is the original Image I used. Initially I thought I could just use the basic image copy functions in GD and it would take care of the rest. Well this isn&#8217;t the case. This is the code I initially tried. Essentially creating a true color image and just using imagecopy to copy the gifs into the sprite.<br/><br/><br/></p>
<pre><code class="php">
 protected static function handleGif($spriteImages, $imgSize, $filePath){
    $sprite = imagecreatetruecolor($imgSize-&gt;right, $imgSize-&gt;bottom);

    foreach($spriteImages as $spriteImage){
      $tempImage = imagecreatefromgif($spriteImage-&gt;getPath());
      $position = $spriteImage-&gt;getPosition();
      imagecopy($sprite, $tempImage, $position-&gt;left, $position-&gt;top, 0, 0, $spriteImage-&gt;getWidth(), $spriteImage-&gt;getHeight());
      imagedestroy($tempImage);
    }

    ob_start();
    imagegif($sprite);
    $spriteOutput = ob_get_clean();
    imagedestroy($sprite);
    SpriteImageWriter::writeImageFile($spriteOutput, $filePath);
  }
</code></pre>
<p><img class="alignleft alignnone size-medium wp-image-7" style="float: left;" title="bad-face1" src="http://www.mummey.org/wp-content/uploads/2008/11/bad-face1.gif" alt="First Try" width="115" height="115" /><br />
Here is the output from the above code. You will notice that GD scrapped the transparency and opted for a black background. Back to the drawing board I guess. The next chunk of code shows the next iteration. Basically allocating the &#8220;black&#8221; color (which most gifs use as transparent) and setting the newly created image to use that as the transparent color.</p>
<pre><code class="php">
protected static function handleGif($spriteImages, $imgSize, $filePath){
    $sprite = imagecreatetruecolor($imgSize-&gt;right, $imgSize-&gt;bottom);
    $trans_color = imagecolorallocatealpha($sprite, 0, 0, 0, 127);
    imagecolortransparent  ( $sprite, $transparent);

    foreach($spriteImages as $spriteImage){
      $tempImage = imagecreatefromgif($spriteImage-&gt;getPath());
      $position = $spriteImage-&gt;getPosition();
      imagecopy($sprite, $tempImage, $position-&gt;left, $position-&gt;top, 0, 0, $spriteImage-&gt;getWidth(), $spriteImage-&gt;getHeight());
      imagedestroy($tempImage);
    }

    ob_start();
    imagegif($sprite);
    $spriteOutput = ob_get_clean();
    imagedestroy($sprite);
    SpriteImageWriter::writeImageFile($spriteOutput, $filePath);
  }
</code></pre>
<p><img class="alignleft alignnone size-medium wp-image-8" style="float: left; margin-left: 10px; margin-right: 10px;" title="bad-face2" src="http://www.mummey.org/wp-content/uploads/2008/11/bad-face2.gif" alt="Second Try" width="115" height="118" />And this is the output from the above code. As you can see we now have some transparency, however big chunks of the image have been made transparent. Specifically where the color in the image matched &#8220;black&#8221;, it was replaced by a transparent color. So last try now. Here is the final code, which first involves setting some flags that tell it to maintain the transparency. Then create a transparent rectangle over the whole image and next set the image color to transparent. So a little more overhead however the results are great. </p>
<pre><code class="php">
protected static function handleGif($spriteImages, $imgSize, $filePath){
    $sprite = imagecreatetruecolor($imgSize-&gt;right, $imgSize-&gt;bottom);
    imagealphablending($sprite, false);
    imagesavealpha($sprite,true);
    $transparent = imagecolorallocatealpha($sprite, 255, 255, 255, 127);
    imagefilledrectangle($sprite, 0, 0, $imgSize-&gt;right, $imgSize-&gt;bottom, $transparent);
    imagecolortransparent  ( $sprite, $transparent);

    foreach($spriteImages as $spriteImage){
      $tempImage = imagecreatefromgif($spriteImage-&gt;getPath());
      $position = $spriteImage-&gt;getPosition();
      imagecopy($sprite, $tempImage, $position-&gt;left, $position-&gt;top, 0, 0, $spriteImage-&gt;getWidth(), $spriteImage-&gt;getHeight());
      imagedestroy($tempImage);
    }

    ob_start();
    imagegif($sprite);
    $spriteOutput = ob_get_clean();
    imagedestroy($sprite);
    SpriteImageWriter::writeImageFile($spriteOutput, $filePath);
  }
</code></pre>
<p>The above code will do the job of copying the transparent GIFs for my sprite generation. However one issue I ran into when generating my sprites is that when I had a lot of different GIFs in one sprite there was some small but noticeable dithering. I assume this is because while each individual GIF can use its own color pallete, when you combine a lot of them they can all only grab from the generated pallete.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.mummey.org%2F2008%2F11%2Ftransparent-gifs-with-php-and-gd%2F&amp;linkname=Transparent%20GIFs%20with%20PHP%20and%20GD"><img src="http://www.mummey.org/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.mummey.org/2008/11/transparent-gifs-with-php-and-gd/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

