<?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>Encodez Blog &#187; php</title>
	<atom:link href="http://encodez.com/blog/category/webdevelopement/php-webdevelopement/feed/" rel="self" type="application/rss+xml" />
	<link>http://encodez.com/blog</link>
	<description>Computer language design is just like a stroll in the park. Jurassic Park, that is. — Larry Wall</description>
	<lastBuildDate>Thu, 22 Jul 2010 05:34:40 +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>Do it simpler : Image thumbnail and resize in PHP</title>
		<link>http://encodez.com/blog/2009/11/do-it-simpler-image-thumbnail-and-resize-php/</link>
		<comments>http://encodez.com/blog/2009/11/do-it-simpler-image-thumbnail-and-resize-php/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 07:31:37 +0000</pubDate>
		<dc:creator>Muneer Shaheed</dc:creator>
				<category><![CDATA[Free Scripts]]></category>
		<category><![CDATA[Webdevelopement]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[image resize]]></category>
		<category><![CDATA[php resize]]></category>
		<category><![CDATA[thumbnail]]></category>

		<guid isPermaLink="false">http://encodez.com/blog/?p=296</guid>
		<description><![CDATA[One of the common complex task among webdevelopers is image resizing. It is a big headach when using multiple size images in multiple pages of a single version of image. For long I was using a an script that would create one thumnail version of the image when the image is uploaded. It was not a suitable solution. Many time we will need different size of thumbnails to be used in different location and pages. ]]></description>
			<content:encoded><![CDATA[<p><img src="http://encodez.com/blog/wp-content/uploads/2009/11/resize2.jpg" alt="resize2" title="Image Resizing" width="150" height="215" class="alignleft size-full wp-image-312" /> One of the common complex task among webdevelopers is image resizing. It is a big headach when using multiple size images in multiple pages of a single version of image. For long I was using a an script that would create one thumnail version of the image when the image is uploaded. It was not a suitable solution. Many time we will need different size of thumbnails to be used in different location and pages. </p>
<p>I came across <a href="http://phpthumb.gxdlabs.com/" target="_blank" title="PHPThumb">PHPThumb</a>. It was a very much better one for me where it can deliver following types of resizing mechanism.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6032126876218296";
google_ad_slot = "8998015769";
google_ad_width = 468;
google_ad_height = 60;
google_cpa_choice = "";
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<ul>
<li>adaptiveResize ($width, $height)</li>
<li>crop ($startX, $startY, $cropWidth, $cropHeight)</li>
<li>cropFromCenter ($cropWidth, $cropHeight = null)</li>
<li>resize ($maxWidth, $maxHeight)</li>
<li>resizePercent ($percent)</li>
<li>rotateImage ($direction = ‘CW’)</li>
<li>rotateImageNDegrees ($degrees)</li>
<li>save ($fileName)</li>
<li>show ()</li>
</ul>
<p>You can learn more about this script <a href="http://wiki.github.com/iselby/PHPThumb/basic-usage" target="_blank" title="Basic usage of PHPThumb">here</a></p>
<p>Well,I have written a simple script that can work with phpThumb. What it would do is, when I request for a resized version of an original image, it will check for a same resized version. If the one resized version exist, it will return the path of that. Otherwise, a new<br />
resized version will be created and saved before returning the path. This will help to use any size of thumbnail image at any time without wasting match resources.</p>
<p>The script</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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;phpthumb/ThumbLib.inc.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/**
* Resize the image according to the given parameter 
* 
* This funciton resize the image to the given criteria and save it 
* in a well-named subdirectory. Then the path to the image will be returnd.
* I the image exists allready with give criteria, the image path is 
* returned simply.
* 
* @param string     $image          path to the source image
* @param integer    $width          required width
* @param integer    $height         required height
* @param string     $resizeStyle    style of resizing (ADAPTIVE , CROP, 
*                                   CROPCENTER, RESIZEPERCENT, RESIZE)
* @param integer    $percent        resize percentage
* @param integer    $starX          start pixel X axis
* @param integer    $starY          start pixel Y axis
* @param integer    $quality        picture quality
* @return string                    path to the reqested resized image
* 
* @author muneer &lt;muneer@live.it&gt;
* @version 1.0
* @copyright (c) 2009 encodez solutions http://www.encodez.com
*/</span>
<span style="color: #000000; font-weight: bold;">function</span> getResized<span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</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;">$resizeStyle</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ADAPTIVE&quot;</span><span style="color: #339933;">,</span> 
                        <span style="color: #000088;">$percent</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #000088;">$starX</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$starY</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$quality</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">80</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>    
    <span style="color: #666666; font-style: italic;">/* setting file name */</span>
    <span style="color: #000088;">$fileName</span> <span style="color: #339933;">=</span> <span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/* creating the path for the resized image */</span>
    <span style="color: #000088;">$path</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</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: #339933;">-</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fileName</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resizeStyle</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;RESIZEPERCENT&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>        
        <span style="color: #000088;">$path</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$width</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;x&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$height</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;-&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$resizeStyle</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;">$path</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$percent</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;-&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$resizeStyle</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/* 
     * checking whether image exists with the same criteria
     * If exist return the generated path,
     * If not, create the thumnail 
    */</span> 
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fileName</span><span style="color: #009900;">&#41;</span> 
            <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">filemtime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fileName</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">filemtime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fileName</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</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;">/* making directory for the image if it is not exists */</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #990000;">mkdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #339933;">,</span> <span style="color: #208080;">0777</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'jpegQuality'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$quality</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        try
        <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">/* creating the  PhpThumbFactory object */</span>
            <span style="color: #000088;">$thumb</span> <span style="color: #339933;">=</span> PhpThumbFactory<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>            
        <span style="color: #009900;">&#125;</span>
        catch<span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$ex</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&lt;!-- There was an error resizing the image <span style="color: #000099; font-weight: bold;">\n</span>Source 
                Image : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$image</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; <span style="color: #000099; font-weight: bold;">\n</span>Error : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ex</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; --&gt;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resizeStyle</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;ADAPTIVE&quot;</span><span style="color: #339933;">:</span>        <span style="color: #666666; font-style: italic;">// adaptiveResize</span>
                <span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">adaptiveResize</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;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fileName</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;CROP&quot;</span><span style="color: #339933;">:</span>            <span style="color: #666666; font-style: italic;">// crop</span>
                <span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">crop</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$startX</span><span style="color: #339933;">,</span> <span style="color: #000088;">$startY</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: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fileName</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;CROPCENTER&quot;</span><span style="color: #339933;">:</span>      <span style="color: #666666; font-style: italic;">// cropFromCenter</span>
                <span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cropFromCenter</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: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fileName</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;RESIZEPERCENT&quot;</span><span style="color: #339933;">:</span>   <span style="color: #666666; font-style: italic;">// resizePercent</span>
                <span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resizePercent</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$percent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fileName</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;RESIZE&quot;</span><span style="color: #339933;">:</span>          <span style="color: #666666; font-style: italic;">// resize</span>
                <span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resize</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;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fileName</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;">default</span><span style="color: #339933;">:</span>                <span style="color: #666666; font-style: italic;">// resize</span>
                <span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resize</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;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fileName</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;">$path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fileName</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: #666666; font-style: italic;">/* return 0 if the src image is not exists */</span>
        <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end function getResized()</span></pre></td></tr></table></div>

<p><strong><u>How to use this function?</u></strong><br />
scenario : Need a adaptive resize verion for /img/catalogue/sample1.jpg at 120px x 100px</p>
<p><strong>solution : </strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="xhtml" style="font-family:monospace;">&lt;img src=&quot;&lt;?php echo getResized(&quot;/img/catalogue/sample1.jpg&quot;, 120, 100, &quot;ADAPTIVE&quot;) ?&gt;&quot; /&gt;</pre></td></tr></table></div>

<p>This will generate a &#8220;adaptive&#8221; resized image at the size of 120 px width and 100 px height. </p>
<p>This iamge will have the path </p>
<p><strong>/img/catalogue/120&#215;100-ADAPTIVE/sample1.jpg</strong></p>
<p>This script might helpful you too. Please do not forget to put a comment if you like it Or any suggestion if you have.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6032126876218296";
google_ui_features = "rc:6";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text";
google_alternate_ad_url = "?adsensem-benice=468x60";
google_color_border = "#ffffff";
google_color_bg = "#ffffff";
google_color_link = "#333333";
google_color_text = "#666666";
google_color_url = "008000";

//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
]]></content:encoded>
			<wfw:commentRss>http://encodez.com/blog/2009/11/do-it-simpler-image-thumbnail-and-resize-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Solution : Disabling image caching in browser (IE, Firefox, Chrome)</title>
		<link>http://encodez.com/blog/2009/11/solution-disabling-image-caching-in-browser-ie-firefox-chrome/</link>
		<comments>http://encodez.com/blog/2009/11/solution-disabling-image-caching-in-browser-ie-firefox-chrome/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 07:01:13 +0000</pubDate>
		<dc:creator>Muneer Shaheed</dc:creator>
				<category><![CDATA[Webdevelopement]]></category>
		<category><![CDATA[XHTML]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[image cache]]></category>

		<guid isPermaLink="false">http://encodez.com/blog/?p=288</guid>
		<description><![CDATA[There is a big headache among web developers with image caching. If you are using the same name while updating the image in the server, browser will not update it as quickly as you updated. This might occur in most cases where you update the image but not the file name. Confused? Let’s Make it clear,,]]></description>
			<content:encoded><![CDATA[<p>There is a big headache among web developers with image caching. If you are using the same name while updating the image in the server, browser will not update it as quickly as you updated. This might occur in most cases where you update the image but not the file name. Confused? Let’s Make it clear,,</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6032126876218296";
google_ad_slot = "8998015769";
google_ad_width = 468;
google_ad_height = 60;
google_cpa_choice = "";
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p>For example you have an image named “datasheet.jpg”. You are displaying this image on the webpage.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;img src=&quot;datasheet.jpg&quot; /&gt;</pre></div></div>

<p>Let’s say, if you are updating this image and leave it with the same name “datasheet.jpg”. Then also you image tag will be same.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;img src=&quot;datasheet.jpg&quot; /&gt;</pre></div></div>

<p> In many cases, browser will not know there is a new version of the same image and it will tend to display the content from the web cache. Even no-cache headers are not helping in these type of occasion (I tried and tired). So the returning visitor will see the same old image. </p>
<p>The simplest solution is to change the image filename. But how you will create a different name for an image that is in same name. Ok. Do it simply.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;img src=&quot;datasheet.jpg?mtime=1257316941&quot; /&gt;</pre></div></div>

<p>Do this -> Append a  <strong>Query Sting</strong></p>
<p>When you attach query sting in end of image file name, browser will treat it as a different file name and looks for the web server for new. This will not make the browser to display the cached content.</p>
<p><strong>How to generate an optimized version of Query String for Image file name?</strong></p>
<p>Using simply random value as query string will lead the browser to load the new version of image regardless of updated version. This will make the loading time of the web page too long even if the correct version of image is there on cache. This will become overhead. To overcome this issue, we can generate the query string using file modified time. So the query string will not be changed until file is modified next time. Once the file is modified, the query string will be changed and browser will load the new version.</p>
<p><strong>Generate filename with query string using file modified time (PHP)</strong></p>
<p>Use the <code>filemtime($path)</code> function to generate file modified time.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;img src=&quot;datasheet.jpg?mtime='</span> <span style="color: #339933;">.</span> <span style="color: #990000;">filemtime</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;datasheet.jpg)  . '&quot;</span> <span style="color: #339933;">/&gt;</span><span style="color: #0000ff;">';</span></pre></div></div>

<p>this will generate something similar to this</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;img src=&quot;datasheet.jpg?mtime=1257316941&quot; /&gt;</pre></div></div>

<p><strong>This will work for all types of browser including IE, FF, GC and all others.</strong><br />
I hope this will be useful for you. Don’t forget to leave a comment if you find any mistake on me. </p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6032126876218296";
google_ui_features = "rc:6";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text";
google_alternate_ad_url = "?adsensem-benice=468x60";
google_color_border = "#ffffff";
google_color_bg = "#ffffff";
google_color_link = "#333333";
google_color_text = "#666666";
google_color_url = "008000";

//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
]]></content:encoded>
			<wfw:commentRss>http://encodez.com/blog/2009/11/solution-disabling-image-caching-in-browser-ie-firefox-chrome/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Escaping &#8216;%&#8217; in MySQL LIKE statement when sprintf</title>
		<link>http://encodez.com/blog/2009/11/escaping-in-mysql-like-statement-when-sprintf/</link>
		<comments>http://encodez.com/blog/2009/11/escaping-in-mysql-like-statement-when-sprintf/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 08:29:12 +0000</pubDate>
		<dc:creator>Muneer Shaheed</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://encodez.com/blog/?p=276</guid>
		<description><![CDATA[I wanted to run a SQL query against MySQL database server which contains search string and need to be formated using <a href="http://php.net/manual/en/function.sprintf.php" target="_blank">sprintf</a>. The problem arise when format. It is because of the sign "%" am using to advance my search term. The query was...
]]></description>
			<content:encoded><![CDATA[<p>I wanted to run a SQL query against MySQL database server which contains search string and need to be formated using <a href="http://php.net/manual/en/function.sprintf.php" target="_blank">sprintf</a>. The problem arise when format. It is because of the sign &#8220;%&#8221; am using to advance my search term. The query was&#8230;</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;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM user WHERE country = '<span style="color: #009933; font-weight: bold;">%s</span>' AND fName LIKE '<span style="color: #009933; font-weight: bold;">%s</span>%' ORDER BY fName&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$country</span><span style="color: #339933;">,</span> <span style="color: #000088;">$searchTerm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Here where the error fires. Thanks God, I found the solution for it.<br />
This can be handled simply as follows,</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;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM user WHERE country = '<span style="color: #009933; font-weight: bold;">%s</span>' AND fName LIKE '<span style="color: #009933; font-weight: bold;">%s</span>' ORDER BY fName&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$country</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;%&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$searchTerm</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>but for a query like below, where it need multiple formating due to it complex and dynamic generation, I managed to prepare like below and worked fine for me.</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fieldArray</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t1</span>.id&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t3</span>.avatar&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t1</span>.login&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t1</span>.firstName&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t1</span>.lastName&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t2</span>.title&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t1</span>.email&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t1</span>.active&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT DISTINCT &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$t1</span>.id, <span style="color: #006699; font-weight: bold;">$t3</span>.avatar, <span style="color: #006699; font-weight: bold;">$t1</span>.login, <span style="color: #006699; font-weight: bold;">$t1</span>.firstName, <span style="color: #006699; font-weight: bold;">$t1</span>.lastName, <span style="color: #006699; font-weight: bold;">$t2</span>.title AS groups, <span style="color: #006699; font-weight: bold;">$t1</span>.email, <span style="color: #006699; font-weight: bold;">$t1</span>.active &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;FROM <span style="color: #006699; font-weight: bold;">$t1</span>, <span style="color: #006699; font-weight: bold;">$t2</span>, <span style="color: #006699; font-weight: bold;">$t3</span> &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;WHERE <span style="color: #006699; font-weight: bold;">$t1</span>.id = <span style="color: #006699; font-weight: bold;">$t3</span>.userId AND <span style="color: #006699; font-weight: bold;">$t3</span>.userGroup = <span style="color: #006699; font-weight: bold;">$t2</span>.id &quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$searchField</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$searchField</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fieldArray</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;AND &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fieldArray</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$searchField</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; LIKE '<span style="color: #009933; font-weight: bold;">%s</span>' &quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;%&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$searchVal</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;%&quot;</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: #000088;">$orderBy</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$orderBy</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fieldArray</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%%</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;ORDER BY <span style="color: #009933; font-weight: bold;">%s</span> <span style="color: #009933; font-weight: bold;">%s</span> &quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fieldArray</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$orderBy</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$orderStyle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$start</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%%</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;LIMIT <span style="color: #009933; font-weight: bold;">%d</span>, <span style="color: #009933; font-weight: bold;">%d</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$start</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Before generating the next %, need to double the current % where it will become single % after formated.</p>
<p>Note this</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%%</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://encodez.com/blog/2009/11/escaping-in-mysql-like-statement-when-sprintf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
