PHP Thumbnail Image creation script

Create a thumbnail from a validated image

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.

Upload and validate the source image

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');
}

Create a proportional thumbnail

[$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.

download Php Thumbnail creation script
Create profile picture by uploading photo by members Php file upload ( single file ) Multiple file upload Photo gallery script with picture upload using PHP MySQL


Subscribe to our YouTube Channel here



plus2net.com







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.




PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer