Coding, PHP & MySQL

Automatic Thumbnails Creation On The Fly Using PHP

The easy to understand code to upload image and make its thumbnail. This code will let you upload an image file. On form submit the script will check if the uploaded image is a valid image and in the allowed size range. The allowed file extensions for the image are jpg, jpeg, png. Aftert the image is uploaded successfully, the thumbnail of that image is created so that the quality and colors etc of original image are retained in the thumbnail.

The following code is explaining every step and is well commented. Every step is commented to clearly understand what is happening. You just need to copy and paste this code to make the automatic thumbnails on the fly using PHP.



MAXIMUM_IMAGE_SIZE*1024) { echo 'The file size greater than allowed limit'; $errors_found=1; } $image_unique_name=time().'.'.$image_extension; // use unix time to create unique image name $new_name="_layouts/images/".$image_unique_name; // path to store new image $image_is_copied = copy($_FILES['image']['tmp_name'], $new_name); if (!$image_is_copied) // if image was not uploaded succesfully in the _layouts/images/ folder then throw an error message { echo 'ERROR!!! Image not copied'; $errors_found=1; } else { $thumbnail_name='_layouts/images/thumbs/thumb_'.$image_name; // the path to store the thumbnail $thumbnail=create_thumbnail($new_name,$thumbnail_name,THUMB_WIDTH,THUMB_HEIGHT); }} }} //If no errors registred, print the success message and show the thumbnail image created if(isset($_POST['Submit']) && !$errors_found) { echo "Thumbnail Created."; echo ''; } /************************************************************************************************************************************8 FUCNTIONS USED IN THE PROCESS /************************************************************************************************************************************/ // This function will create the thumbs from the uploaded image. function create_thumbnail($image_name,$filename_,$new_width,$new_height) { $extension=getimageExtension($image_name); // Getting the image type i.e gif, jpg etc...... if(!strcmp("jpg",$extension) || !strcmp("jpeg",$extension)) { $source_image=imagecreatefromjpeg($image_name); // creating another new image } if(!strcmp("png",$extension)) { $sorce_image=imagecreatefrompng($imgage_name);// creating another new image } $old_image_x=imageSX($source_image); $old_image_y=imageSY($source_image); // Now lets find new width and heights for the thumb image // 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable // and the height will be calculated so the image ratio will not change // 3. otherwise we will use the height ratio for the image // as a result, only one of the dimmensions will be from the fixed ones $ratioA=$old_image_x/$new_width; // Finding the ratio(divide the old x and y values with the new x and y values) $ratioB=$old_image_y/$new_height; // Finding the ratio(divide the old x and y values with the new x and y values) if($ratioA>$ratioB) { $thumbnail_width=$new_width; $thumbnail_height=$old_image_y/$ratioB; } else { $thumbnail_height=$new_height; $thumbnail_width=$old_image_x/$ratioB; } $destination_image=ImageCreateTrueColor($thumbnail_width,$thumbnail_height); // Make the fresh image using the above craeted widths and heights // re-size image imagecopyresampled($destination_image,$source_image,0,0,0,0,$thumbnail_width,$thumbnail_height,$old_image_x,$old_image_y); // Now we are outputting frshly made image to file. The thumb image will be now named by $file_name if(!strcmp("png",$extension)) { imagepng($destination_image,$file_name); } else { imagejpeg($destination_image,$file_name); } imagedestroy($$destination_image); // Remove the destination image imagedestroy($source_image); //// Remove the source image } // this usefull little function is giving us the file extension function getimageExtension($string) { $ii = strrpos($string,"."); if (!$ii) return ""; $ll = strlen($string) - $ii; $image_extension = substr($string,$ii+1,$ll); return $image_extension; } ?>