addOpacity()
Adjusts the opacity/transparency of an image. Output is always PNG to preserve transparency.
Signature
Section titled “Signature”public function addOpacity( string $inputImagePath, string $outputImagePath, int $opacityPercent): voidParameters
Section titled “Parameters”$inputImagePath (string, required)
Section titled “$inputImagePath (string, required)”Path to the input image file. Supports JPEG, PNG, and GIF.
$outputImagePath (string, required)
Section titled “$outputImagePath (string, required)”Path where the output PNG will be saved. Always outputs PNG format.
$opacityPercent (int, required)
Section titled “$opacityPercent (int, required)”Opacity level (0-100).
0- Fully transparent (invisible)50- Semi-transparent100- Fully opaque (no transparency)
Values outside 0-100 are automatically clamped.
Return Value
Section titled “Return Value”Returns void. Creates the output PNG file.
Behavior
Section titled “Behavior”Opacity Calculation
Section titled “Opacity Calculation”The method applies the opacity to all pixels in the image:
- Creates a transparent canvas
- Copies the original image with specified opacity
- Saves as PNG to preserve transparency
Output Format
Section titled “Output Format”Always outputs PNG, regardless of input format:
- JPEG → PNG
- PNG → PNG
- GIF → PNG
Examples
Section titled “Examples”Basic Usage
Section titled “Basic Usage”use Kenura\Imagick\ImageProcessor;
$processor = new ImageProcessor();
// Make image 50% transparent$processor->addOpacity('image.jpg', 'transparent.png', 50);Create Watermark
Section titled “Create Watermark”// Create semi-transparent logo for watermarking$processor->addOpacity('logo.png', 'watermark.png', 40);
// Use as watermark$processor->addWatermark( 'photo.jpg', 'watermarked.jpg', 'watermark.png', 'bottom-right', 10);Different Opacity Levels
Section titled “Different Opacity Levels”// Very transparent$processor->addOpacity('image.jpg', 'very-transparent.png', 20);
// Semi-transparent$processor->addOpacity('image.jpg', 'semi-transparent.png', 50);
// Slightly transparent$processor->addOpacity('image.jpg', 'slightly-transparent.png', 80);
// Fully opaque (no change)$processor->addOpacity('image.jpg', 'opaque.png', 100);Batch Processing
Section titled “Batch Processing”$images = glob('images/*.jpg');
foreach ($images as $image) { $filename = pathinfo($image, PATHINFO_FILENAME); $processor->addOpacity($image, "transparent/{$filename}.png", 60);}Opacity Scale
Section titled “Opacity Scale”| Value | Description | Use Case |
|---|---|---|
| 0-20 | Very transparent | Ghost effects, subtle overlays |
| 20-40 | Mostly transparent | Watermarks, background patterns |
| 40-60 | Semi-transparent | Overlays, fade effects |
| 60-80 | Slightly transparent | Subtle effects |
| 80-100 | Nearly/fully opaque | Minimal transparency |
Best Practices
Section titled “Best Practices”1. Use PNG Extension
Section titled “1. Use PNG Extension”// ✅ Good: Clear that output is PNG$processor->addOpacity('image.jpg', 'output.png', 50);
// ⚠️ Confusing: Output is still PNG despite .jpg extension$processor->addOpacity('image.jpg', 'output.jpg', 50);2. Validate Opacity Range
Section titled “2. Validate Opacity Range”// Values are automatically clamped, but validate for clarity$opacity = max(0, min(100, $userInput));
$processor->addOpacity('image.jpg', 'output.png', $opacity);3. Consider File Size
Section titled “3. Consider File Size”// PNG files can be larger than JPEG// Original JPEG: 500KB$processor->addOpacity('photo.jpg', 'transparent.png', 50);// Output PNG might be: 800KB+Common Use Cases
Section titled “Common Use Cases”1. Watermark Creation
Section titled “1. Watermark Creation”// Create transparent watermark$processor->addOpacity('logo.png', 'watermark.png', 50);2. Overlay Effects
Section titled “2. Overlay Effects”// Create semi-transparent overlay$processor->addOpacity('texture.jpg', 'overlay.png', 30);3. Fade Effects
Section titled “3. Fade Effects”// Create faded version$processor->addOpacity('image.jpg', 'faded.png', 60);Common Issues
Section titled “Common Issues”Issue: Output file is JPEG, not PNG
Section titled “Issue: Output file is JPEG, not PNG”Cause: You specified .jpg extension
Solution: Output is always PNG. Use .png extension:
// Even with .jpg extension, output is PNG$processor->addOpacity('input.jpg', 'output.jpg', 50);
// Better: use .png extension$processor->addOpacity('input.jpg', 'output.png', 50);Issue: File size increased
Section titled “Issue: File size increased”Cause: PNG files are often larger than JPEG
Solution: This is expected when converting JPEG to PNG with transparency
Issue: No visible change at 100% opacity
Section titled “Issue: No visible change at 100% opacity”Cause: 100% opacity means fully opaque (no transparency)
Solution: Use lower values for transparency:
// No change$processor->addOpacity('image.jpg', 'output.png', 100);
// Transparent$processor->addOpacity('image.jpg', 'output.png', 50);Related Methods
Section titled “Related Methods”addWatermark()- Use transparent images as watermarksresizeImage()- Resize before adjusting opacity
See Also
Section titled “See Also”- Opacity Guide - Detailed guide with examples
- Watermarks Guide - Create transparent watermarks
- ImageProcessor Class - Full class reference