Skip to content

convertToWebP()

Converts images to the modern WebP format with configurable quality settings.

public function convertToWebP(
string $inputImagePath,
string $outputImagePath,
int $quality = 80
): void

Path to the input image file. Supports JPEG, PNG, and GIF formats.

Path where the WebP image will be saved. Should use .webp extension.

Quality level (0-100). Default: 80

  • Higher values = better quality, larger file
  • Lower values = worse quality, smaller file
  • Recommended: 75-90 for most use cases

Returns void. Creates the output WebP file.

  1. Opens the input image (JPEG, PNG, or GIF)
  2. Validates quality parameter (0-100)
  3. Converts to WebP format with specified quality
  4. Saves to output path
  5. Cleans up resources
  • PNG with transparency → WebP with transparency (preserved)
  • JPEG → WebP (opaque)
  • GIF → WebP (first frame only, transparency preserved)

WebP provides excellent compression:

  • Quality 90: ~30% smaller than JPEG at same quality
  • Quality 80: ~35% smaller than JPEG
  • Quality 70: ~40% smaller than JPEG
use Kenura\Imagick\ImageProcessor;
$processor = new ImageProcessor();
// Convert with default quality (80)
$processor->convertToWebP('photo.jpg', 'photo.webp');
// High quality
$processor->convertToWebP('photo.jpg', 'high-quality.webp', 95);
// Balanced (recommended)
$processor->convertToWebP('photo.jpg', 'balanced.webp', 85);
// Smaller file
$processor->convertToWebP('photo.jpg', 'compressed.webp', 70);
// Transparency is preserved
$processor->convertToWebP('logo.png', 'logo.webp', 90);
$images = glob('photos/*.{jpg,png}', GLOB_BRACE);
foreach ($images as $image) {
$filename = pathinfo($image, PATHINFO_FILENAME);
$processor->convertToWebP($image, "webp/{$filename}.webp", 85);
}
$inputPath = 'photo.jpg';
$outputPath = 'photo.webp';
$originalSize = filesize($inputPath);
$processor->convertToWebP($inputPath, $outputPath, 85);
$webpSize = filesize($outputPath);
$savings = round((1 - $webpSize / $originalSize) * 100, 2);
echo "Reduced by {$savings}%\n";
Use CaseQualityFile SizeVisual Quality
High-end photography90-95MediumExcellent
Standard web images80-85SmallVery good
Thumbnails70-80Very smallGood
Icons/previews60-70MinimalAcceptable
// Test different qualities
$qualities = [95, 85, 75, 65];
foreach ($qualities as $quality) {
$processor->convertToWebP(
'photo.jpg',
"test-{$quality}.webp",
$quality
);
}
FormatSupportNotes
JPEG✅ FullBest compression gains
PNG✅ FullTransparency preserved
GIF✅ PartialFirst frame only

Always outputs WebP format, regardless of input.

WebP is supported by:

  • Chrome 23+
  • Firefox 65+
  • Edge 18+
  • Safari 14+ (macOS Big Sur)
  • Opera 12.1+

Always provide fallback images:

// Create WebP version
$processor->convertToWebP('photo.jpg', 'photo.webp', 85);
// Keep JPEG fallback
copy('photo.jpg', 'photo-fallback.jpg');
// Or compress it
$processor->compressToJpg('photo.jpg', 'photo-fallback.jpg', 150);

Use in HTML:

<picture>
<source srcset="photo.webp" type="image/webp">
<img src="photo-fallback.jpg" alt="Photo">
</picture>

Compared to original formats:

OriginalWebP Quality 85Savings
JPEG 100KB~70KB30%
PNG 200KB~80KB60%
GIF 150KB~50KB67%

Typical conversion times:

Image SizeTime
4000x3000~0.3s
1920x1080~0.1s
800x600~0.05s
// ✅ Good: Balanced quality
$processor->convertToWebP('photo.jpg', 'photo.webp', 85);
// ❌ Bad: Unnecessarily high
$processor->convertToWebP('photo.jpg', 'photo.webp', 100);
// ❌ Bad: Too low quality
$processor->convertToWebP('photo.jpg', 'photo.webp', 30);
// ✅ Good: Clear extension
$processor->convertToWebP('photo.jpg', 'photo.webp', 85);
// ⚠️ Confusing: Wrong extension
$processor->convertToWebP('photo.jpg', 'photo.jpg', 85);
// Find the sweet spot for your images
foreach ([95, 85, 75, 65] as $quality) {
$processor->convertToWebP('photo.jpg', "test-{$quality}.webp", $quality);
// Compare visually
}
// Create both formats
$processor->convertToWebP('photo.jpg', 'photo.webp', 85);
$processor->compressToJpg('photo.jpg', 'photo.jpg', 150);

Cause: GD library not compiled with WebP support

Solution: Check and install WebP support:

Terminal window
# Check if WebP is supported
php -r "echo function_exists('imagewebp') ? 'Supported' : 'Not supported';"
# Ubuntu/Debian
sudo apt-get install libwebp-dev
sudo apt-get install php-gd
# Rebuild PHP with WebP support if needed

Cause: Quality parameter too low

Solution: Increase quality:

// Increase from 60 to 85
$processor->convertToWebP('photo.jpg', 'photo.webp', 85);

Cause: Input already optimized or quality too high

Solution: Lower quality or check input:

// Lower quality
$processor->convertToWebP('photo.jpg', 'photo.webp', 75);
// Check original size
echo filesize('photo.jpg') / 1024 . "KB\n";

Handle conversion errors:

<?php
use Kenura\Imagick\ImageProcessor;
$processor = new ImageProcessor();
try {
// Check WebP support
if (!function_exists('imagewebp')) {
throw new Exception("WebP not supported");
}
// Validate input
if (!file_exists('photo.jpg')) {
throw new Exception("Input file not found");
}
// Convert
$processor->convertToWebP('photo.jpg', 'photo.webp', 85);
echo "✅ Conversion successful!";
} catch (Exception $e) {
error_log("WebP error: " . $e->getMessage());
echo "❌ Conversion failed.";
}
// Resize first
$processor->resizeImage('large.jpg', 'temp.jpg', 1920, 1080);
// Convert to WebP
$processor->convertToWebP('temp.jpg', 'final.webp', 85);
// Clean up
unlink('temp.jpg');
// Add watermark
$processor->addWatermark('photo.jpg', 'temp.jpg', 'logo.png', 'bottom-right', 10);
// Convert to WebP
$processor->convertToWebP('temp.jpg', 'final.webp', 85);
// Clean up
unlink('temp.jpg');
// Resize, watermark, and convert
$processor->resizeImage('original.jpg', 'temp1.jpg', 1920, 1080);
$processor->addWatermark('temp1.jpg', 'temp2.jpg', 'logo.png', 'bottom-right', 10);
$processor->convertToWebP('temp2.jpg', 'final.webp', 85);
// Clean up
unlink('temp1.jpg');
unlink('temp2.jpg');