Image resizing is an essential part of most web applications. To display uploaded images at a consistent size, you need to resize or crop them server-side. This tutorial uses PHP's built-in GD library to resize a JPEG image to a fixed 100×100 pixel thumbnail.
Before you begin: create an images/ folder (for the original upload) and a resize/ folder (for the output) in your project root.
Step 1 — HTML Upload Form
A simple form with enctype="multipart/form-data" to accept image files.
<html>
<head>
<title>Resize image in PHP</title>
</head>
<body>
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="POST" enctype="multipart/form-data">
<table>
<tr>
<td><label>Select image to resize:</label></td>
<td><input type="file" name="file" id="file"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Upload & Resize" name="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
Step 2 — PHP Resize Code
When the form is submitted, PHP validates the file type and size, uploads the original to images/, then creates a resized copy in resize/ using GD.
<?php
if (isset($_POST['submit'])) {
$error = 0;
$uploadfile = 'images/' . basename($_FILES['file']['name']);
// $uploadfile — original image directory; can be unlinked after resizing
$resize = 'resize/';
// $resize — destination directory for the resized image
$file_name = basename($_FILES['file']['name']);
$filecheck = basename($_FILES['file']['name']);
$ext = strtolower(substr($filecheck, strrpos($filecheck, '.') + 1));
// Validate: only jpg/gif/png, max ~2 MB
$allowed_ext = ($ext == "jpg" || $ext == "gif" || $ext == "png");
$allowed_type = ($_FILES["file"]["type"] == "image/jpeg"
|| $_FILES["file"]["type"] == "image/gif"
|| $_FILES["file"]["type"] == "image/png");
$allowed_size = ($_FILES["file"]["size"] < 2120000);
if (!($allowed_ext && $allowed_type && $allowed_size)) {
echo "File Not Supported";
$error = 1;
die();
}
if ($_FILES['file']['name'] != "") {
ini_set('memory_limit', '32M');
if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo "Error uploading file (check size limit).";
$error = 1;
} else {
// Load original image and get its dimensions
$img = imagecreatefromjpeg($uploadfile);
$width = imagesx($img);
$height = imagesy($img);
// Set target dimensions
$new_width = 100;
$new_height = 100;
// Create a blank canvas and copy the resized image onto it
$tmp_img = imagecreatetruecolor($new_width, $new_height);
imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
// Save the resized image to the resize/ folder
imagejpeg($tmp_img, "{$resize}{$file_name}");
}
}
if ($error == 0) {
echo "Image uploaded and resized successfully.";
exit;
}
}
?>
How It Works
imagecreatefromjpeg()— loads the uploaded JPEG into a GD image resource.imagesx()/imagesy()— read the original width and height.imagecreatetruecolor($new_width, $new_height)— creates a blank 100×100 canvas.imagecopyresized()— copies and scales the original onto the new canvas.imagejpeg($tmp_img, $path)— saves the result as a JPEG in theresize/folder.
Hope this tutorial is useful for you. Keep following PHP Tutorial for Beginners for more help.