Handling images in PHP is a routine endeavor for web developers creating websites, online stores, or applications that involve image uploads. If you are eager to convert image in PHP, optimize image in PHP, or simply process image in PHP, the Intervention Image package simplifies this job for you. Suppose you have an online shop where customers upload product images. Big images can bog down your website, impact SEO, and damage user experience. With PHP image optimization by Intervention Image, you are able to resize, convert, and optimize big images on the fly, enhancing performance and load speed.
Use Cases for Processing Image in PHP
With PHP image intervention, you can process a great variety of tasks:
- Convert images from one format to another such as JPG, PNG, and WebP
- Resizing big images to enhance web speed
- Crop pictures for avatars, banners, or thumbnails
- Rotate pictures to right orientation
- Get metadata like size, dimensions, and type
- Optimize pictures to make them smaller in size without compromising on quality’
These type of functionalities can assist developers in keeping professional and fast-loading websites and offer flexibility in managing or manipulating images.
Installing intervention/image Package In PHP
Before we get started, it’s important that Intervention Image library requires either the GD or Imagick PHP libraries for image processing. We won’t be covering how to install either of these in this article. However, depending on your PHP setup, you may already have one of these libraries installed and available.
To get started, you need to install the package via Composer using the following command:
composer require intervention/imageIt will download required files for this package but before moving further you need to check for GD or Imagick library.
Image Conversion in PHP with Intervention Image
Converting images is a key part of PHP image optimization. With Intervention Image, you can convert images between multiple formats such as PNG, JPG, JPEG, GIF, BMP, and WebP. This flexibility allows you to serve the right image format depending on the platform or requirements of user.
For example, WebP files are smaller and faster for the web, while JPG and PNG are widely supported across all devices and provide much higher quality in compared to WebP. You can define the output format and also control the image quality to reduce file size without compromising visual quality. This makes it easy to process image in PHP efficiently, whether you are handling user uploads, product images, or website assets.
Let’s take a few example to convert image from one type to another:
use Intervention\Image\ImageManager;
$manager = new ImageManager(
    new Intervention\Image\Drivers\Gd\Driver()
);
// open an image file
$image = $manager->read('images/example.png');
// convert and encode to JPG
$encodedJpg = $image->toJpg();
$encodedJpg->save('images/example.jpg');
// convert and encode to PNG
$encodedPng = $image->toPng();
$encodedPng->save('images/example_converted.png');
// convert and encode to WebP
$encodedWebp = $image->toWebp();
$encodedWebp->save('images/example.webp');
// convert and encode to GIF
$encodedGif = $image->toGif();
$encodedGif->save('images/example.gif');As you can see in example, it provide class with image manipulation methods. First, you have to load image and create object of class with providing proper drive. If your file is another directory then you have to provide full path for that file. Once file is loaded, you can start encoding it to different format using it’s methods.
With save() method, you can store converted file to the storage. However, you can chain some other methods for resizing or placeholder on same object.
Resizing Images in PHP
Resizing images is also essential for web development and performance optimization. They fit within your website’s layout and for reducing file sizes. Resizing is one of the most common operations in processing images in PHP. When users upload large photos, it can affect your website’s performance and page load time. Using Intervention Image, you can easily resize images while keeping their aspect ratio intact.
Example: Resize Image to Fixed Width and Height
use Intervention\Image\ImageManager;
$manager = new ImageManager(
    new Intervention\Image\Drivers\Gd\Driver()
);
$image = $manager->read('images/product.png');
$image->resize(800, 600);
$image->save('images/product_resized.jpg');This example resizes the image to fixed dimensions (800*600). This approach is used when you want all images to have consistent sizes across your layout, such as for gallery or listings.
Example: Resize While Maintaining Aspect Ratio
use Intervention\Image\ImageManager;
$manager = new ImageManager(
    new Intervention\Image\Drivers\Gd\Driver()
);
$image = $manager->read('images/product_large.jpg');
// Resize by width while keeping aspect ratio
$image->resize(800, null, function ($constraint) {
    $constraint->aspectRatio();
});
$image->save('images/product_resized_ratio.jpg');Here, width is fixed to 800px and the height adjusts automatically to preserve the original ratio of image.
Reading Image Metadata in PHP
Understanding the properties of an image is often necessary before applying transformations. With Intervention Image, you can easily read metadata such as dimensions, format, and MIME type. This helps validate uploaded files or adjust your resizing logic dynamically.
use Intervention\Image\ImageManager;
$manager = new ImageManager(
    new Intervention\Image\Drivers\Gd\Driver()
);
// Read the image
$image = $manager->read('images/product_resized.jpg');
// Get metadata
$width = $image->width();
$height = $image->height();
$mime = $image->mime();
echo "Width: $width px\n";
echo "Height: $height px\n";
echo "Type: $mime\n";Rotating Images in PHP
Sometimes images appear rotated incorrectly, especially when captured on different devices. With Intervention Image, rotating an image to the correct orientation takes just one line of code.
use Intervention\Image\ImageManager;
$manager = new ImageManager(
    new Intervention\Image\Drivers\Gd\Driver()
);
$image = $manager->read('images/product.jpg');
$image->rotate(90);
$image->save('images/product_rotated.jpg');Cropping Images in PHP
Another useful feature of image intervention is cropping, when you need to focus on a specific part of an image like product center, a face, or a logo section it’s useful.
use Intervention\Image\ImageManager;
$manager = new ImageManager(
    new Intervention\Image\Drivers\Gd\Driver()
);
$image = $manager->read('images/banner.jpg');
// Crop image to 400x300 starting from (50, 50)
$image->crop(400, 300, 50, 50);
$image->save('images/banner_cropped.jpg');Generally, this point selection value is taken as selection from user and based on user selection crop operation is performed. Dynamic crop is common practice into social media profile picture selection. Social media platform like Linkedin, Facebook or Twitter(X) allows user to upload their image and perform selection via front-end and crop uploaded image using selection points.
Conclusion
With Intervention Image, you’re able to resize image in PHP, optimize image in PHP, and process image in PHP completely with little effort. From cropping and resizing to reading the metadata and rotating an image, the package ensures image processing is quick, efficient, and developer-friendly. For your e-commerce website or your individual project, PHP image intervention ensures that your images are amazing while ensuring your website remains optimized for performance and speed.
If you’re working with Laravel and want an even more advanced way to handle and resize uploaded images, check out our detailed guide on Resize Images in Laravel with Spatie Media Library. It explains how to automate image resizing and optimization directly within your Laravel projects for smoother

