Skip to content

Instantly share code, notes, and snippets.

@blbwd blbwd/thumbnail.php
Created Sep 9, 2015

Embed
What would you like to do?
Thumbnail Creation with PHP
<?php
// Maximum size of the uploaded image
define('MAX_SIZE','100');
// Width & Height of the thumbnail in pixels
define('WIDTH','150');
define('HEIGHT','100');
// Thumbnail creation function
function makeThumb($img_name,$file_name,$new_width,$new_height)
{
// Get the extension of the image
$ext =getExtension ($img_name);
// Creates new image using GD library function
if (!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
{
$src_img = imagecreatefromjpeg($img_name);
}
if (!strcmp("png",$ext))
{
$src_img = imagecreatefrompng($img_name);
}
// Getthe dimension of the image
$old_width = imagesx($src_img);
$old_height = imagesy($src_img);
// Calculate the width & height ratio of the thumbnail
$ratio1 = $old_width / $new_width;
$ratio2 = $old_height / $new_height;
if ($ratio1 > $ratio2)
{
$thumb_width = $new_width;
$thumb_height = $old_height / $ratio1;
}
else
{
$thumb_width = $old_width / $ratio2;
$thumb_height = $new_height;
}
// Create a new image with the new dimension
$dst_img = imagecreatetruecolor($new_width,$new_height);
// Resize the big image with the new created one
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_width,$thumb_height,$new_width,$new_height);
// Output the created image to the file
if (!strcmp("png",$ext))
{
imagepng ($dst_img,$file_name);
}
else
{
imagejpeg ($dst_img,$file_name);
}
// Destroy the source & destination image
//imagedestroy($dst_img);
//imagedestroy($src_img);
}
// Function for checkingthe extension of the file
function getExtension ($str)
{
$i =strpos($str,".");
if (!$i)
{
return "";
}
$j =strlen($str) - $i;
$ext =substr($str,$i+1,$j);
return $ext;
}
// Flag
$error = 0;
if (isset($_POST['submit']))
{
// Read the name of the uploaded file
$image = $_FILES['image']['name'];
if ($image)
{
// Get the original name of the file
$file_name = stripslashes($_FILES['image']['name']);
// Get the extension of the file in lower case
$extension =getExtension($file_name);
$extension =strtolower($extension);
// If extension is not valid print the error msg
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png"))
{
echo "Unkown File type.";
$error = 1;
}
else
{
// Get the size of the image in bytes
$size =getimagesize($_FILES['image']['tmp_name']);
$sizekb =filesize($_FILES['image']['tmp_name']);
// If the size exceeds to themaximum size,print error
if ($sizekb > MAX_SIZE * 1024)
{
echo "The file size is larger than the maximum allowed size";
$error = 1;
}
// Give the unique time span
$image_name = time() . '.' . $extension;
// the new image will be stored in "images" folder
$new_name = "images/".$image_name;
$copied = copy($_FILES['image']['tmp_name'],$new_name);
if (!$copied)
{
echo "Copy Unsuccessful";
$error = 1;
}
else
{
// The new thumbnail imagewill be placed: images/thumbs folder
$thumb_name = "images/thumbs/thumb_".$image_name;
// Call the thumbnail creation function
$thumb =makeThumb ($new_name,$thumb_name,WIDTH,HEIGHT);
}
}
}
}
if (isset($_POST['submit']) && !$error)
{
echo "Thumbnail Created Successfully";
echo '<img src="'.$thumb_name.'" alt="" />';
}
?>
<form name="imageupload" action="" method="post" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" name="submit" value="Upload">
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.