
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://www.thelinuxwiki.com/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://www.thelinuxwiki.com/index.php?action=history&amp;feed=atom&amp;title=Php_image_thumbnail_generation_script</id>
		<title>Php image thumbnail generation script - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://www.thelinuxwiki.com/index.php?action=history&amp;feed=atom&amp;title=Php_image_thumbnail_generation_script"/>
		<link rel="alternate" type="text/html" href="http://www.thelinuxwiki.com/index.php?title=Php_image_thumbnail_generation_script&amp;action=history"/>
		<updated>2026-04-29T00:15:51Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.21.5</generator>

	<entry>
		<id>http://www.thelinuxwiki.com/index.php?title=Php_image_thumbnail_generation_script&amp;diff=196&amp;oldid=prev</id>
		<title>Nighthawk: Pushed from Themanclub.</title>
		<link rel="alternate" type="text/html" href="http://www.thelinuxwiki.com/index.php?title=Php_image_thumbnail_generation_script&amp;diff=196&amp;oldid=prev"/>
				<updated>2013-04-12T16:26:08Z</updated>
		
		<summary type="html">&lt;p&gt;Pushed from Themanclub.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.reconn.us/image_thumbnail.html&lt;br /&gt;
&lt;br /&gt;
'''Start of script...'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
//define a maxim size for the uploaded images&lt;br /&gt;
define (&amp;quot;MAX_SIZE&amp;quot;,&amp;quot;100&amp;quot;);&lt;br /&gt;
// define the width and height for the thumbnail&lt;br /&gt;
// note that theese dimmensions are considered the maximum dimmension and are not fixed,&lt;br /&gt;
// because we have to keep the image ratio intact or it will be deformed&lt;br /&gt;
define (&amp;quot;WIDTH&amp;quot;,&amp;quot;150&amp;quot;);&lt;br /&gt;
define (&amp;quot;HEIGHT&amp;quot;,&amp;quot;100&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
// this is the function that will create the thumbnail image from the uploaded image&lt;br /&gt;
// the resize will be done considering the width and height defined, but without deforming the image&lt;br /&gt;
function make_thumb($img_name,$filename,$new_w,$new_h)&lt;br /&gt;
{&lt;br /&gt;
//get image extension.&lt;br /&gt;
$ext=getExtension($img_name);&lt;br /&gt;
//creates the new image using the appropriate function from gd library&lt;br /&gt;
if(!strcmp(&amp;quot;jpg&amp;quot;,$ext) || !strcmp(&amp;quot;jpeg&amp;quot;,$ext))&lt;br /&gt;
$src_img=imagecreatefromjpeg($img_name);&lt;br /&gt;
&lt;br /&gt;
if(!strcmp(&amp;quot;png&amp;quot;,$ext))&lt;br /&gt;
$src_img=imagecreatefrompng($img_name);&lt;br /&gt;
&lt;br /&gt;
//gets the dimmensions of the image&lt;br /&gt;
$old_x=imageSX($src_img);&lt;br /&gt;
$old_y=imageSY($src_img);&lt;br /&gt;
&lt;br /&gt;
// next we will calculate the new dimmensions for the thumbnail image&lt;br /&gt;
// the next steps will be taken:&lt;br /&gt;
// 1. calculate the ratio by dividing the old dimmensions with the new ones&lt;br /&gt;
// 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable&lt;br /&gt;
// and the height will be calculated so the image ratio will not change&lt;br /&gt;
// 3. otherwise we will use the height ratio for the image&lt;br /&gt;
// as a result, only one of the dimmensions will be from the fixed ones&lt;br /&gt;
$ratio1=$old_x/$new_w;&lt;br /&gt;
$ratio2=$old_y/$new_h;&lt;br /&gt;
if($ratio1&amp;gt;$ratio2) {&lt;br /&gt;
$thumb_w=$new_w;&lt;br /&gt;
$thumb_h=$old_y/$ratio1;&lt;br /&gt;
}&lt;br /&gt;
else {&lt;br /&gt;
$thumb_h=$new_h;&lt;br /&gt;
$thumb_w=$old_x/$ratio2;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// we create a new image with the new dimmensions&lt;br /&gt;
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);&lt;br /&gt;
&lt;br /&gt;
// resize the big image to the new created one&lt;br /&gt;
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);&lt;br /&gt;
&lt;br /&gt;
// output the created image to the file. Now we will have the thumbnail into the file named by $filename&lt;br /&gt;
if(!strcmp(&amp;quot;png&amp;quot;,$ext))&lt;br /&gt;
imagepng($dst_img,$filename);&lt;br /&gt;
else&lt;br /&gt;
imagejpeg($dst_img,$filename);&lt;br /&gt;
&lt;br /&gt;
//destroys source and destination images.&lt;br /&gt;
imagedestroy($dst_img);&lt;br /&gt;
imagedestroy($src_img);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This function reads the extension of the file.&lt;br /&gt;
// It is used to determine if the file is an image by checking the extension.&lt;br /&gt;
function getExtension($str) {&lt;br /&gt;
$i = strrpos($str,&amp;quot;.&amp;quot;);&lt;br /&gt;
if (!$i) { return &amp;quot;&amp;quot;; }&lt;br /&gt;
$l = strlen($str) - $i;&lt;br /&gt;
$ext = substr($str,$i+1,$l);&lt;br /&gt;
return $ext;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This variable is used as a flag. The value is initialized with 0 (meaning no error found)&lt;br /&gt;
and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.&lt;br /&gt;
$errors=0;&lt;br /&gt;
// checks if the form has been submitted&lt;br /&gt;
if(isset($_POST['Submit']))&lt;br /&gt;
{&lt;br /&gt;
//reads the name of the file the user submitted for uploading&lt;br /&gt;
$image=$_FILES['image']['name'];&lt;br /&gt;
// if it is not empty&lt;br /&gt;
if ($image)&lt;br /&gt;
{&lt;br /&gt;
// get the original name of the file from the clients machine&lt;br /&gt;
$filename = stripslashes($_FILES['image']['name']);&lt;br /&gt;
&lt;br /&gt;
// get the extension of the file in a lower case format&lt;br /&gt;
$extension = getExtension($filename);&lt;br /&gt;
$extension = strtolower($extension);&lt;br /&gt;
// if it is not a known extension, we will suppose it is an error, print an error message&lt;br /&gt;
and will not upload the file, otherwise we continue&lt;br /&gt;
if (($extension != &amp;quot;jpg&amp;quot;) &amp;amp;&amp;amp; ($extension != &amp;quot;jpeg&amp;quot;) &amp;amp;&amp;amp; ($extension != &amp;quot;png&amp;quot;))&lt;br /&gt;
{&lt;br /&gt;
echo '&amp;lt;h1&amp;gt;Unknown extension!&amp;lt;/h1&amp;gt;';&lt;br /&gt;
$errors=1;&lt;br /&gt;
}&lt;br /&gt;
else&lt;br /&gt;
{&lt;br /&gt;
// get the size of the image in bytes&lt;br /&gt;
// $_FILES[\'image\'][\'tmp_name\'] is the temporary filename of the file in which the uploaded file was stored on the server&lt;br /&gt;
$size=getimagesize($_FILES['image']['tmp_name']);&lt;br /&gt;
$sizekb=filesize($_FILES['image']['tmp_name']);&lt;br /&gt;
&lt;br /&gt;
//compare the size with the maxim size we defined and print error if bigger&lt;br /&gt;
if ($sizekb &amp;gt; MAX_SIZE*1024)&lt;br /&gt;
{&lt;br /&gt;
echo '&amp;lt;h1&amp;gt;You have exceeded the size limit!&amp;lt;/h1&amp;gt;';&lt;br /&gt;
$errors=1;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//we will give an unique name, for example the time in unix time format&lt;br /&gt;
$image_name=time().'.'.$extension;&lt;br /&gt;
//the new name will be containing the full path where will be stored (images folder)&lt;br /&gt;
$newname=&amp;quot;images/&amp;quot;.$image_name;&lt;br /&gt;
$copied = copy($_FILES['image']['tmp_name'], $newname);&lt;br /&gt;
//we verify if the image has been uploaded, and print error instead&lt;br /&gt;
if (!$copied)&lt;br /&gt;
{&lt;br /&gt;
echo '&amp;lt;h1&amp;gt;Copy unsuccessfull!&amp;lt;/h1&amp;gt;';&lt;br /&gt;
$errors=1;&lt;br /&gt;
}&lt;br /&gt;
else&lt;br /&gt;
{&lt;br /&gt;
// the new thumbnail image will be placed in images/thumbs/ folder&lt;br /&gt;
$thumb_name='images/thumbs/thumb_'.$image_name;&lt;br /&gt;
// call the function that will create the thumbnail. The function will get as parameters&lt;br /&gt;
the image name, the thumbnail name and the width and height desired for the thumbnail&lt;br /&gt;
$thumb=make_thumb($newname,$thumb_name,WIDTH,HEIGHT);&lt;br /&gt;
}} }}&lt;br /&gt;
&lt;br /&gt;
//If no errors registred, print the success message and show the thumbnail image created&lt;br /&gt;
if(isset($_POST['Submit']) &amp;amp;&amp;amp; !$errors)&lt;br /&gt;
{&lt;br /&gt;
echo &amp;quot;&amp;lt;h1&amp;gt;Thumbnail created Successfully!&amp;lt;/h1&amp;gt;&amp;quot;;&lt;br /&gt;
echo '&amp;lt;img src=&amp;quot;'.$thumb_name.'&amp;quot;&amp;gt;';&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;!-- next comes the form, you must set the enctype to &amp;quot;multipart/form-data&amp;quot; and use an input type &amp;quot;file&amp;quot; --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form name=&amp;quot;newad&amp;quot; method=&amp;quot;post&amp;quot; enctype=&amp;quot;multipart/form-data&amp;quot; action=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;table&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;file&amp;quot; name=&amp;quot;image&amp;quot; &amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&amp;lt;input name=&amp;quot;Submit&amp;quot; type=&amp;quot;submit&amp;quot; value=&amp;quot;Upload image&amp;quot;&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&lt;br /&gt;
End of script...&lt;/div&gt;</summary>
		<author><name>Nighthawk</name></author>	</entry>

	</feed>