<?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>CAM Web Design &#187; PHP</title>
	<atom:link href="http://camwebdesign.com/category/techniques/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://camwebdesign.com</link>
	<description>High quality, cost effective wed design and hosting solutions</description>
	<lastBuildDate>Mon, 12 Dec 2011 11:48:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Simplify the WordPress Admin sidebar navigation</title>
		<link>http://camwebdesign.com/techniques/simplify-the-wordpress-admin-sidebar-navigation/</link>
		<comments>http://camwebdesign.com/techniques/simplify-the-wordpress-admin-sidebar-navigation/#comments</comments>
		<pubDate>Mon, 24 Jan 2011 07:35:49 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://camwebdesign.com/?p=1648</guid>
		<description><![CDATA[Want to remove some of the root WordPress Admin sidebar navigation? We have a client that was mainly interested in writing pages and managing media for those pages, they were not going to use Posts, Comments or Links in the administration area. We set them up as Editors on a WordPress Multisite installation, below is <a href="http://camwebdesign.com/techniques/simplify-the-wordpress-admin-sidebar-navigation/" class="more-link">(more...)</a>]]></description>
			<content:encoded><![CDATA[<p>Want to remove some of the root WordPress Admin sidebar navigation?  We have a client that was mainly interested in writing pages and managing media for those pages, they were not going to use Posts, Comments or Links in the administration area.  We set them up as <a href="http://codex.wordpress.org/Roles_and_Capabilities#Editor">Editors</a> on a <a href="/techniques/wordpress/wordpress-multisite-on-hostgator-reseller/">WordPress Multisite</a> installation, below is how we removed those items from appearing in the navigation.</p>
<p>First we define a new function, we used <code>remove_admin_menu_items()</code>.<span id="more-1648"></span>  In this function we define an array <code>$remove_menu_items</code> that contains the menu items we wish to remove from the admin navigation.  The function is designed to step through WordPress&#8217; multidimensional array <code>$menu</code>, getting each <code>$menu</code> item name by use of the PHP function <a href="http://php.net/manual/en/function.explode.php"><code>explode()</code></a>.  It then checks if that <code>$menu</code> item name is in our <code>$remove_menu_items</code> array using the PHP function <a href="http://php.net/manual/en/function.in-array.php"><code>in_array()</code></a>.  If the <code>$menu</code> item name does appear, then that item is removed from the multidimensional array <code>$menu</code>, by use the PHP function <a href="http://php.net/manual/en/function.unset.php"><code>unset()</code></a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> remove_admin_menu_items<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$remove_menu_items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Posts'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Links'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comments'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$menu</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">end</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$menu</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$menu</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$item</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #000088;">$menu</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$menu</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span>?<span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$remove_menu_items</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$menu</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$menu</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We then initiate the <code>remove_admin_menu_items()</code> function using the <a href="http://codex.wordpress.org/Function_Reference/add_action"><code>add_action()</code></a> WordPress function as shown below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'admin_menu'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'remove_admin_menu_items'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Putting it all together yields the following code to be placed in your theme&#8217;s functions.php file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* Clean up the admin sidebar navigation *************************************************/</span>
<span style="color: #000000; font-weight: bold;">function</span> remove_admin_menu_items<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$remove_menu_items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Posts'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Links'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comments'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$menu</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">end</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$menu</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$menu</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$item</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #000088;">$menu</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$menu</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span>?<span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$remove_menu_items</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$menu</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$menu</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'admin_menu'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'remove_admin_menu_items'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>All you&#8217;ll need to change is the array of items to remove in the <cdoe>$remove_menu_items</code> array.  In our example we removed: Posts, Links and Comments.  Other root level WordPress admin sidebar navigation items available for removal are Media, Pages, Appearance, Plugins, Users, Tools and Settings.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/wordpress-3-0-menu-management/" rel="bookmark" title="July 9, 2010">WordPress 3.0 Menu Management</a></li>
<li><a href="http://camwebdesign.com/techniques/remove-widgets-from-twenty-ten-wordpress-theme/" rel="bookmark" title="January 22, 2011">Remove Widgets from Twenty Ten WordPress theme</a></li>
<li><a href="http://camwebdesign.com/techniques/wordpress-role-capability-restriction/" rel="bookmark" title="June 9, 2010">WordPress Role Capability Restriction</a></li>
</ul>
<p><!-- Similar Posts took 17.605 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/simplify-the-wordpress-admin-sidebar-navigation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Redefine Twenty Ten header image width and height</title>
		<link>http://camwebdesign.com/techniques/redefine-twenty-ten-header-image-width-and-height/</link>
		<comments>http://camwebdesign.com/techniques/redefine-twenty-ten-header-image-width-and-height/#comments</comments>
		<pubDate>Sun, 23 Jan 2011 05:59:24 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[Twenty Ten]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://camwebdesign.com/?p=1629</guid>
		<description><![CDATA[How do you go about redefining the WordPress Twenty Ten header image width and height? We recently used the Twenty Ten theme as a Parent Theme for a multi-site installation for a client. We wanted to retain the use of the custom header image, but wanted a different image width and height. It is accomplished <a href="http://camwebdesign.com/techniques/redefine-twenty-ten-header-image-width-and-height/" class="more-link">(more...)</a>]]></description>
			<content:encoded><![CDATA[<p>How do you go about redefining the WordPress Twenty Ten header image width and height?  We recently used the Twenty Ten theme as a Parent Theme for a multi-site installation for a client. We wanted to retain the use of the custom header image, but wanted a different image width and height.  It is accomplished with a few lines of code in the Twenty Ten <a href="http://codex.wordpress.org/Child_Themes">child theme</a>’s functions.php file.  <a href="http://camwebdesign.com/?p=1593">You are using a child theme, aren’t you?</a></p>
<p>The answer to this one is just so simple<span id="more-1629"></span> it needs nearly no explanation.  Simply redefine two varaibles <code>HEADER_IMAGE_WIDTH</code>, and <code>HEADER_IMAGE_HEIGHT</code>.  For our purposes we needed <code>800x182</code> to fit into the template.  Below is the code to be placed in your theme&#8217;s functions.php file simply add the image size to suite your needs:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* Redefine the header image width and height ********************************************/</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'HEADER_IMAGE_WIDTH'</span><span style="color: #339933;">,</span> apply_filters<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'twentyten_header_image_width'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">800</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'HEADER_IMAGE_HEIGHT'</span><span style="color: #339933;">,</span> apply_filters<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'twentyten_header_image_height'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">182</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>UPDATE: This also works for the WordPress Twenty Eleven theme.<br />
<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/remove-default-wordpresstwenty-ten-header-images/" rel="bookmark" title="January 23, 2011">Remove default WordPress Twenty Ten header images</a></li>
<li><a href="http://camwebdesign.com/techniques/create-a-wordpress-child-theme-allow-updates/" rel="bookmark" title="January 23, 2011">Create a WordPress Child Theme, allow updates</a></li>
<li><a href="http://camwebdesign.com/techniques/serve-watermarked-images-with-php/" rel="bookmark" title="August 15, 2010">Serve Watermarked Images with PHP</a></li>
</ul>
<p><!-- Similar Posts took 19.146 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/redefine-twenty-ten-header-image-width-and-height/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Remove default WordPress Twenty Ten header images</title>
		<link>http://camwebdesign.com/techniques/remove-default-wordpresstwenty-ten-header-images/</link>
		<comments>http://camwebdesign.com/techniques/remove-default-wordpresstwenty-ten-header-images/#comments</comments>
		<pubDate>Sun, 23 Jan 2011 05:44:30 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[Twenty Ten]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://camwebdesign.com/?p=1622</guid>
		<description><![CDATA[How do you remove those pesky default header images included in the Twenty Ten WordPress theme. We recently used the Twenty Ten theme as a Parent Theme for a multi-site installation for a client. We wanted to retain the use of the custom header image, but didn&#8217;t want the default header images to be visible. <a href="http://camwebdesign.com/techniques/remove-default-wordpresstwenty-ten-header-images/" class="more-link">(more...)</a>]]></description>
			<content:encoded><![CDATA[<p>How do you remove those pesky default header images included in the Twenty Ten WordPress theme.  We recently used the Twenty Ten theme as a Parent Theme for a multi-site installation for a client.  We wanted to retain the use of the custom header image, but didn&#8217;t want the default header images to be visible.  It is accomplished with a few lines of code in the Twenty Ten <a href="http://codex.wordpress.org/Child_Themes">child theme</a>’s functions.php file.  <a href="http://camwebdesign.com/?p=1593">You are using a child theme, aren’t you?</a></p>
<p>First define a new function, we used <code>remove_twenty_ten_headers()</code>.<span id="more-1622"></span>  We use the <a href="http://codex.wordpress.org/Function_Reference/unregister_default_headers"><code>unregister_default_headers()</code></a> WordPress function to un-register as many as we want.  In our example we removed them all.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> remove_twenty_ten_headers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    unregister_default_headers<span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'berries'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'cherryblossom'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'concave'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'fern'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'forestfloor'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'inkwell'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'path'</span> <span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'sunset'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We then initiate the new <code>remove_twenty_ten_headers()</code> function using the <a href="http://codex.wordpress.org/Function_Reference/add_action"><code>add_action()</code></a> WordPress function as shown below.  We make sure to call our function after the theme setup has run to be sure our function is called after <a href="http://codex.wordpress.org/Function_Reference/register_default_headers"><code>register_default_headers()</code></a> has already added the headers we want to remove.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">add_action<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'after_setup_theme'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'remove_twenty_ten_headers'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">11</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Putting it all together yields the following code to be placed in your theme&#8217;s functions.php file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* Remove default twenty ten images ***********************************************/</span>
<span style="color: #000000; font-weight: bold;">function</span> remove_twenty_ten_headers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    unregister_default_headers<span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'berries'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'cherryblossom'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'concave'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'fern'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'forestfloor'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'inkwell'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'path'</span> <span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'sunset'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_action<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'after_setup_theme'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'remove_twenty_ten_headers'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">11</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>How simple is that!<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/remove-widgets-from-twenty-ten-wordpress-theme/" rel="bookmark" title="January 22, 2011">Remove Widgets from Twenty Ten WordPress theme</a></li>
<li><a href="http://camwebdesign.com/techniques/simplify-the-wordpress-admin-sidebar-navigation/" rel="bookmark" title="January 24, 2011">Simplify the WordPress Admin sidebar navigation</a></li>
<li><a href="http://camwebdesign.com/techniques/wordpress-3-0-menu-management/" rel="bookmark" title="July 9, 2010">WordPress 3.0 Menu Management</a></li>
</ul>
<p><!-- Similar Posts took 18.496 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/remove-default-wordpresstwenty-ten-header-images/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Remove Widgets from Twenty Ten WordPress theme</title>
		<link>http://camwebdesign.com/techniques/remove-widgets-from-twenty-ten-wordpress-theme/</link>
		<comments>http://camwebdesign.com/techniques/remove-widgets-from-twenty-ten-wordpress-theme/#comments</comments>
		<pubDate>Sun, 23 Jan 2011 03:50:37 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[Twenty Ten]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://camwebdesign.com/?p=1585</guid>
		<description><![CDATA[So you want to remove some of the &#8220;stock&#8221; Widget Areas from the WordPress Twenty Ten theme, how do you go about doing this? We will be using the WordPress function unregister_sidebar(). It is accomplished with a few lines of code in your Twenty Ten child theme’s functions.php file. You are using a child theme, <a href="http://camwebdesign.com/techniques/remove-widgets-from-twenty-ten-wordpress-theme/" class="more-link">(more...)</a>]]></description>
			<content:encoded><![CDATA[<p>So you want to remove some of the &#8220;stock&#8221; Widget Areas from the WordPress Twenty Ten theme, how do you go about doing this?  We will be using the WordPress function <a href="http://codex.wordpress.org/Function_Reference/unregister_sidebar"><code>unregister_sidebar()</code></a>. It is accomplished with a few lines of code in your Twenty Ten <a href="http://codex.wordpress.org/Child_Themes">child theme</a>’s functions.php file.  <a href="http://camwebdesign.com/?p=1593">You are using a child theme, aren’t you?</a></p>
<p>First define a new function, we used <code>remove_widget_area()</code>.  <span id="more-1585"></span>Inside this function we un-register the desired &#8220;stock&#8221; Widget Areas we don&#8217;t want or need.  Twenty Ten comes with the following &#8220;stock&#8221; Widget Areas: primary-widget-area, secondary-widget-area, first-footer-widget-area, second-footer-widget-area, third-footer-widget-area and fourth-footer-widget-area.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> remove_widget_area<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	unregister_sidebar<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'secondary-widget-area'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	unregister_sidebar<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'first-footer-widget-area'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	unregister_sidebar<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'second-footer-widget-area'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	unregister_sidebar<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'third-footer-widget-area'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	unregister_sidebar<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'fourth-footer-widget-area'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We then initiate the new <code>remove_widget_area()</code> function using the <a href="http://codex.wordpress.org/Function_Reference/add_action"><code>add_action()</code></a> WordPress function as shown below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">add_action<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'admin_init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'remove_widget_area'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Putting it all together yields the following code to be placed in your theme&#8217;s functions.php file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* Remove &quot;extra&quot; Twenty Ten Widget Areas *******************************************************/</span>
<span style="color: #000000; font-weight: bold;">function</span> remove_widget_area<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	unregister_sidebar<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'secondary-widget-area'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	unregister_sidebar<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'first-footer-widget-area'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	unregister_sidebar<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'second-footer-widget-area'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	unregister_sidebar<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'third-footer-widget-area'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	unregister_sidebar<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'fourth-footer-widget-area'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_action<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'admin_init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'remove_widget_area'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>It&#8217;s just that simple, now it&#8217;s your turn!<br />
<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/remove-default-wordpresstwenty-ten-header-images/" rel="bookmark" title="January 23, 2011">Remove default WordPress Twenty Ten header images</a></li>
<li><a href="http://camwebdesign.com/techniques/wordpress-3-0-menu-management/" rel="bookmark" title="July 9, 2010">WordPress 3.0 Menu Management</a></li>
<li><a href="http://camwebdesign.com/techniques/simplify-the-wordpress-admin-sidebar-navigation/" rel="bookmark" title="January 24, 2011">Simplify the WordPress Admin sidebar navigation</a></li>
</ul>
<p><!-- Similar Posts took 17.788 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/remove-widgets-from-twenty-ten-wordpress-theme/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Serve Watermarked Images with PHP</title>
		<link>http://camwebdesign.com/techniques/serve-watermarked-images-with-php/</link>
		<comments>http://camwebdesign.com/techniques/serve-watermarked-images-with-php/#comments</comments>
		<pubDate>Sun, 15 Aug 2010 15:41:02 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Images]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Techniques]]></category>

		<guid isPermaLink="false">http://camwebdesign.com/?p=1301</guid>
		<description><![CDATA[So you want to watermark your images, how do you do this? In this technique we&#8217;ll show you how we serve our images with a watermark using PHP. We&#8217;ll be building on the Serve Resized Images with PHP tutorial we published previously, which explains basic re-size manipulation of your images. Stepping through the code, we <a href="http://camwebdesign.com/techniques/serve-watermarked-images-with-php/" class="more-link">(more...)</a>]]></description>
			<content:encoded><![CDATA[<p>So you want to watermark your images, how do you do this?  In this technique we&#8217;ll show you how we serve our images with a watermark using PHP.  We&#8217;ll be building on the <a href="http://camwebdesign.com/techniques/serve-resized-images-with-php/">Serve Resized Images with PHP</a> tutorial we published previously, which explains basic re-size manipulation of your images.</p>
<p>Stepping through the code, we first get our previously created watermark<span id="more-1301"></span> <img src="http://camwebdesign.com/images/copyright.png" alt="watermark" style="vertical-align:middle;" /> using the <a href="http://php.net/manual/en/function.imagecreatefrompng.php"><code>imagecreatefrompng()</code></a> PHP function, setting it to the variable $watermark for simplicity.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$watermark</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatefrompng</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'../images/watermark.png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Next we determine the watermark image&#8217;s width and height.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$watermark_width</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$watermark</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$watermark_height</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$watermark</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Since we want our watermark to appear in the bottom right of the original image, we&#8217;ll subtract the watermark image&#8217;s width and height from the original image&#8217;s width and height to determine it&#8217;s placement.  We also subtract 5 pixels for a padded placement.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dest_x</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$width</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$watermark_width</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dest_y</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$height</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$watermark_height</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Lastly we&#8217;ll actually apply the watermark image using the <a href="http://php.net/manual/en/function.imagecopymerge.php"><code>imagecopymerge()</code></a> PHP function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">imagecopymerge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #000088;">$watermark</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dest_x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dest_y</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$watermark_width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$watermark_height</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Putting the watermark code together we get the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//Set up Watermark</span>
<span style="color: #000088;">$watermark</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatefrompng</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'../images/watermark.png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$watermark_width</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$watermark</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$watermark_height</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$watermark</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dest_x</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$width</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$watermark_width</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dest_y</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$height</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$watermark_height</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Apply Watermark</span>
<span style="color: #990000;">imagecopymerge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #000088;">$watermark</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dest_x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dest_y</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$watermark_width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$watermark_height</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Adding the watermark code to our previous Serve Resized Images with PHP technique code we&#8217;ll have the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/path/to/include/functions.phtml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/images/&quot;</span> <span style="color: #339933;">.</span>  <span style="color: #990000;">urldecode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>img<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$img1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/nophoto.gif&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>is_image<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//Send proper image header base on mime type</span>
  <span style="color: #000088;">$mime</span><span style="color: #339933;">=</span>get_mime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$mime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Set dimensions, quality &amp; variables</span>
  <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>w<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//it's a thumbnail request don't preserve aspect</span>
    <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">150</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">150</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">;</span>
    <span style="color: #000088;">$height</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$width</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$thmb_quality</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">300</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">588</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">588</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">;</span>
    <span style="color: #000088;">$height</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$width</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$thmb_quality</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Get orig dimensions</span>
  <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width_orig</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height_orig</span><span style="color: #339933;">,</span> <span style="color: #000088;">$type_orig</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$src_x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$src_y</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//it's a thumbnail request, make it square from the center</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width_orig</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #000088;">$height_orig</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$src_y</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$height_orig</span><span style="color: #339933;">-</span><span style="color: #000088;">$width_orig</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$height_orig</span><span style="color: #339933;">=</span><span style="color: #000088;">$width_orig</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$src_x</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width_orig</span><span style="color: #339933;">-</span><span style="color: #000088;">$height_orig</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$width_orig</span><span style="color: #339933;">=</span><span style="color: #000088;">$height_orig</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//keep aspect ratio but keeping width set from page</span>
    <span style="color: #000088;">$height</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$width_orig</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$height_orig</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Resample</span>
  <span style="color: #000088;">$image</span><span style="color: #339933;">=</span>imagecreatefrom<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$im</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">imagecopyresampled</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$src_x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$src_y</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width_orig</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height_orig</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">/*BEGIN WATERMARK CODE**************************************************************/</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//Set up Watermark</span>
  <span style="color: #000088;">$watermark</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatefrompng</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'../images/watermark.png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$watermark_width</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$watermark</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$watermark_height</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$watermark</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$dest_x</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$width</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$watermark_width</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$dest_y</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$height</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$watermark_height</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//Apply Watermark</span>
  <span style="color: #990000;">imagecopymerge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #000088;">$watermark</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dest_x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dest_y</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$watermark_width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$watermark_height</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">/*END WATERMARK CODE****************************************************************/</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//Print Image</span>
  image_same_type<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #339933;">,</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span><span style="color: #000088;">$thmb_quality</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
  image_cache_type<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #339933;">,</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span><span style="color: #000088;">$image_path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now we simply call the image in a generic HTML image tag.  Be sure to use the alt portion of the image tag so that if the image generation fails text still appears, not to mention this will aid in SEO.  Below we are calling an image that has the default 588pixel width/height, keeping the aspect ratio, using an image named &#8220;uwcmc.jpg&#8221; for our <a href="http://camwebdesign.com/portfolio/united-way-of-cape-may-county/">United Way of Cape May County</a> client.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;img src=&quot;http://camwebdesign.com/layout/thumb.php?a=y&amp;img=uwcmc.jpg&quot; alt=&quot;UWCMC&quot; /&gt;</pre></td></tr></table></div>

<p><img src="http://camwebdesign.com/layout/thumb.php?a=y&#038;img=uwcmc.jpg" alt="UWCMC" class="contentimg" /><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/serve-resized-images-with-php/" rel="bookmark" title="January 11, 2010">Serve Resized Images with PHP</a></li>
<li><a href="http://camwebdesign.com/techniques/redefine-twenty-ten-header-image-width-and-height/" rel="bookmark" title="January 23, 2011">Redefine Twenty Ten header image width and height</a></li>
<li><a href="http://camwebdesign.com/techniques/php-image-manipulation-functions/" rel="bookmark" title="January 9, 2010">PHP Image Manipulation Functions</a></li>
</ul>
<p><!-- Similar Posts took 18.279 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/serve-watermarked-images-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Admin Link Shortcode for Thematic Footer Text</title>
		<link>http://camwebdesign.com/techniques/admin-link-shortcode-for-thematic-footer-text/</link>
		<comments>http://camwebdesign.com/techniques/admin-link-shortcode-for-thematic-footer-text/#comments</comments>
		<pubDate>Sun, 15 Aug 2010 12:07:03 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[Thematic]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://camwebdesign.com/?p=1252</guid>
		<description><![CDATA[Thematic comes with a variety of shortcodes that can be added to the footer text in the Thematic Options. We wanted to add the Admin link to the footer, but there is no predefined Thematic shortcode to do this. This technique shows how to create a custom Admin shortcode for Thematic. It is accomplished with <a href="http://camwebdesign.com/techniques/admin-link-shortcode-for-thematic-footer-text/" class="more-link">(more...)</a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://themeshaper.com/thematic/">Thematic</a> comes with a variety of <a href="http://en.support.wordpress.com/shortcodes/">shortcodes</a> that can be added to the footer text in the Thematic Options.  We wanted to add the Admin link to the footer, but there is no predefined Thematic shortcode to do this.  This technique shows how to create a custom Admin shortcode for Thematic.  It is accomplished with a few lines of code in your Thematic <a href="http://codex.wordpress.org/Child_Themes">child theme</a>’s functions.php file.  <a href="http://camwebdesign.com/?p=1593">You are using a child theme, aren’t you?</a></p>
<p>First define a new function, we used <code>thmfooter_admin_link()</code>.<span id="more-1252"></span>  We return the <a href="http://codex.wordpress.org/Function_Reference/wp_register"><code>wp_register()</code></a> WordPress function, to yield the Admin link.  Be sure to set the <code>wp_register() $echo</code> <a href="http://codex.wordpress.org/Function_Reference/wp_register#Parameters">parameter</a> to false, otherwise the Admin link will automatically print.  We&#8217;ll also set the <code>$before</code> and <code>$after</code> parameters as needed.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> thmfooter_admin_link<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> wp_register<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'| '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Lastly we register the shortcode, naming it &#8216;admin-link&#8217;, using the <a href="http://codex.wordpress.org/Function_Reference/add_shortcode"><code>add_shortcode()</code></a> WordPress function as shown below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">add_shortcode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'admin-link'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'thmfooter_admin_link'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Usage of our new shortcode is accomplished by simply adding [admin-link] in the footer text of the Thematic Options.</p>
<div id="attachment_1289" class="wp-caption alignnone" style="width: 580px"><img class="size-full wp-image-1289" title="Thematic Options" src="http://camwebdesign.com/~/wp-content/uploads/2010/08/thematic_options.jpg" alt="Thematic Options" width="570" height="320" /><p class="wp-caption-text">Thematic Options</p></div>
<p>Putting it all together, we have the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//Add Admin shortcode to the siteinfo footer***************************************************************************</span>
<span style="color: #000000; font-weight: bold;">function</span> thmfooter_admin_link<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> wp_register<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'| '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_shortcode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'admin-link'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'thmfooter_admin_link'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Yep, it&#8217;s just that simple.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/wordpress-3-0-menu-management/" rel="bookmark" title="July 9, 2010">WordPress 3.0 Menu Management</a></li>
<li><a href="http://camwebdesign.com/techniques/using-html5-with-your-thematic-child-theme/" rel="bookmark" title="August 14, 2010">Using HTML5 with your Thematic Child Theme</a></li>
<li><a href="http://camwebdesign.com/techniques/remove-default-wordpresstwenty-ten-header-images/" rel="bookmark" title="January 23, 2011">Remove default WordPress Twenty Ten header images</a></li>
</ul>
<p><!-- Similar Posts took 29.544 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/admin-link-shortcode-for-thematic-footer-text/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using HTML5 with your Thematic Child Theme</title>
		<link>http://camwebdesign.com/techniques/using-html5-with-your-thematic-child-theme/</link>
		<comments>http://camwebdesign.com/techniques/using-html5-with-your-thematic-child-theme/#comments</comments>
		<pubDate>Sun, 15 Aug 2010 02:32:38 +0000</pubDate>
		<dc:creator>Troy</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[Thematic]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://camwebdesign.com/?p=1180</guid>
		<description><![CDATA[In the interest of keeping our websites &#8220;future-proof&#8221;, I began investigating using the HTML5 DOCTYPE. As it turns out, the switch for most WordPress themes will be trivial: Change the doctype declaration to: &#60;&#33;DOCTYPE html&#62; Remove the (now defunct) profile="..." attribute from the opening &#60;head&#62; tag. Remove the unnecessary &#60;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&#62; tag. Add <a href="http://camwebdesign.com/techniques/using-html5-with-your-thematic-child-theme/" class="more-link">(more...)</a>]]></description>
			<content:encoded><![CDATA[<p>In the interest of keeping our websites &#8220;future-proof&#8221;, I began investigating using the HTML5 DOCTYPE.</p>
<p>As it turns out, the switch for most WordPress themes will be trivial: </p>
<ol>
<li>Change the doctype declaration to: <code>&lt;&#33;DOCTYPE html&gt;</code> </li>
<li>Remove the (now defunct) <code>profile="..."</code> attribute from the opening <code>&lt;head&gt;</code> tag.</li>
<li>Remove the unnecessary <code>&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;</code> tag.</li>
<li>Add the <code>&lt;meta charset="utf-8"&gt;</code> tag.</li>
</ol>
<p>Normally, this would be easily found in the <code>header.php</code> file of your theme.  In fact, the default WordPress theme (&#8216;<a href="http://2010dev.wordpress.com/">TwentyTen</a>&#8216; as of WP3) automatically uses the HTML5 doctype declaration.</p>
<p><a href="http://themeshaper.com/thematic/">Thematic</a>, however, uses the traditional &#8220;XHTML 1.0 Transitional&#8221; doctype<span id="more-1180"></span> that WordPress has used for years.  In order to use the HTML5 doctype, you will need to override four standard Thematic functions: <code>thematic_create_doctype</code>, <code>language_attributes</code>, <code>thematic_head_profile</code> and <code>thematic_create_contenttype</code> within your <a href="http://codex.wordpress.org/Child_Themes">child theme&#8217;s</a> <code>functions.php</code> file (<a href="http://camwebdesign.com/?p=1593">you are using a child theme, aren’t you?</a>), thusly:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//replace the standard doctype declaration and html tag opening...</span>
<span style="color: #000000; font-weight: bold;">function</span> html5_create_doctype<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;!DOCTYPE html&gt;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;html&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'thematic_create_doctype'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'html5_create_doctype'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//replace the lang attribute in the opening &lt;html&gt; tag...</span>
<span style="color: #000000; font-weight: bold;">function</span> html5_language_attributes<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;lang=<span style="color: #000099; font-weight: bold;">\&quot;</span>en<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'language_attributes'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'html5_language_attributes'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//replace the &lt;head&gt; tag opening to remove the now defunct profile attribute and add the &lt;meta charset=&quot;utf-8&quot;&gt; tag...</span>
<span style="color: #000000; font-weight: bold;">function</span> html5_head<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;head&gt;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;meta charset=<span style="color: #000099; font-weight: bold;">\&quot;</span>utf-8<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$content</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'thematic_head_profile'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'html5_head'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//remove the now defunct &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt; tag...</span>
<span style="color: #000000; font-weight: bold;">function</span> html5_create_charset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'thematic_create_contenttype'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'html5_create_charset'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/create-a-wordpress-child-theme-allow-updates/" rel="bookmark" title="January 23, 2011">Create a WordPress Child Theme, allow updates</a></li>
<li><a href="http://camwebdesign.com/techniques/redefine-twenty-ten-header-image-width-and-height/" rel="bookmark" title="January 23, 2011">Redefine Twenty Ten header image width and height</a></li>
<li><a href="http://camwebdesign.com/techniques/remove-default-wordpresstwenty-ten-header-images/" rel="bookmark" title="January 23, 2011">Remove default WordPress Twenty Ten header images</a></li>
</ul>
<p><!-- Similar Posts took 35.244 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/using-html5-with-your-thematic-child-theme/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WordPress 3.0 Menu Management</title>
		<link>http://camwebdesign.com/techniques/wordpress-3-0-menu-management/</link>
		<comments>http://camwebdesign.com/techniques/wordpress-3-0-menu-management/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 08:28:02 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://camwebdesign.com/?p=1126</guid>
		<description><![CDATA[One of the best all-around additions to WordPress 3.0 is custom Menu management. So how do you implement custom menus into your WordPress site? Simply add a few lines of code in your theme&#8217;s functions.php file, then add the menus where desired in your theme. Yes, it requires adding php code to your theme&#8217;s files, <a href="http://camwebdesign.com/techniques/wordpress-3-0-menu-management/" class="more-link">(more...)</a>]]></description>
			<content:encoded><![CDATA[<p>One of the best all-around <a href="http://codex.wordpress.org/Version_3.0#Highlights">additions to WordPress 3.0</a> is <a href="http://codex.wordpress.org/Appearance_Menus_SubPanel">custom Menu management</a>.  So how do you implement custom menus into your WordPress site?  Simply add a few lines of code in your theme&#8217;s <a href="http://codex.wordpress.org/Theme_Development#Functions_File">functions.php</a> file, then add the menus where desired in your theme.  Yes, it requires adding php code to your theme&#8217;s files, but it&#8217;s simple and easy to understand.</p>
<p>First we register the ability for your theme to use custom menus using the <a href="http://codex.wordpress.org/Function_Reference/add_theme_support"><code>add_theme_support()</code></a> WordPress function.<span id="more-1126"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">add_theme_support<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'menus'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Next we define the custom menus.  For this example we&#8217;ll create two menus: menu-1 and menu-2, and name them &#8220;Main Navigation&#8217; and &#8216;Footer Navigation&#8217; respectively.  We accomplish this in a new function &#8216;register_site_menus&#8217;, using the <a href="http://codex.wordpress.org/Function_Reference/register_nav_menus"><code>register_nav_menus()</code></a> WordPress function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> register_site_menus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	register_nav_menus<span style="color: #009900;">&#40;</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'menu-1'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Main Navigation'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'menu-2'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Footer Navigation'</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We then initiate the new <code>register_site_menus()</code> function using the <a href="http://codex.wordpress.org/Function_Reference/add_action"><code>add_actions()</code></a> WordPress function as shown below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">add_action<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'register_site_menus'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Putting it all together yields the following code to be placed in your theme&#8217;s functions.php file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">add_theme_support<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'menus'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> register_site_menus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	register_nav_menus<span style="color: #009900;">&#40;</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'menu-1'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Main Navigation'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'menu-2'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Footer Navigation'</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
add_action<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'register_site_menus'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Next we add &#8216;menu-1&#8242; our &#8216;Main Navigation&#8217; menu, using the <a href="http://codex.wordpress.org/Function_Reference/wp_nav_menu"><code>wp_nav_menu()</code></a> WordPress function where we want that menu to appear in your theme (header, sidebar, etc.).  We also chose to not have a container automatically added to our menu for this example below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">wp_nav_menu<span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> 
	<span style="color: #0000ff;">'theme_location'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'menu-1'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'container'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span>
<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We&#8217;ll add &#8216;menu-2&#8242; our &#8216;Footer Navigation&#8217; menu, using the same function but, we also give the menu a custom CSS id of &#8216;footer-nav&#8217;.  You can read all about the <a href="http://codex.wordpress.org/Function_Reference/wp_nav_menu#Parameters">parameters of the <code>wp_nav_menu()</code> function</a> yourself rather than bore you with the details here.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">wp_nav_menu<span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'theme_location'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'menu-2'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'container'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'menu_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'footer-nav'</span>
<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/simplify-the-wordpress-admin-sidebar-navigation/" rel="bookmark" title="January 24, 2011">Simplify the WordPress Admin sidebar navigation</a></li>
<li><a href="http://camwebdesign.com/techniques/admin-link-shortcode-for-thematic-footer-text/" rel="bookmark" title="August 15, 2010">Admin Link Shortcode for Thematic Footer Text</a></li>
<li><a href="http://camwebdesign.com/techniques/remove-widgets-from-twenty-ten-wordpress-theme/" rel="bookmark" title="January 22, 2011">Remove Widgets from Twenty Ten WordPress theme</a></li>
</ul>
<p><!-- Similar Posts took 19.019 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/wordpress-3-0-menu-management/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Create Custom Roles and Capabilities</title>
		<link>http://camwebdesign.com/techniques/wordpress-create-custom-roles-and-capabilities/</link>
		<comments>http://camwebdesign.com/techniques/wordpress-create-custom-roles-and-capabilities/#comments</comments>
		<pubDate>Sat, 12 Jun 2010 01:17:08 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://camwebdesign.com/?p=1166</guid>
		<description><![CDATA[So you want to add a custom Role to your WordPress site to add more flexibility when assigning users their role, what do you do? Simply add a role, with a few lines of code in your theme&#8217;s functions.php file, using WordPress&#8217; add_role() function, as defined below. 1 add_role&#40; $role_name, $display_name, $capabilities &#41;; The example <a href="http://camwebdesign.com/techniques/wordpress-create-custom-roles-and-capabilities/" class="more-link">(more...)</a>]]></description>
			<content:encoded><![CDATA[<p>So you want to add a custom <a href="http://codex.wordpress.org/Roles_and_Capabilities">Role</a> to your WordPress site to add more flexibility when assigning users their role, what do you do? Simply add a role, with a few lines of code in your theme&#8217;s <a href="http://codex.wordpress.org/Theme_Development#Functions_File">functions.php</a> file, using WordPress&#8217; <a href="http://codex.wordpress.org/Function_Reference/add_role"><code>add_role()</code></a> function, as defined below.<span id="more-1166"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">add_role<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$role_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$display_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$capabilities</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The example below adds a role of Video Manager with a custom cabability of &#8216;manage videos&#8217;.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">add_role<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'video_manager, '</span>Video Manager<span style="color: #0000ff;">', array( '</span>manage_videos<span style="color: #0000ff;">' ) );</span></pre></td></tr></table></div>

<h3>NOTE: Once you add this code to your functions.php file and view any page on your site to make the role addition, you can and should comment it out so that it doesn&#8217;t run on every page view.</h3>
<p>Now, how do we use this new role with the new capability you ask?  Simply use the WordPress <code>current_user_can()</code> function to check the user&#8217;s capability.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> current_user_can<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'manage_videos'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// let them manage those videos!</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This technique can be combined with our <a href="http://camwebdesign.com/techniques/wordpress-role-capability-restriction/">WordPress Role Capability Restriction technique</a> to add even more capabilities to your new custom role.  Simply create your new role then use it in the <code>get_role()</code> function call detailed in our technique.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/wordpress-role-capability-restriction/" rel="bookmark" title="June 9, 2010">WordPress Role Capability Restriction</a></li>
<li><a href="http://camwebdesign.com/techniques/wordpress-3-0-menu-management/" rel="bookmark" title="July 9, 2010">WordPress 3.0 Menu Management</a></li>
<li><a href="http://camwebdesign.com/techniques/admin-link-shortcode-for-thematic-footer-text/" rel="bookmark" title="August 15, 2010">Admin Link Shortcode for Thematic Footer Text</a></li>
</ul>
<p><!-- Similar Posts took 18.019 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/wordpress-create-custom-roles-and-capabilities/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>WordPress Role Capability Restriction</title>
		<link>http://camwebdesign.com/techniques/wordpress-role-capability-restriction/</link>
		<comments>http://camwebdesign.com/techniques/wordpress-role-capability-restriction/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 06:59:23 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://camwebdesign.com/?p=1089</guid>
		<description><![CDATA[So you want to restrict user capabilities of a role on your WordPress site from editing and deleting other user&#8217;s pages and posts, but you can&#8217;t seem to find an easy-to-use plugin that meets your needs, what do you do? Simply restrict that role, with a few lines of code in your theme&#8217;s functions.php file, <a href="http://camwebdesign.com/techniques/wordpress-role-capability-restriction/" class="more-link">(more...)</a>]]></description>
			<content:encoded><![CDATA[<p>So you want to restrict user <a href="http://codex.wordpress.org/Roles_and_Capabilities">capabilities of a role</a> on your WordPress site from editing and deleting other user&#8217;s pages and posts, but you can&#8217;t seem to find an easy-to-use plugin that meets your needs, what do you do?  Simply restrict that role, with a few lines of code in your theme&#8217;s <a href="http://codex.wordpress.org/Theme_Development#Functions_File">functions.php</a> file, using WordPress&#8217; <code>get_role()</code> and <code>remove_cap()</code> functions.  The example below is how to restrict the <a href="http://codex.wordpress.org/Roles_and_Capabilities#Author">author role</a>.<span id="more-1089"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$role</span> <span style="color: #339933;">=</span> get_role<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'author'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$role</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remove_cap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'edit_others_pages'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$role</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remove_cap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'edit_others_posts'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$role</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remove_cap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'delete_others_pages'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$role</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remove_cap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'delete_others_posts'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Pretty simple huh?  That&#8217;s what I thought when I learned how easily this can be accomplished.</p>
<p>So what&#8217;s actually going on with the code?  First, we create a variable $role populated with the author&#8217;s capabilities.  Then we remove the ability of the authors to &#8216;edit_others_pages&#8217;, and <a href="http://codex.wordpress.org/Roles_and_Capabilities#Capabilities">other capabilities</a> as desired.  This code performs a database change to the role.</p>
<h3>NOTE: Once you add this code to your functions.php file and view any page on your site to make the capability changes, you can and should comment it out so that it doesn&#8217;t run on every page view.</h3>
<p>How do you reverse this you may ask?  Simple, use WordPress&#8217; <code>add_cap()</code> function, as if WordPress would let us down.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$role</span> <span style="color: #339933;">=</span> get_role<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'author'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$role</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add_cap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'edit_others_pages'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$role</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add_cap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'edit_others_posts'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$role</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add_cap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'delete_others_pages'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$role</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add_cap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'delete_others_posts'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This works great when you&#8217;ve <a href="http://camwebdesign.com/techniques/wordpress-create-custom-role-and-capabilities/">Created Custom Roles</a> and need to assign capabilities to those new roles.</p>
<p>Below is real-world code we&#8217;ve used on a site, and how we left it commented for future reference.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* Begin Editor Restriction  ****************/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Enable Restriction Below */</span>
<span style="color: #666666; font-style: italic;">//$role = get_role('editor');</span>
<span style="color: #666666; font-style: italic;">//$role-&gt;remove_cap('edit_others_pages');</span>
<span style="color: #666666; font-style: italic;">//$role-&gt;remove_cap('edit_others_posts');</span>
<span style="color: #666666; font-style: italic;">//$role-&gt;remove_cap('delete_others_pages');</span>
<span style="color: #666666; font-style: italic;">//$role-&gt;remove_cap('delete_others_posts');</span>
<span style="color: #666666; font-style: italic;">//$role-&gt;remove_cap('manage_links');</span>
<span style="color: #666666; font-style: italic;">//$role-&gt;remove_cap('moderate_comments');</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Disable Restriction Below */</span>
<span style="color: #666666; font-style: italic;">//$role = get_role('editor');</span>
<span style="color: #666666; font-style: italic;">//$role-&gt;add_cap('edit_others_pages');</span>
<span style="color: #666666; font-style: italic;">//$role-&gt;add_cap('edit_others_posts');</span>
<span style="color: #666666; font-style: italic;">//$role-&gt;add_cap('delete_others_pages');</span>
<span style="color: #666666; font-style: italic;">//$role-&gt;add_cap('delete_others_posts');</span>
<span style="color: #666666; font-style: italic;">//$role-&gt;add_cap('manage_links');</span>
<span style="color: #666666; font-style: italic;">//$role-&gt;add_cap('moderate_comments');</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* End Editor Restriction  ***************/</span></pre></td></tr></table></div>

<p><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/wordpress-create-custom-roles-and-capabilities/" rel="bookmark" title="June 11, 2010">WordPress Create Custom Roles and Capabilities</a></li>
<li><a href="http://camwebdesign.com/techniques/simplify-the-wordpress-admin-sidebar-navigation/" rel="bookmark" title="January 24, 2011">Simplify the WordPress Admin sidebar navigation</a></li>
<li><a href="http://camwebdesign.com/techniques/what-wordpress-plugins-do-we-use/" rel="bookmark" title="January 3, 2010">What WordPress plugins do we use?</a></li>
</ul>
<p><!-- Similar Posts took 17.807 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/wordpress-role-capability-restriction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Serve Resized Images with PHP</title>
		<link>http://camwebdesign.com/techniques/serve-resized-images-with-php/</link>
		<comments>http://camwebdesign.com/techniques/serve-resized-images-with-php/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 19:33:00 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Images]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Techniques]]></category>

		<guid isPermaLink="false">http://camwebdesign.com/?p=605</guid>
		<description><![CDATA[There are many reasons we use PHP to serve images on sites including: resizing, watermarking, skewing, server-caching, etc. This will cover the basics of serving and resizing an image with PHP. This technique will work with JPG, JPEG, GIF &#038; PNG image types. We&#8217;ll cover more advanced image manipulation techniques in later posts. The first <a href="http://camwebdesign.com/techniques/serve-resized-images-with-php/" class="more-link">(more...)</a>]]></description>
			<content:encoded><![CDATA[<p>There are many reasons we use PHP to serve images on sites including: resizing, <a href="http://camwebdesign.com/techniques/serve-watermarked-images-with-php/">watermarking</a>, skewing, server-caching, etc.  This will cover the basics of serving and resizing an image with PHP.  This technique will work with JPG, JPEG, GIF &#038; PNG image types.  We&#8217;ll cover more advanced image manipulation techniques in later posts.</p>
<p>The first line of code calls the functions.phtml<span id="more-605"></span> include we defined in our <a href="http://camwebdesign.com/techniques/php-image-manipulation-functions/">PHP Image Manipulation Functions</a> post, change the &#8220;/path/to/include/&#8221; to your path.  The second line <a href="http://php.net/manual/en/reserved.variables.get.php"><code>$_GET</code></a>s the image file from the URL we want to manipulate, and assigns it to the $img1 variable.  The third line checks if this requested image is a file, otherwise we use a generic <em>&#8220;No Photo&#8221;</em> image to prevent no image from displaying.  The fourth line checks if the requested file is an image and surrounds the remaining code with the if statement.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/path/to/include/functions.phtml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/images/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">urldecode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>img<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$img1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/images/nophoto.gif&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>is_image<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
&nbsp;
   <span style="color: #666666; font-style: italic;">/*** The rest of the Code ***/</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Next part of the code gets the MIME type of the image file and calls the proper image header based on that MIME type.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">//Send proper image header base on mime type</span>
  <span style="color: #000088;">$mime</span><span style="color: #339933;">=</span>get_mime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$mime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>With the following code we determine the final image&#8217;s width and height.  Based on the presence of <code>$_GET[a]</code> variable in the URL used to represent aspect, our code: makes a smaller image with the aspect ratio kept intact, otherwise it makes a thumbnail with equal width and height starting from the image&#8217;s center.  For thumbnails a width of 100 pixels is default, if we do not specify a width, with a max of 178 pixels.  For non-thumbnails (with aspect preserved) a default width of 300 pixels is set with a maximum of 588 pixels.  These numbers can be changed, we just set them to work within our site&#8217;s layout.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">// Set dimensions, quality &amp; variables</span>
  <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>w<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//it's a thumbnail request don't preserve aspect</span>
    <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">178</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">178</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">;</span>
    <span style="color: #000088;">$height</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$width</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$thmb_quality</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">300</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">588</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">588</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">;</span>
    <span style="color: #000088;">$height</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$width</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$thmb_quality</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We then determine the original image&#8217;s width and height.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">// Get orig dimensions</span>
  <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width_orig</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height_orig</span><span style="color: #339933;">,</span> <span style="color: #000088;">$type_orig</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$src_x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$src_y</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>With the following code we determine the final image&#8217;s width and height.  Our code makes thumbnails with equal with and height from an image&#8217;s center, or a smaller image with the aspect ratio kept intact all based on the presence of <code>$_GET[a]</code> variable in the URL.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//it's a thumbnail request, make it square from the center</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width_orig</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$height_orig</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$src_y</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$height_orig</span><span style="color: #339933;">-</span><span style="color: #000088;">$width_orig</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$height_orig</span><span style="color: #339933;">=</span><span style="color: #000088;">$width_orig</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$src_x</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width_orig</span><span style="color: #339933;">-</span><span style="color: #000088;">$height_orig</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$width_orig</span><span style="color: #339933;">=</span><span style="color: #000088;">$height_orig</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//keep aspect ratio but keeping width set from page</span>
    <span style="color: #000088;">$height</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$width_orig</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$height_orig</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</pre>
<p>Finally we re-sample then print the image with PHP.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">// Resample</span>
  <span style="color: #000088;">$image</span><span style="color: #339933;">=</span>imagecreatefrom<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$im</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">imagecopyresampled</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$src_x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$src_y</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width_orig</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height_orig</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//Print Image</span>
  image_same_type<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #339933;">,</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span><span style="color: #000088;">$thmb_quality</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
  image_cache_type<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #339933;">,</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span><span style="color: #000088;">$image_path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<hr />
<br />
Putting it all together we arrive at the following code, we named our PHP file thumb.php since it's primary role is to make thumbnails for the site.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/path/to/include/functions.phtml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/images/&quot;</span> <span style="color: #339933;">.</span>  <span style="color: #990000;">urldecode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>img<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$img1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/nophoto.gif&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>is_image<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//Send proper image header base on mime type</span>
  <span style="color: #000088;">$mime</span><span style="color: #339933;">=</span>get_mime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$mime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Set dimensions, quality &amp; variables</span>
  <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>w<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//it's a thumbnail request don't preserve aspect</span>
    <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">150</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">150</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">;</span>
    <span style="color: #000088;">$height</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$width</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$thmb_quality</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">300</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">588</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">588</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">;</span>
    <span style="color: #000088;">$height</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$width</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$thmb_quality</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Get orig dimensions</span>
  <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width_orig</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height_orig</span><span style="color: #339933;">,</span> <span style="color: #000088;">$type_orig</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$src_x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$src_y</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//it's a thumbnail request, make it square from the center</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width_orig</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #000088;">$height_orig</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$src_y</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$height_orig</span><span style="color: #339933;">-</span><span style="color: #000088;">$width_orig</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$height_orig</span><span style="color: #339933;">=</span><span style="color: #000088;">$width_orig</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$src_x</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width_orig</span><span style="color: #339933;">-</span><span style="color: #000088;">$height_orig</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$width_orig</span><span style="color: #339933;">=</span><span style="color: #000088;">$height_orig</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//keep aspect ratio but keeping width set from page</span>
    <span style="color: #000088;">$height</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$width_orig</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$height_orig</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Resample</span>
  <span style="color: #000088;">$image</span><span style="color: #339933;">=</span>imagecreatefrom<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$im</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">imagecopyresampled</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$src_x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$src_y</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width_orig</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height_orig</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//Print Image</span>
  image_same_type<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #339933;">,</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span><span style="color: #000088;">$thmb_quality</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
  image_cache_type<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img1</span><span style="color: #339933;">,</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span><span style="color: #000088;">$image_path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now we simply call the image in a generic HTML image tag.  Be sure to use the alt portion of the image tag so that if the image generation fails text still appears, not to mention this will aid in SEO.  Below we are calling an image that has a max 200pixel width/height, keeping the aspect ratio, using an image named "uwcmc.jpg" for our <a href="http://camwebdesign.com/portfolio/united-way-of-cape-may-county/">United Way of Cape May County</a> client.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;img src=&quot;http://camwebdesign.com/layout/thumb.php?w=200&amp;a=y&amp;img=uwcmc.jpg&quot; alt=&quot;UWCMC&quot; /&gt;</pre></td></tr></table></div>

<p><img src="http://camwebdesign.com/layout/thumb.php?w=200&#038;a=y&#038;img=uwcmc.jpg" alt="UWCMC" /></p>
<p>
Below we are calling an image without specifying width or aspect ration, again using an image named "uwcmc.jpg".</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;img src=&quot;http://camwebdesign.com/layout/thumb.php?img=uwcmc.jpg&quot; alt=&quot;UWCMC&quot; /&gt;</pre></td></tr></table></div>

<p><img src="http://camwebdesign.com/layout/thumb.php?img=uwcmc.jpg" alt="UWCMC" /><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/serve-watermarked-images-with-php/" rel="bookmark" title="August 15, 2010">Serve Watermarked Images with PHP</a></li>
<li><a href="http://camwebdesign.com/techniques/redefine-twenty-ten-header-image-width-and-height/" rel="bookmark" title="January 23, 2011">Redefine Twenty Ten header image width and height</a></li>
<li><a href="http://camwebdesign.com/techniques/php-image-manipulation-functions/" rel="bookmark" title="January 9, 2010">PHP Image Manipulation Functions</a></li>
</ul>
<p><!-- Similar Posts took 18.634 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/serve-resized-images-with-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Image Manipulation Functions</title>
		<link>http://camwebdesign.com/techniques/php-image-manipulation-functions/</link>
		<comments>http://camwebdesign.com/techniques/php-image-manipulation-functions/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 20:14:04 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Images]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Techniques]]></category>

		<guid isPermaLink="false">http://camwebdesign.com/?p=631</guid>
		<description><![CDATA[Lets define some PHP functions we&#8217;ll be calling on for our PHP image tutorial series. We define all these functions in a file named functions.phtml and call it in our PHP code through a PHP include_once(), this way we don&#8217;t bother with the functions in our PHP image code. This first function allows us to <a href="http://camwebdesign.com/techniques/php-image-manipulation-functions/" class="more-link">(more...)</a>]]></description>
			<content:encoded><![CDATA[<p>Lets define some PHP functions we&#8217;ll be calling on for our <a href="/category/techniques/php/images/">PHP image tutorial series</a>.  We define all these functions in a file named functions.phtml and call it in our PHP code through a PHP <a href="http://php.net/manual/en/function.include-once.php"><code>include_once()</code></a>, this way we don&#8217;t bother with the functions in our PHP image code.</p>
<p>This first function allows us to determine if the requested image file is an image <code>is_image()</code><span id="more-631"></span>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*Define is_image() ****************************************************************************************************/</span>
<span style="color: #000000; font-weight: bold;">function</span> is_image<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$t</span><span style="color: #339933;">=</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var1</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$t2</span><span style="color: #339933;">=</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var1</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$t</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;GIF&quot;</span> <span style="color: #339933;">||</span><span style="color: #000088;">$t</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;PNG&quot;</span> <span style="color: #339933;">||</span><span style="color: #000088;">$t</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;JPG&quot;</span> <span style="color: #339933;">||</span><span style="color: #000088;">$t2</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;JPEG&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This function returns the extension for any file <code>getExtension().</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*Define getExtension() ****************************************************************************************************/</span>
<span style="color: #000000; font-weight: bold;">function</span> getExtension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$photosubmitted</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Error: Unknown Filetype'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pos</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$str</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>><br />
The next function allows us to call the proper <a href="http://en.wikipedia.org/wiki/MIME#Content-Type">MIME type</a>, based on image type, in the header of the PHP code <code>get_mime()</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*Define get_mime() ****************************************************************************************************/</span>
<span style="color: #000000; font-weight: bold;">function</span> get_mime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$t</span><span style="color: #339933;">=</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var1</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;.JPG&quot;</span><span style="color: #339933;">:</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;.JPEG&quot;</span><span style="color: #339933;">:</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;image/jpeg&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;.GIF&quot;</span><span style="color: #339933;">:</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;image/gif&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;.PNG&quot;</span><span style="color: #339933;">:</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;image/png&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This fourth function allows for the use of the proper PHP image <code>createfrom<em>xxx</em>()</code> call based on image type <code>imagecreatefrom()</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*Define imagecreatefrom() ****************************************************************************************************/</span>
<span style="color: #000000; font-weight: bold;">function</span> imagecreatefrom<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;JPG&quot;</span> <span style="color: #339933;">||</span> <span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;JPEG&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>		
	<span style="color: #000088;">$image</span><span style="color: #339933;">=</span><span style="color: #990000;">imagecreatefromjpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;PNG&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>		
	<span style="color: #000088;">$image</span><span style="color: #339933;">=</span><span style="color: #990000;">imagecreatefrompng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;GIF&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>		
	<span style="color: #000088;">$image</span><span style="color: #339933;">=</span><span style="color: #990000;">imagecreatefromgif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$image</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Lastly this functions will output the image to the browser <code>image_same_type().</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*Define image_same_type() ****************************************************************************************************/</span>
<span style="color: #000000; font-weight: bold;">function</span> image_same_type<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span><span style="color: #000088;">$quality</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;JPG&quot;</span> <span style="color: #339933;">||</span> <span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;JPEG&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>		
  	<span style="color: #990000;">imagejpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$quality</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;PNG&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>		
  	<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;GIF&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>		
  	<span style="color: #990000;">imagegif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/serve-resized-images-with-php/" rel="bookmark" title="January 11, 2010">Serve Resized Images with PHP</a></li>
<li><a href="http://camwebdesign.com/techniques/serve-watermarked-images-with-php/" rel="bookmark" title="August 15, 2010">Serve Watermarked Images with PHP</a></li>
<li><a href="http://camwebdesign.com/techniques/dynamic-font-based-images/" rel="bookmark" title="January 2, 2010">Dynamic Font-Based Images</a></li>
</ul>
<p><!-- Similar Posts took 18.163 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/php-image-manipulation-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic Font-Based Images</title>
		<link>http://camwebdesign.com/techniques/dynamic-font-based-images/</link>
		<comments>http://camwebdesign.com/techniques/dynamic-font-based-images/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 19:26:12 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Images]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Techniques]]></category>

		<guid isPermaLink="false">http://camwebdesign.com/?p=319</guid>
		<description><![CDATA[We wanted to create stylish text images for our page and post headings, but we didn&#8217;t want to create them in advance, we wanted them on the fly. This is a tutorial on how we accomplished this using PHP. You&#8217;ll need PHP (PHP: Hypertext Preprocessor) the open source dynamic language the code in our example <a href="http://camwebdesign.com/techniques/dynamic-font-based-images/" class="more-link">(more...)</a>]]></description>
			<content:encoded><![CDATA[<p>We wanted to create stylish text images for our page and post headings, but we didn&#8217;t want to create them in advance, we wanted them on the fly.  This is a tutorial on how we accomplished this using PHP.</p>
<p>You&#8217;ll need PHP (PHP: Hypertext Preprocessor) the open source dynamic language the code in our example is written.  We won&#8217;t elaborate, the internet is full of <a href="http://www.google.com/search?q=php">information reguarding PHP</a>.<span id="more-319"></span></p>
<p>You&#8217;ll need the GD library, an open source code library for the dynamic creation of images by programmers, <em>typically</em> installed along with PHP.  <a href="http://www.google.com/search?q=gd+library">Information on the GD library</a> also abounds on the internet.  Check with your host if you are unsure if you have it installed.</p>
<p>A windows font of your choice, for our site we chose a nice font called <a href="http://www.google.com/search?q=agency+font">Agency</a>.  Upload a copy of this font onto your server wherever you like, just be sure to change the path/to/the/font to your font&#8217;s path in the PHP code.</p>
<p>Determine the colors of your image (in hexidecimal form) for the following:  text, background &amp; text-shadow (if wanted).   Hexadecimal uses three sets of numbers to represent the Red, Green, and Blue values for a particular color.  We chose a blue text, with a white background and a light grey shadow.  There are quite a few free color picking programs and <a href="http://www.visibone.com/">websites</a> available, we use <a href="http://www.iconico.com/colorpic/">ColorPic by iconico.com</a> for all our color needs.  It&#8217;s freeware and does not include any other unwanted software included in the download package.</p>
<p>Save this PHP code and name the file font.php:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Get the text to draw</span>
<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'oops...missing text'</span> <span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//set default text if missing</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Path to the font</span>
<span style="color: #000088;">$font</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/path/to/the/font/AGENCYR.TTF'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get display font size</span>
<span style="color: #000088;">$size</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'size'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'size'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color:#800080;">18.5</span> <span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//set default if empty</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get display width</span>
<span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">100</span> <span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//set default if empty</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get display height</span>
<span style="color: #000088;">$height</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'h'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'h'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">40</span> <span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//set default if empty</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create the image</span>
<span style="color: #000088;">$im</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Choose text color</span>
<span style="color: #000088;">$color</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>color<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>color<span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;black&quot;</span> <span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//Image color</span>
<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;black&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$use_color</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;orange&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$use_color</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">159</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">64</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;blue&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$use_color</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">53</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">111</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;white&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$use_color</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the top offset</span>
<span style="color: #000088;">$offset</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'offset'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'offset'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">30</span> <span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//set default if empty</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create the image background</span>
<span style="color: #000088;">$bgcolor</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'bgcolor'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'white'</span><span style="color: #009900;">&#41;</span> ?	<span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//set default if empty</span>
<span style="color: #990000;">imagefilledrectangle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bgcolor</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Make the background-color transparent</span>
<span style="color: #990000;">imagecolortransparent</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bgcolor</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Add some shadow to the text if requested</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'shadow'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$shadoffset</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$offset</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$shadow</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">220</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">220</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">220</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">imagettftext</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #000088;">$size</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$shadoffset</span><span style="color: #339933;">,</span> <span style="color: #000088;">$shadow</span><span style="color: #339933;">,</span> <span style="color: #000088;">$font</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Add the text in the color chosen</span>
<span style="color: #990000;">imagettftext</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #000088;">$size</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span><span style="color: #339933;">,</span> <span style="color: #000088;">$use_color</span><span style="color: #339933;">,</span> <span style="color: #000088;">$font</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
<span style="color: #666666; font-style: italic;">// Set the content-type and print the image</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: image/gif'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagegif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Simply call the dynamic font based image in a generic HTML image tag. Be sure to use the <em>alt</em> portion of the image tag so that if the image generation fails text still appears, not to mention this will aid in SEO.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;img src=&quot;font.php?size=18&amp;amp;w=400&amp;amp;h=34&amp;amp;shadow=y&amp;amp;color=blue&amp;amp;bgcolor=white&amp;amp;text=My+Dynamic+Font+Based+Image&quot; alt=&quot;My Dynamic Font Based Image&quot; /&gt;</pre></td></tr></table></div>

<p>Some Examples:</p>
<ul>
<li><img src="/layout/font.php?w=400&amp;shadow=y&amp;color=blue&amp;bgcolor=white&amp;text=Blue+with+a+shadow+on+a+white+background" alt="Blue with a shadow on a white background" /></li>
<li><img src="/layout/font.php?w=400&amp;shadow=y&amp;color=black&amp;bgcolor=white&amp;text=Black+with+a+shadow+on+a+white+background" alt="Black without a shadow on a white background" /></li>
<li><img src="/layout/font.php?w=400&amp;shadow=y&amp;color=orange&amp;bgcolor=white&amp;text=Orange+with+a+shadow+on+a+white+background" alt="Orange with a shadow on a white background" /></li>
<li><img src="/layout/font.php?w=400&amp;color=blue&amp;bgcolor=white&amp;text=Blue+without+a+shadow+on+a+white+background" alt="Blue without a shadow on a white background" /></li>
<li><img src="/layout/font.php?w=400&amp;color=black&amp;bgcolor=white&amp;text=Black+without+a+shadow+on+a+white+background" alt="Black without a shadow on a white background" /></li>
<li><img src="/layout/font.php?w=400&amp;color=orange&amp;bgcolor=white&amp;text=Orange+without+a+shadow+on+a+white+background" alt="Orange without a shadow on a white background" /></li>
</ul>
<p>How do <em>we</em> dynamically use this technique in conjunction with our site&#8217;s code?  The text for our images come from the <code>&lt;?php the_title(); ?&gt;</code> call from WordPress.  Hence, whatever text is set for the page or post title will be displayed in image form.  No need to create static text images for our pages and posts.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;img src=&quot;font.php?size=20&amp;amp;w=500&amp;amp;shadow=y&amp;amp;color=blue&amp;amp;bgcolor=white&amp;amp;text=&lt;?php the_title(); ?&gt;&quot; alt=&quot;&lt;?php the_title(); ?&gt;&quot; /&gt;</pre></td></tr></table></div>

<p>Room for improvement?  Why yes there is always improvements that can be made to the code, some or our thoughts include:</p>
<ul>
<li>Create an on the fly HEX color conversion based on the <code>$color</code> and <code>$bgcolor</code> variables</li>
<li>Determine the top offset based on the image height and the font size, allowing all text to align in the center of the image.</li>
<li>Auto width based on number of text characters.  This could be tricky and may require a monospacing (fixed-width) font.</li>
</ul>
<p><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/serve-watermarked-images-with-php/" rel="bookmark" title="August 15, 2010">Serve Watermarked Images with PHP</a></li>
<li><a href="http://camwebdesign.com/techniques/serve-resized-images-with-php/" rel="bookmark" title="January 11, 2010">Serve Resized Images with PHP</a></li>
<li><a href="http://camwebdesign.com/techniques/redefine-twenty-ten-header-image-width-and-height/" rel="bookmark" title="January 23, 2011">Redefine Twenty Ten header image width and height</a></li>
</ul>
<p><!-- Similar Posts took 18.021 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/dynamic-font-based-images/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello World! &#8211; PHP Style</title>
		<link>http://camwebdesign.com/techniques/hello-world-php-style/</link>
		<comments>http://camwebdesign.com/techniques/hello-world-php-style/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 19:32:45 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Techniques]]></category>

		<guid isPermaLink="false">http://camwebdesign.com/?p=563</guid>
		<description><![CDATA[What would a list of How-To-Techniques be on a WordPress site without a &#8220;Hello World!&#8221; example in PHP? We think lacking at best, so let&#8217;s play with some simple php code in the examples below. 1 print &#34;Hello, World!&#34;; Alternatively we could write it this way: 1 echo &#34;Hello, World!&#34;; Both Yield Hello, World! If <a href="http://camwebdesign.com/techniques/hello-world-php-style/" class="more-link">(more...)</a>]]></description>
			<content:encoded><![CDATA[<p>What would a list of How-To-Techniques be on a WordPress site without a &#8220;Hello World!&#8221; example in PHP?</p>
<p>We think lacking at best, so let&#8217;s play with some simple php code in the examples <span id="more-563"></span>below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Hello, World!&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Alternatively we could write it this way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Hello, World!&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Both Yield <code>Hello, World!</code></p>
<p>If you don&#8217;t understand the significance of the PHP &#8220;Hello World!&#8221; example, then you must not know the advantages <a href="http://en.wikipedia.org/wiki/Server-side_scripting">server-side programming</a> languages provide.  The use of HTML to print Hello World! is simple, but having it done dynamically on the fly is quite another thing.  Server-side programming has changed the face of how the web works, and PHP has truly fueled the open source revolution.</p>
<p>Now we&#8217;ll move on to the question, &#8220;What is the difference between the PHP <code>print()</code> and <code>echo()</code> language constructs&#8221;?</p>
<p>The answer, not much &#8211; BUT &#8211; <code>echo</code> is faster than <code>print</code>.  <code>Print</code> returns an int value and <code>echo</code> returns no value.  Example: <code>$value = print "Hello World";</code> here <code>$value</code> will be set to <code>1</code>.  So <code>print</code> can be used in complex expression where you can&#8217;t use <code>echo</code>.</p>
<p>We can however, use <code>echo</code> is some creative short hand statements, like this one.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'hi'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;Hello World!&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Goodbye World!&quot;</span> <span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Suppose the variable <code>$text</code> was set to &#8220;hi&#8221;, this would print <code>Hello World!</code>.  If <code>$text</code> was set to anything else then it will print <code>Goodbye World!</code>.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/admin-link-shortcode-for-thematic-footer-text/" rel="bookmark" title="August 15, 2010">Admin Link Shortcode for Thematic Footer Text</a></li>
<li><a href="http://camwebdesign.com/techniques/dynamic-font-based-images/" rel="bookmark" title="January 2, 2010">Dynamic Font-Based Images</a></li>
<li><a href="http://camwebdesign.com/techniques/serve-watermarked-images-with-php/" rel="bookmark" title="August 15, 2010">Serve Watermarked Images with PHP</a></li>
</ul>
<p><!-- Similar Posts took 17.813 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/hello-world-php-style/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

