<?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>PHP Magic Book - Free PHP Scripts, Tutorials and Downloads &#187; Thumbnail</title>
	<atom:link href="http://www.phpmagicbook.com/tag/thumbnail/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpmagicbook.com</link>
	<description>PHP AtoZ Reloaded, free php tutorials, free php downloads, php scripts, PHP tips</description>
	<lastBuildDate>Tue, 15 Jun 2010 13:22:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Upload Image And Create Thumbnail Using PHP</title>
		<link>http://www.phpmagicbook.com/upload-image-and-create-thumbnail/</link>
		<comments>http://www.phpmagicbook.com/upload-image-and-create-thumbnail/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 23:01:10 +0000</pubDate>
		<dc:creator>Hiroshi</dc:creator>
				<category><![CDATA[File Upload]]></category>
		<category><![CDATA[Image Handling]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Thumbnail]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://www.phpmagicbook.com/?p=84</guid>
		<description><![CDATA[This php script takes image file creates a thumbhnail and upload it into &#8216;thumb&#8217; directory and also upload origional image with rename in &#8216;images&#8217; directory. Functionality - Uploads image in the &#8216;images; directory and renames it - Uploads thumbnail in &#8216;thumbs&#8217; directory and renames it with thumb_filename - Determine thumbnail max width and height - [...]<p><a href="http://www.phpmagicbook.com/upload-image-and-create-thumbnail/">Upload Image And Create Thumbnail Using PHP</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
]]></description>
			<content:encoded><![CDATA[<p>This php script takes image file creates a thumbhnail and upload it into &#8216;thumb&#8217; directory and also upload origional image with rename in &#8216;images&#8217; directory.</p>
<h4>Functionality</h4>
<p>- Uploads image in the &#8216;images; directory and renames it<br />
- Uploads thumbnail in &#8216;thumbs&#8217; directory and renames it with thumb_filename<br />
- Determine thumbnail max width and height<br />
- Keeps aspect ratio of thumbnail size<br />
- Allows jpg, jpeg and png image files<br />
- Determines Max File Size allowed<br />
- Displays thumbnail after uploading</p>
<p><span id="more-184"></span></p>
<h4>Form Code</h4>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;newad&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; action=&quot;&quot;&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;&lt;input type=&quot;file&quot; name=&quot;image&quot; &gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;input name=&quot;Submit&quot; type=&quot;submit&quot; value=&quot;Upload image&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;</pre></div></div>

<h4>PHP Code</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//define a maxim size for the uploaded images</span>
<span style="color: #990000;">define</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MAX_SIZE&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;100&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// define the width and height for the thumbnail</span>
<span style="color: #666666; font-style: italic;">// note that theese dimmensions are considered the maximum dimmension and are not fixed,</span>
<span style="color: #666666; font-style: italic;">// because we have to keep the image ratio intact or it will be deformed</span>
<span style="color: #990000;">define</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;WIDTH&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;150&quot;</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;">&quot;HEIGHT&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;100&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// this is the function that will create the thumbnail image from the uploaded image</span>
<span style="color: #666666; font-style: italic;">// the resize will be done considering the width and height defined, but without deforming the image</span>
<span style="color: #000000; font-weight: bold;">function</span> make_thumb<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_name</span><span style="color: #339933;">,</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span><span style="color: #000088;">$new_w</span><span style="color: #339933;">,</span><span style="color: #000088;">$new_h</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//get image extension.</span>
<span style="color: #000088;">$ext</span><span style="color: #339933;">=</span>getExtension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//creates the new image using the appropriate function from gd library</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jpg&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$ext</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jpeg&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$ext</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000088;">$src_img</span><span style="color: #339933;">=</span><span style="color: #990000;">imagecreatefromjpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_name</span><span style="color: #009900;">&#41;</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: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;png&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$ext</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000088;">$src_img</span><span style="color: #339933;">=</span><span style="color: #990000;">imagecreatefrompng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//gets the dimmensions of the image</span>
<span style="color: #000088;">$old_x</span><span style="color: #339933;">=</span><span style="color: #990000;">imageSX</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$old_y</span><span style="color: #339933;">=</span><span style="color: #990000;">imageSY</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// next we will calculate the new dimmensions for the thumbnail image</span>
<span style="color: #666666; font-style: italic;">// the next steps will be taken:</span>
<span style="color: #666666; font-style: italic;">// 1. calculate the ratio by dividing the old dimmensions with the new ones</span>
<span style="color: #666666; font-style: italic;">// 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable</span>
<span style="color: #666666; font-style: italic;">// and the height will be calculated so the image ratio will not change</span>
<span style="color: #666666; font-style: italic;">// 3. otherwise we will use the height ratio for the image</span>
<span style="color: #666666; font-style: italic;">// as a result, only one of the dimmensions will be from the fixed ones</span>
<span style="color: #000088;">$ratio1</span><span style="color: #339933;">=</span><span style="color: #000088;">$old_x</span><span style="color: #339933;">/</span><span style="color: #000088;">$new_w</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ratio2</span><span style="color: #339933;">=</span><span style="color: #000088;">$old_y</span><span style="color: #339933;">/</span><span style="color: #000088;">$new_h</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ratio1</span><span style="color: #339933;">&gt;</span><span style="color: #000088;">$ratio2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$thumb_w</span><span style="color: #339933;">=</span><span style="color: #000088;">$new_w</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$thumb_h</span><span style="color: #339933;">=</span><span style="color: #000088;">$old_y</span><span style="color: #339933;">/</span><span style="color: #000088;">$ratio1</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;">$thumb_h</span><span style="color: #339933;">=</span><span style="color: #000088;">$new_h</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$thumb_w</span><span style="color: #339933;">=</span><span style="color: #000088;">$old_x</span><span style="color: #339933;">/</span><span style="color: #000088;">$ratio2</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// we create a new image with the new dimmensions</span>
<span style="color: #000088;">$dst_img</span><span style="color: #339933;">=</span><span style="color: #990000;">ImageCreateTrueColor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$thumb_w</span><span style="color: #339933;">,</span><span style="color: #000088;">$thumb_h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// resize the big image to the new created one</span>
<span style="color: #990000;">imagecopyresampled</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #339933;">,</span><span style="color: #000088;">$src_img</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: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$thumb_w</span><span style="color: #339933;">,</span><span style="color: #000088;">$thumb_h</span><span style="color: #339933;">,</span><span style="color: #000088;">$old_x</span><span style="color: #339933;">,</span><span style="color: #000088;">$old_y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// output the created image to the file. Now we will have the thumbnail into the file named by $filename</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;png&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$ext</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</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;">else</span>
<span style="color: #990000;">imagejpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #339933;">,</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//destroys source and destination images.</span>
<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src_img</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;">// This function reads the extension of the file.</span>
<span style="color: #666666; font-style: italic;">// It is used to determine if the file is an image by checking the extension.</span>
<span style="color: #000000; font-weight: bold;">function</span> getExtension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$l</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ext</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #000088;">$l</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$ext</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// This variable is used as a flag. The value is initialized with 0 (meaning no error found)</span>
<span style="color: #666666; font-style: italic;">//and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.</span>
<span style="color: #000088;">$errors</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// checks if the form has been submitted</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Submit'</span><span style="color: #009900;">&#93;</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;">//reads the name of the file the user submitted for uploading</span>
<span style="color: #000088;">$image</span><span style="color: #339933;">=</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// if it is not empty</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// get the original name of the file from the clients machine</span>
<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// get the extension of the file in a lower case format</span>
<span style="color: #000088;">$extension</span> <span style="color: #339933;">=</span> getExtension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$extension</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$extension</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// if it is not a known extension, we will suppose it is an error, print an error message</span>
<span style="color: #666666; font-style: italic;">//and will not upload the file, otherwise we continue</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$extension</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;jpg&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$extension</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;jpeg&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$extension</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;Unknown extension!&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$errors</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</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;">// get the size of the image in bytes</span>
<span style="color: #666666; font-style: italic;">// $_FILES[\'image\'][\'tmp_name\'] is the temporary filename of the file in which the uploaded file was stored on the server</span>
<span style="color: #000088;">$size</span><span style="color: #339933;">=</span><span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tmp_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sizekb</span><span style="color: #339933;">=</span><span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tmp_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//compare the size with the maxim size we defined and print error if bigger</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sizekb</span> <span style="color: #339933;">&gt;</span> MAX_SIZE<span style="color: #339933;">*</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;You have exceeded the size limit!&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$errors</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//we will give an unique name, for example the time in unix time format</span>
<span style="color: #000088;">$image_name</span><span style="color: #339933;">=</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">.</span><span style="color: #000088;">$extension</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//the new name will be containing the full path where will be stored (images folder)</span>
<span style="color: #000088;">$newname</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;images/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$image_name</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$copied</span> <span style="color: #339933;">=</span> <span style="color: #990000;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tmp_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//we verify if the image has been uploaded, and print error instead</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$copied</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;Copy unsuccessfull!&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$errors</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</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;">// the new thumbnail image will be placed in images/thumbs/ folder</span>
<span style="color: #000088;">$thumb_name</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'images/thumbs/thumb_'</span><span style="color: #339933;">.</span><span style="color: #000088;">$image_name</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// call the function that will create the thumbnail. The function will get as parameters</span>
<span style="color: #666666; font-style: italic;">//the image name, the thumbnail name and the width and height desired for the thumbnail</span>
<span style="color: #000088;">$thumb</span><span style="color: #339933;">=</span>make_thumb<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newname</span><span style="color: #339933;">,</span><span style="color: #000088;">$thumb_name</span><span style="color: #339933;">,</span>WIDTH<span style="color: #339933;">,</span>HEIGHT<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><span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//If no errors registred, print the success message and show the thumbnail image created</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$errors</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;h1&gt;Thumbnail created Successfully!&lt;/h1&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;img src=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$thumb_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h4>Precautions</h4>
<ul>
<li>CHMOD &#8216;images&#8217; and &#8216;thumbs&#8217; folders to 777 if it is at online server</li>
<li>Use enctype=&#8221;multipart/form-data&#8221; in form tag</li>
</ul>
<p><a href='http://www.phpmagicbook.com/wp-content/uploads/2008/06/upload-img-create-and-show-thumb.rar' class="download">Upload Image Create and Show Thumbnail Script Download</a></p>
<p><a href="http://www.phpmagicbook.com/upload-image-and-create-thumbnail/">Upload Image And Create Thumbnail Using PHP</a> is a post from: <a href="http://www.phpmagicbook.com">PHP Magic Book - Free PHP Scripts, Tutorials and Downloads</a></p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.phpmagicbook.com/php-file-upload-script-globals-off/" title="PHP File Upload Script &#8211; Globals Off">PHP File Upload Script &#8211; Globals Off</a></li><li><a href="http://www.phpmagicbook.com/upload-file-attach-send-html-format-email/" title="Upload File &#8211; Send HTML Format Email with Attachment Using PHP">Upload File &#8211; Send HTML Format Email with Attachment Using PHP</a></li><li><a href="http://www.phpmagicbook.com/file-upload-get-info/" title="File Upload Using PHP and Get File Info">File Upload Using PHP and Get File Info</a></li><li><a href="http://www.phpmagicbook.com/image-upload-rename-apply-restrictions/" title="Image Upload, Rename and Apply Restrictions Using PHP">Image Upload, Rename and Apply Restrictions Using PHP</a></li><li><a href="http://www.phpmagicbook.com/upload-file-email-file-restrict/" title="Upload and eMail File Using PHP and Apply Restrictions">Upload and eMail File Using PHP and Apply Restrictions</a></li><li><a href="http://www.phpmagicbook.com/file-upload-with-renaming/" title="File Upload With Renaming Using PHP">File Upload With Renaming Using PHP</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.phpmagicbook.com/upload-image-and-create-thumbnail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
