Thumbnail generation should start only after the uploaded file has been validated as an image. Preserve aspect ratio unless the lesson intentionally demonstrates cropping.
$source = imagecreatefromjpeg(__DIR__ . '/images/photo.jpg');
if ($source !== false) {
$thumb = imagescale($source, 240);
if ($thumb !== false) {
imagejpeg($thumb, __DIR__ . '/images/photo-thumb.jpg', 85);
imagedestroy($thumb);
}
imagedestroy($source);
}The examples below show additional variations and practical cases.
For uploaded images, validate the upload error, inspect the file with finfo, generate a server-side filename, and move it with move_uploaded_file(). Do not trust $_FILES['type'] or the original filename.
if (!isset($_FILES['userfile']) || $_FILES['userfile']['error'] !== UPLOAD_ERR_OK) {
exit('Upload failed');
}
$tmp = $_FILES['userfile']['tmp_name'];
if (!is_uploaded_file($tmp)) {
exit('Invalid upload');
}
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($tmp);
$allowed = [
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/gif' => 'gif',
];
if (!isset($allowed[$mime])) {
exit('Unsupported image type');
}
$filename = bin2hex(random_bytes(16)) . '.' . $allowed[$mime];
$sourcePath = __DIR__ . '/upimg/' . $filename;
if (!move_uploaded_file($tmp, $sourcePath)) {
exit('Could not store upload');
}
[$width, $height] = getimagesize($sourcePath);
$thumbWidth = 180;
$thumbHeight = (int) round($thumbWidth / $width * $height);
$create = match ($mime) {
'image/jpeg' => 'imagecreatefromjpeg',
'image/png' => 'imagecreatefrompng',
'image/gif' => 'imagecreatefromgif',
};
$save = match ($mime) {
'image/jpeg' => 'imagejpeg',
'image/png' => 'imagepng',
'image/gif' => 'imagegif',
};
$source = $create($sourcePath);
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, $height);
$thumbPath = __DIR__ . '/thimg/' . $filename;
$save($thumb, $thumbPath);
imagedestroy($source);
imagedestroy($thumb);
Keep upload and thumbnail directories non-executable at the web-server level, enforce a size limit before processing, and consider pixel-dimension limits to prevent decompression-bomb style resource exhaustion.

Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| parteek kumar | 24-07-2014 |
| please give an best and short idea for make thubnail after image upload in php. | |
| Melvin | 15-10-2014 |
| I will try your code soon.... | |
| Pooya.S | 17-10-2014 |
| Great . Thank you very much. | |
| proPhet | 09-12-2014 |
| The above code works well, thanks! However it does not allow for aspect ratio preservation. | |
| smo | 12-12-2014 |
| One line of code is to be added to proportionately manage the height of thumbnail image. That part is added now. Thanks. | |
| navjit singh | 14-01-2015 |
| thanx above code is very helpfull | |
| success4gande | 22-05-2015 |
| Sir, How will the image upload and thumbnail php script look like if it was to be in an application form where applicant will need to upload image and immediately the browser uploads then the image previews or thumbnails in the form? | |
| smo1234 | 22-05-2015 |
| To display thumbnail the file has to be uploaded first. Displaying directly from user local computer is not possible. | |
| Php Expert Programmer | 15-06-2017 |
| Great tutorial. This tutorial was really helpful to create a thumbnail images. Thanks for this tutorial. | |