<?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>camwebdesign.com &#187; Images</title>
	<atom:link href="http://camwebdesign.com/category/techniques/php/images/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 Jul 2010 10:54:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<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[This is the first tutorial of our PHP image tutorial series. There are many reasons we use PHP to serve images on sites including: resizing, watermarking, manipulation, 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. [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first tutorial of our PHP image tutorial series.  There are many reasons we use PHP to serve images on sites including: resizing, watermarking, manipulation, 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 <span id="more-605"></span>image types.  We&#8217;ll cover more advanced image manipulation techniques in later posts.  Now lets get into the PHP code for the image manipulation.</p>
<p>The first line of code calls the functions.phtml 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">$_GET</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, feel free to copy ours at the bottom of the post.  The fourth line checks if the requested file is an image and surrounds the remaining code with the if statement.</p>
<div>
<pre><code id="code1">include_once("/path/to/include/functions.phtml");
$img1 = "/images/" . urldecode(stripslashes($_GET[img]));
if(!is_file($img1)) $img1 = "/images/nophoto.gif";
if(is_image($img1)){ 

   /*** The rest of the Code ***/

}</code></pre>
</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>
<pre><code id="code2">  //Send proper image header base on mime type
  $mime=get_mime($img1);
  header("Content-type: ".$mime);
</code></pre>
</div>
<p>With the following code we determine the final image&#8217;s width and height.  Based on the presence of $_GET[a] 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>
<pre><code id="code3">  // Set dimensions, quality &#038; variables
  $width = $_GET[w];
  $v = ($_GET[a]) ? "none" : "thumb" ;
  if($v == "thumb") {
    ($width) ? "" : $width = 100;
    ($width > 178) ? $width = 178 : "" ;
    $height = $width;
    $thmb_quality = 100;
  }else{
    ($width) ? "" : $width = 300;
    ($width > 588) ? $width = 588 : "" ;
    $height = $width;
    $thmb_quality = 100;
  }</code></pre>
</div>
<p>We then determine the original image&#8217;s width and height.</p>
<div>
<pre><code id="code4">  // Get orig dimensions
  list($width_orig, $height_orig, $type_orig) = getimagesize($img1);
  $src_x=0;
  $src_y=0;</code></pre>
</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 $_GET[a] variable in the URL.</p>
<div>
<pre><code id="code5">  if(!$_GET[a]){
    //it's a thumbnail request, make it square from the center
    if ($width_orig < $height_orig) {
      $src_y=($height_orig-$width_orig)/2;
      $height_orig=$width_orig;
    }else{
      $src_x=($width_orig-$height_orig)/2;
      $width_orig=$height_orig;
    }
	}else{
    //keep aspect ratio but keeping width set from page
    $height = ($width / $width_orig) * $height_orig;
  }</code></code></pre>
</div>
<p>Finally we resample then print the image with PHP.</p>
<div>
<pre><code id="code6">  // Resample
  $image=imagecreatefrom($img1);
  $im = imagecreatetruecolor($width, $height);
  imagecopyresampled($im, $image, 0, 0, $src_x, $src_y, $width, $height, $width_orig, $height_orig);

  //Print Image
  image_same_type($img1,$im,$thmb_quality);
  image_cache_type($img1,$im,$image_path);</code></pre>
</div>
<hr />
</p>
<p>Putting it all together we arrive at the following code, we named our PHP file thumb.php since it&#8217;s primary role is to make thumbnails for the site.</p>
<div><span class="CODEselect">[<a href="#">Select Code</a>]</span></p>
<pre><code id="code7">&lt;?php
include_once("/path/to/include/functions.phtml");
$img1 = "/images/" .  urldecode(stripslashes($_GET[img]));
if(!is_file($img1)) $img1 = "/nophoto.gif";
if(is_image($img1)){

  //Send proper image header base on mime type
  $mime=get_mime($img1);
  header("Content-type: ".$mime);

  // Set dimensions, quality &#038; variables
  $width = $_GET[w];
  $v = ($_GET[a]) ? "none" : "thumb" ;
  if($v == "thumb") {
    ($width) ? "" : $width = 100;
    ($width &gt; 150) ? $width = 150 : "" ;
    $height = $width;
    $thmb_quality = 100;
  }else{
    ($width) ? "" : $width = 300;
    ($width &gt; 588) ? $width = 588 : "" ;
    $height = $width;
    $thmb_quality = 100;
  }

  // Get orig dimensions
  list($width_orig, $height_orig, $type_orig) = getimagesize($img1);
  $src_x=0;
  $src_y=0;

  if(!$_GET[a]){
    //it's a thumbnail request, make it square from the center
    if ($width_orig &lt; $height_orig) {
      $src_y=($height_orig-$width_orig)/2;
      $height_orig=$width_orig;
    }else{
      $src_x=($width_orig-$height_orig)/2;
      $width_orig=$height_orig;
    }
	}else{
    //keep aspect ratio but keeping width set from page
    $height = ($width / $width_orig) * $height_orig;
  }

  // Resample
  $image=imagecreatefrom($img1);
  $im = imagecreatetruecolor($width, $height);
  imagecopyresampled($im, $image, 0, 0, $src_x, $src_y, $width, $height, $width_orig, $height_orig);

  //Print Image
  image_same_type($img1,$im,$thmb_quality);
  image_cache_type($img1,$im,$image_path);

}
?&gt;</code></pre>
</div>
<hr />
<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 &#8220;uwcmc.jpg&#8221; for our United Way of Cape May County client.</p>
<div><span class="CODEselect">[<a href="#">Select Code</a>]</span></p>
<pre><code id="code8">&lt;img src="http://camwebdesign.com/layout/thumb.php?w=200&#038;a=y&#038;img=uwcmc.jpg" alt="UWCMC" /&gt;</code></pre>
</div>
<p><img src="http://camwebdesign.com/layout/thumb.php?w=200&#038;a=y&#038;img=uwcmc.jpg" alt="UWCMC" /></p>
<hr />
</p>
<p>Below we are calling an image without specifying width or aspect ration, again using an image named &#8220;uwcmc.jpg&#8221;.</p>
<div><span class="CODEselect">[<a href="#">Select Code</a>]</span></p>
<pre><code id="code9">&lt;img src="http://camwebdesign.com/layout/thumb.php?img=uwcmc.jpg" alt="UWCMC" /&gt;</code></pre>
</div>
<p><img src="http://camwebdesign.com/layout/thumb.php?img=uwcmc.jpg" alt="UWCMC" /></p>
<hr />
</p>
<p>Here is our free <em>&#8220;No Photo&#8221;</em> image:</p>
<p><a href="http://camwebdesign.com/techniques/serve-resized-images-with-php/attachment/nophoto" rel="attachment wp-att-644"><img src="http://camwebdesign.com/~/wp-content/uploads/2010/01/nophoto.gif" alt="" title="nophoto" width="135" height="135" class="alignnone size-full wp-image-644" /></a></p>
<p>Post your thoughts below&#8230;</p>
<hr /><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://camwebdesign.com/techniques/php-image-manipulation-functions/" rel="bookmark" title="January 9, 2010">PHP Image Manipulation Functions</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/consistent-cross-browser-code-snippets/" rel="bookmark" title="January 6, 2010">Consistent Cross-Browser Code Snippets</a></li>
</ul>
<p><!-- Similar Posts took 14.535 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/serve-resized-images-with-php/feed/</wfw:commentRss>
		<slash:comments>0</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 [...]]]></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<span id="more-631"></span> a <a href="http://php.net/manual/en/function.include-once.php">PHP include_once()</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 <em>is_image()</em>.</p>
<div><span class="CODEselect">[<a href="#">Select Code</a>]</span></p>
<pre><code id="code1">/*Define is_image() ****************************************************************************************************/
function is_image($var1) {
	$t=strtoupper(substr($var1,-3,3));
	$t2=strtoupper(substr($var1,-4,4));
	if($t=="GIF" ||$t=="PNG" ||$t=="JPG" ||$t2=="JPEG") return true;
	return false;
}
</code></pre>
</div>
<p>This function returns the extension for any file <em>getExtension().</em></p>
<div><span class="CODEselect">[<a href="#">Select Code</a>]</span></p>
<pre><code id="code2">/*Define getExtension() ****************************************************************************************************/
function getExtension($file) {
  $pos = strrpos($file, '.');
  if(!$pos) { $photosubmitted = 'Error: Unknown Filetype'; }
  $str = substr($file, $pos, strlen($file));
  return $str;
}
</code></pre>
</div>
<p>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 <em>get_mime()</em>.</p>
<div><span class="CODEselect">[<a href="#">Select Code</a>]</span></p>
<pre><code id="code3">/*Define get_mime() ****************************************************************************************************/
function get_mime($var1) {
	$t=strtoupper(substr($var1,-4,4));
	switch($t){
		case ".JPG":
		case ".JPEG":
			return "image/jpeg";
			break;
		case ".GIF":
			return "image/gif";
			break;
		case ".PNG":
			return "image/png";
			break;
		default: return "";
			break;
	}
}</code></pre>
</div>
<p>This fourth function allows for the use of the proper PHP image createfrom<em>xxx</em> call based on image type <em>imagecreatefrom()</em>.</p>
<div><span class="CODEselect">[<a href="#">Select Code</a>]</span></p>
<pre><code id="code4">/*Define imagecreatefrom() ****************************************************************************************************/
function imagecreatefrom($file)  {
if(strtoupper(substr($file,-3,3))=="JPG" || strtoupper(substr($file,-4,4))=="JPEG"){
	$image=imagecreatefromjpeg($file);
}
if(strtoupper(substr($file,-3,3))=="PNG"){
	$image=imagecreatefrompng($file);
}
if(strtoupper(substr($file,-3,3))=="GIF"){
	$image=imagecreatefromgif($file);
}
return $image;
}</code></pre>
</div>
<p>Lastly this functions will output the image to the browser <em>image_same_type().</em></p>
<div><span class="CODEselect">[<a href="#">Select Code</a>]</span></p>
<pre><code id="code5">/*Define image_same_type() ****************************************************************************************************/
function image_same_type($file,$image,$quality = 100) {
  if(strtoupper(substr($file,-3,3))=="JPG" || strtoupper(substr($file,-4,4))=="JPEG"){
  	imagejpeg($image,null,$quality);
  }
  if(strtoupper(substr($file,-3,3))=="PNG"){
  	imagepng($image);
  }
  if(strtoupper(substr($file,-3,3))=="GIF"){
  	imagegif($image);
  }
}</code></pre>
</div>
<p>Post your thoughts below&#8230;</p>
<hr /><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/dynamic-font-based-images/" rel="bookmark" title="January 2, 2010">Dynamic Font-Based Images</a></li>
<li><a href="http://camwebdesign.com/techniques/select-code-in-snippets-with-jquery/" rel="bookmark" title="January 7, 2010">Select code in snippets with jQuery</a></li>
</ul>
<p><!-- Similar Posts took 14.562 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://camwebdesign.com/techniques/php-image-manipulation-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
