Hi Friends.
In this article, i will let you know how to generate thumbnail image while uploading in PHP. Here i will show you quick and simple code of upload picture and make thumbnail image using php gd library.
If you know, we moistly require to set several images in website and that's why it take more time to load of webpage. So if you make it better on size like generate thumbnail image for it then it make it smooth and easy to load. So thumbnail image will help to quick and fast your web page. We always require to generate thumbnail image for users, products, profile, categories etc.
Here i wanted to show you code of generate thumbnail image in PHP. So you can also use this code in your other framework too. Here i created index.php and process.php file, write code for make thumbnail image. You have to create "media"
So let's just follow bellow two files.
Create index.php file:
<!DOCTYPE html>
<html>
<head>
<title>How to Create Thumbnail Image in PHP?</title>
</head>
<body>
<h1>How to Create Thumbnail Image in PHP?</h1>
<form action="process.php" method="post" enctype="multipart/form-data">
<input type="file" name="picture" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Create process.php file:
<?php
if(isset($_POST["submit"])) {
if(is_array($_FILES)) {
$file = $_FILES['picture']['tmp_name'];
$sourceProperties = getimagesize($file);
$fileNewName = time();
$folderPath = "media/";
$ext = pathinfo($_FILES['picture']['name'], PATHINFO_EXTENSION);
$imageType = $sourceProperties[2];
switch ($imageType) {
case IMAGETYPE_PNG:
$imageResourceId = imagecreatefrompng($file);
$targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
imagepng($targetLayer,$folderPath. $fileNewName. "_thumbnail.". $ext);
break;
case IMAGETYPE_GIF:
$imageResourceId = imagecreatefromgif($file);
$targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
imagegif($targetLayer,$folderPath. $fileNewName. "_thumbnail.". $ext);
break;
case IMAGETYPE_JPEG:
$imageResourceId = imagecreatefromjpeg($file);
$targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
imagejpeg($targetLayer,$folderPath. $fileNewName. "_thumbnail.". $ext);
break;
default:
echo "Invalid Image type.";
exit;
break;
}
move_uploaded_file($file, $folderPath. $fileNewName. ".". $ext);
echo "Image Uploaded with created thumbnail Successfully.";
}
}
function imageResize($imageResourceId,$width,$height) {
$targetWidth =250;
$targetHeight =250;
$targetLayer=imagecreatetruecolor($targetWidth,$targetHeight);
imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);
return $targetLayer;
}
?>
After create both files, make sure again you have to create "media" folder with full permission.
I hope you found your best.
Do you like below Tutorials ?
- Laravel 5.6 - Collection could not be converted to int
- Laravel - How to generate secure https URL from route?
- Laravel - Vue JS File Upload Example
- How to get last 7 days data in Laravel?
- Laravel Validation required if other field empty example
- Laravel Eloquent - When Case Statement in Select Query Example
- Laravel 7.x and 6.x Passing Variable to Javascript Example
- How to pass PHP variables in JavaScript or jQuery?