Resize an image using the GD library in PHP
Today we will explain to you how to resize an image using the GD library in PHP. When we are working with the gallery or individual images to display on a page then it’s more important to reduce the size of an image. That way we can improve the page speed and user experience and for that, we will use the GD library.
We’ll take an example to upload an image and parallelly create a thumbnail of the same image using the GD library in PHP to store in a different folder.
Steps to resize an image using the GD library
1. Enable GD extension
First, we have to enable the GD Support before we start. You can easily check on your server just create a simple php file and write phpinfo()
then access it on a web browser.
Create a PHP file and run it in the browser.
check-gd-support.php
1 2 3 | <?php phpinfo(); ?> |
Now search “GD Support” in “gd” section and it should be “Enabled“. Check the below image for your reference.
If GD Library is not installed on your server then you can manually install GD library and enable it.
2. Create HTML form
In the next step, we will create a HTML form to manage the upload image UI.
index.php
1 2 3 4 5 6 7 8 9 10 11 12 | <html> <head> <title>Resize an image using the GD library in PHP - Clue Mediator</title> </head> <body> <h3>Resize an image using the GD library - <a href="https://www.cluemediator.com" target="_blank" rel="noopener noreferrer">Clue Mediator</a></h3> <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="image" /><br /><br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> |
Let’s create a uploads
folder in root directory and also create a thumbs
folder inside the “uploads” folder.
3. Write PHP code to resize an image
Now, we create a file named upload.php
and write the following php code to resize the image. Using this code we will create thumbnail images of jpg, jpeg, png, gif.
upload.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <?php if(isset($_FILES["image"])) { $image = $_FILES['image']['tmp_name']; $imgProperties = getimagesize($image); $imageName = $_FILES['image']['name']; $pathToImages = "./uploads/"; $pathToThumbs = "./uploads/thumbs/"; $extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); $img_type = $imgProperties[2]; if( $img_type == IMAGETYPE_JPEG ) { $source = imagecreatefromjpeg($image); $resizeImg = image_resize($source,$imgProperties[0],$imgProperties[1]); imagejpeg($resizeImg,$pathToThumbs.$imageName); } elseif ($img_type == IMAGETYPE_PNG ) { $source = imagecreatefrompng($image); $resizeImg = image_resize($source,$imgProperties[0],$imgProperties[1]); imagepng($resizeImg,$pathToThumbs.$imageName); } elseif ($img_type == IMAGETYPE_GIF ) { $source = imagecreatefromgif($image); $resizeImg = image_resize($source,$imgProperties[0],$imgProperties[1]); imagegif($resizeImg,$pathToThumbs.$imageName); } move_uploaded_file($image, $pathToImages.$imageName); echo "Image resize successfully."; } function image_resize($source,$width,$height) { $new_width =150; $new_height =150; $thumbImg=imagecreatetruecolor($new_width,$new_height); imagecopyresampled($thumbImg,$source,0,0,0,0,$new_width,$new_height,$width,$height); return $thumbImg; } ?> |
Here, we compare image type with IMAGETYPE_JPEG
, IMAGETYPE_PNG
and IMAGETYPE_GIF
that are the predefined constants in php and you can check it using the below code.
1 2 3 | <?php print_r(get_defined_constants()); ?> |
We have used the following GD functions.
- getimagesize() – To get the height and width of the uploaded (source) image.
- imagecreatefromjpeg() / imagecreatefrompng() / imagecreatefromgif() – To create a new image from an uploaded (source) image.
- imagecreatetruecolor() – Use to create the true color image.
- imagecopyresampled() – Generate the resized image.
- imagejpeg() / imagepng() / imagegif() – To save the output image.
4. Output
Run the above code and check the output.
That’s it for today.
Thank you for reading. Happy Coding..!!
Pretty! This was an incredibly wonderful post. Thank you
for supplying these details.