www.xbdev.net
xbdev - software development
Tuesday April 14, 2026
Home | Contact | Support | PHP... a powerful, flexible, fully supported, battle tested server side language ..
     
 

PHP...

a powerful, flexible, fully supported, battle tested server side language ..

 

Overlay Images


Simple little function to generate versions of the image with an overlay in the corner (e.g., tick or a cross).


function addShapeOverlay($sourceImagePath, $shape = 'tick', $color = [0, 200, 0], $sizeFactor = 0.15) {
    if (!file_exists($sourceImagePath)) {
        return false;
    }

    // Define cache directory
    $cacheDir = './cache/';
    if (!is_dir($cacheDir)) {
        mkdir($cacheDir, 0777, true);
    }

    $outputPath = $cacheDir . 'overlay_' . basename($sourceImagePath);

    // Load the image
    $imageInfo = getimagesize($sourceImagePath);
    $width = $imageInfo[0];
    $height = $imageInfo[1];

    switch ($imageInfo['mime']) {
        case 'image/jpeg':
            $image = imagecreatefromjpeg($sourceImagePath);
            break;
        case 'image/png':
            $image = imagecreatefrompng($sourceImagePath);
            break;
        default:
            return false;
    }

    // Define shape size based on the image size
    $shapeSize = min($width, $height) * $sizeFactor;
    $padding = $shapeSize * 0.2;
    $xPosition = $width - ($shapeSize + $padding); // Move to right side
    $yPosition = $padding;

    // Custom shape color
    $shapeColor = imagecolorallocate($image, $color[0], $color[1], $color[2]);

    // Draw selected shape
    switch ($shape) {
        case 'tick':
            imageline($image, $xPosition, $yPosition + ($shapeSize * 0.5), $xPosition + ($shapeSize * 0.4), $yPosition + $shapeSize, $shapeColor);
            imageline($image, $xPosition + ($shapeSize * 0.4), $yPosition + $shapeSize, $xPosition + $shapeSize, $yPosition, $shapeColor);
            break;

        case 'cross':
            imageline($image, $xPosition, $yPosition, $xPosition + $shapeSize, $yPosition + $shapeSize, $shapeColor);
            imageline($image, $xPosition, $yPosition + $shapeSize, $xPosition + $shapeSize, $yPosition, $shapeColor);
            break;

        case 'question':
            imagearc($image, $xPosition + ($shapeSize / 2), $yPosition + ($shapeSize / 2), $shapeSize, $shapeSize, 10, 200, $shapeColor);
            imageline($image, $xPosition + ($shapeSize * 0.5), $yPosition + ($shapeSize * 0.8), $xPosition + ($shapeSize * 0.5), $yPosition + $shapeSize, $shapeColor);
            break;

        case 'star':
            $points = [
                $xPosition + ($shapeSize * 0.5), $yPosition,
                $xPosition + ($shapeSize * 0.6), $yPosition + ($shapeSize * 0.4),
                $xPosition + $shapeSize, $yPosition + ($shapeSize * 0.4),
                $xPosition + ($shapeSize * 0.7), $yPosition + ($shapeSize * 0.7),
                $xPosition + ($shapeSize * 0.8), $yPosition + $shapeSize,
                $xPosition + ($shapeSize * 0.5), $yPosition + ($shapeSize * 0.85),
                $xPosition + ($shapeSize * 0.2), $yPosition + $shapeSize,
                $xPosition + ($shapeSize * 0.3), $yPosition + ($shapeSize * 0.7),
                $xPosition, $yPosition + ($shapeSize * 0.4),
                $xPosition + ($shapeSize * 0.4), $yPosition + ($shapeSize * 0.4)
            ];
            imagefilledpolygon($image, $points, count($points) / 2, $shapeColor);
            break;
    }

    // Save the modified image
    imagepng($image, $outputPath);
    imagedestroy($image);

    return $outputPath;
}

// Usage Example
$modifiedImage = addShapeOverlay("./cover.jpg", "star", [255, 200, 0], 0.2);
echo "Overlay image saved at: " . $modifiedImage;






 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2026 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.