الرئيسية أدوات API المقالات Hire Me نبذة عني
Sign In Create Account
متاح باقة مجانية متاحة

Optimize Images via API

Resize, compress, and convert images to WebP, JPEG, or PNG. Apply filters like blur, grayscale, and rotation. Up to 92% size reduction with one API call.

WebP / JPEG / PNG Resize & Crop Blur & Grayscale Up to 92% Smaller

Try it Free

Enter an image URL, configure options, and see the optimized result instantly.

جرب مجاناً
80%
0%
Result

Enter an image URL and click Optimize

API Documentation

Resize, compress, convert, rotate, blur, and apply grayscale to images. Supports JPEG, PNG, WebP, and GIF. Input via URL or base64. Output as optimized base64 data URI.

Endpoint

POST https://www.mahmoudalhabash.com/api/v1/image/optimize

Send an image URL or base64 data with transformation options. The API processes the image server-side and returns an optimized base64-encoded result with size comparison metrics.

Request Parameters

ParameterTypeRequiredDefaultDescription
urlstringrequired*Image URL to fetch and optimize
base64stringrequired*Base64-encoded image data (alternative to url)
widthintegeroptionaloriginalTarget width in pixels (1–4096)
heightintegeroptionaloriginalTarget height in pixels (1–4096)
qualityintegeroptional80Output quality 1–100
formatstringoptionalwebpjpeg, png, webp, or gif
fitstringoptionalcontaincontain, cover (crop), or fill (stretch)
grayscalebooleanoptionalfalseConvert to grayscale
blurintegeroptional0Gaussian blur intensity 0–100
rotateintegeroptional0Rotation in degrees (0–360)
flipstringoptionalFlip: h (horizontal), v (vertical), both

* Either url or base64 is required.

Response Fields

status "success" or "error"
format Output format used
original_size Original file size in bytes
original_size_human Human-readable original size
output_size Optimized file size in bytes
output_size_human Human-readable output size
savings_percent Size reduction percentage (negative means larger)
dimensions Output width and height
took_ms Processing time in milliseconds
image Base64 data URI of the optimized image

Code Examples

cURL
curl -X POST https://www.mahmoudalhabash.com/api/v1/image/optimize \ -H "Content-Type: application/json" \ -d '{ "url": "https://picsum.photos/1200/800", "width": 600, "quality": 75, "format": "webp" }'
Response 200JSON
{ "status": "success", "format": "webp", "original_size": 245760, "original_size_human": "240 KB", "output_size": 18432, "output_size_human": "18 KB", "savings_percent": 92.5, "dimensions": {"width": 600, "height": 400}, "took_ms": 156, "image": "data:image/webp;base64,UklGR..." }
JavaScript (fetch)
const res = await fetch('https://www.mahmoudalhabash.com/api/v1/image/optimize', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ url: imageUrl, width: 800, format: 'webp', quality: 80, grayscale: true }) }); const { image, savings_percent } = await res.json(); document.getElementById('preview').src = image; console.log(`Saved ${savings_percent}%`);
Python (requests)
import requests, base64 resp = requests.post( 'https://www.mahmoudalhabash.com/api/v1/image/optimize', json={ 'url': 'https://picsum.photos/1200/800', 'width': 600, 'format': 'webp', 'quality': 75 } ) data = resp.json() img_b64 = data['image'].split(',')[1] with open('optimized.webp', 'wb') as f: f.write(base64.b64decode(img_b64)) print(f"Saved {data['savings_percent']}%")

Error Codes

422 Validation failed — URL or base64 required, invalid dimensions or format
422 Unable to decode image — not a valid JPEG, PNG, GIF, or WebP
429 Rate limit exceeded
500 Image processing failed — GD library error

Rate Limits

Unlimited — Free during beta

No API key required. Processing uses PHP GD. Max output 4096×4096px. Images fetched via URL have a 15-second timeout.