7

Total Sites

3

Total Users

293

Total Optimize CSS

978

Total Image Converted

Optimize Image API Documentation

The /optimize-image endpoint allows you to download, resize, and optimize images from a URL. The endpoint supports various formats (e.g., png, jpg, webp, svg) and provides options for resizing and quality adjustments.

Endpoint

GET https://codefusiononline.com/optimize-image

Query Parameters

Parameter Type Description Required Default
imageUrl string The URL of the image to be processed. Yes N/A
format string The output format (e.g., png, jpg, webp, svg). No png
width number The width to resize the image to. No N/A
height number The height to resize the image to. No N/A
quality number The quality of the output image (1–100). No 80

Example Request URL

https://codefusiononline.com/optimize-image?imageUrl=https://codefusiononline.com/yourimage.jpg&width=400&height=300&format=webp&quality=80

Response Codes


Example Code

1. WordPress HTTP Request (wp_remote_get)


// WordPress wp_remote_get example
$url = 'https://codefusiononline.com/optimize-image';
$args = [
    'method' => 'GET',
    'body'   => [
        'imageUrl' => 'https://codefusiononline.com/yourimage.jpg',
        'width'    => 400,
        'height'   => 300,
        'format'   => 'webp',
        'quality'  => 80,
    ],
];

$response = wp_remote_get(add_query_arg($args['body'], $url));
if (is_wp_error($response)) {
    error_log('Error fetching image: ' . $response->get_error_message());
} else {
    $body = wp_remote_retrieve_body($response);
    // Process the image data
}

2. PHP cURL Request


// PHP cURL example
$url = 'https://codefusiononline.com/optimize-image';
$queryParams = http_build_query([
    'imageUrl' => 'https://codefusiononline.com/yourimage.jpg',
    'width'    => 400,
    'height'   => 300,
    'format'   => 'webp',
    'quality'  => 80,
]);

$ch = curl_init("$url?$queryParams");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
} else {
    // Process the image data
    file_put_contents('optimized_image.webp', $response);
}
curl_close($ch);

3. Laravel HTTP Request


use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;

$response = Http::get('https://codefusiononline.com/optimize-image', [
    'imageUrl' => 'https://codefusiononline.com/yourimage.jpg',
    'width'    => 400,
    'height'   => 300,
    'format'   => 'webp',
    'quality'  => 80,
]);

if ($response->successful()) {
    Storage::put('optimized_image.webp', $response->body());
} else {
    logger('Image optimization failed', $response->json());
}

4. JavaScript Fetch API


// JavaScript Fetch example
const params = new URLSearchParams({
    imageUrl: 'https://codefusiononline.com/yourimage.jpg',
    width: 400,
    height: 300,
    format: 'webp',
    quality: 80,
}).toString();

fetch(`https://codefusiononline.com/optimize-image?${params}`)
    .then(response => response.blob())
    .then(blob => {
        const url = URL.createObjectURL(blob);
        const img = document.createElement('img');
        img.src = url;
        document.body.appendChild(img); // Display the image in the browser
    })
    .catch(error => console.error('Error fetching optimized image:', error));
Back to Top