Clue Mediator

Resize an image using the GD library in PHP

📅August 12, 2020
🗁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
  2. Create HTML form
  3. Write PHP code to resize an image
  4. Output

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

<!--?php
phpinfo();
?-->

Now search "GD Support" in "gd" section and it should be "Enabled". Check the below image for your reference.

GD Supports - Clue Mediator

GD Supports - Clue Mediator

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



	<title>Resize an image using the GD library in PHP - Clue Mediator</title>


	<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>

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

<!--?php<p-->

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.

<!--?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.

Output - Resize an image using the GD library in PHP - Clue Mediator

Output - Resize an image using the GD library in PHP - Clue Mediator

That’s it for today.
Thank you for reading. Happy Coding..!!

Demo & Source Code

Github Repository