How to Resize Images in PHP

Cloudmersive
2 min readMar 10, 2023

--

Large image files play a big role in slowing down website loading speeds. Thankfully, resizing image files before they go live is a great way to fix that problem.

Using the PHP code examples provided in this article, you can quickly and easily take advantage of an Image Resizing API for free. This API allows you to customize exact height and width dimensions for your images, ensuring all images on your website will adhere to a uniform standard.

To use this API for free, first register a free account on our website to get a free-tier API key. Once that’s done, install the SDK by running this command:

composer require cloudmersive/cloudmersive_imagerecognition_api_client

Then, use the following code examples to structure your API calls:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\ResizeApi(


new GuzzleHttp\Client(),
$config
);
$width = 56; // int | Width of the output image - final image will be exactly this width
$height = 56; // int | Height of the output image - final image will be exactly this height
$image_file = "/path/to/inputfile"; // \SplFileObject | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.

try {
$result = $apiInstance->resizeResizeSimple($width, $height, $image_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ResizeApi->resizeResizeSimple: ', $e->getMessage(), PHP_EOL;
}
?>

That’s all the code you’ll need!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet