Crop image using gd library php

In today’s digital world, images are an integral part of any website or application. However, using images in their original size and resolution can significantly increase the loading time of the website, leading to a poor user experience. That’s where image cropping comes into play. Image cropping is the process of removing unwanted portions of an image to make it more suitable for a particular use case. In this article, we will focus on how to crop an image using the GD library in PHP.

What is the GD Library?

The GD library is a popular image manipulation library for PHP. It provides various functions to create, modify, and save images in different formats, such as JPEG, PNG, and GIF. It is a powerful tool that can be used to perform a wide range of image processing tasks, including resizing, cropping, rotating, and filtering.

Step-by-Step Guide to Crop an Image using the GD Library in PHP:

Step 1: Install GD Library

Before we start, we need to make sure that the GD library is installed on our system. You can check whether GD is installed on your system or not by running the following command in your terminal or command prompt.

php -m | grep -i gd

If the command returns “gd,” it means that the GD library is installed on your system. If not, you need to install it using the following command.

sudo apt-get install php-gd

Read Also : Laravel crop image before upload using cropper js

Step 2: Create a PHP Script

Create a new PHP script file and name it “crop_image.php.” In this script, we will use the GD library to crop an image. Start by including the GD library using the following code:

<?php  // Include GD Library  if (!function_exists('gd_info')) {    echo 'GD Library is not installed';    exit();  }?>

This code checks whether the GD library is installed or not. If it is not installed, it displays an error message and exits the script.

Step 3: Load the Image

In this step, we will load the image that we want to crop using the imagecreatefromjpeg() function. This function creates a new image from a JPEG file.

<?php  // Load the Image  $image = imagecreatefromjpeg('original.jpg');?>

Replace “original.jpg” with the path and filename of the image that you want to crop.

Read Also : Crop and Upload image in laravel using jcrop js

Step 4: Crop the Image

To crop the image, we need to use the imagecrop() function. This function takes the following parameters:

  • $image: The image that we want to crop.
  • $crop: An array that defines the cropping rectangle. The array must contain four values, representing the x-coordinate, y-coordinate, width, and height of the rectangle.
  • $color: The background color of the cropped image. This parameter is optional, and if omitted, the cropped image will have a transparent background.

Here’s the code to crop the image:

<?php  // Crop the Image  $cropped = imagecrop($image, ['x' => 100, 'y' => 100, 'width' => 300, 'height' => 200]);?>

This code crops the original image starting at the coordinates (100, 100) and creates a new image with a width of 300 pixels and a height of 200 pixels.

Step 5: Save the Cropped Image

Finally, we need to save the cropped image to a file using the imagejpeg() function. This function takes the following parameters:

  • $image: The image that we want to save.
  • $filename: The path and filename of the output file.
  • $quality: The quality of the output image. This parameter is optional and has a default value of 75.

Here’s the code to save the cropped image:

<?php  // Save the Cropped Image  imagejpeg($cropped, 'cropped.jpg', 90);?>

This code saves the cropped image to a file named “cropped.jpg” with a quality of 90.

Step 6: Display the Cropped Image

To display the cropped image on a webpage, we can use the echo statement along with an HTML img tag.

<?php  // Display the Cropped Image  echo '<img src="cropped.jpg">';?>

This code displays the cropped image on a webpage.

Conclusion:

In this article, we have learned how to crop an image using the GD library in PHP. We started by checking whether the GD library is installed or not, then loaded the image using the imagecreatefromjpeg() function. After that, we cropped the image using the imagecrop() function and saved the cropped image using the imagejpeg() function. Finally, we displayed the cropped image on a webpage using an HTML img tag.

By following the steps mentioned above, you can easily crop images using the GD library in PHP. Remember to optimize your images for web use by reducing their size and resolution, as this can significantly improve the loading time of your website or application.

Comments