1

I'm using this function to add text with shadow on a picture:

function addWatermark($image, $text){
        // Schriftdatei (TrueType Font)
    $fontPath = __DIR__ . "/NotoSansSymbols-Regular.ttf";

    // Text und Schriftgröße
    $fontSize = 18; // in Punkten

    // Farben definieren
    $white = imagecolorallocate($image, 255, 255, 255);
    $black = imagecolorallocate($image, 0, 0, 0);

    // Bildgröße
    $imageWidth = imagesx($image);
    $imageHeight = imagesy($image);

    if($imageWidth < 550){
        $fontSize = 16;
    }

    // Textgröße berechnen
    $bbox = imagettfbbox($fontSize, 0, $fontPath, $text);
    $textWidth = abs($bbox[2] - $bbox[0]);
    $textHeight = abs($bbox[7] - $bbox[1]);

    // Padding (20% des Bildes)
    $paddingY = 10;
    $paddingX = 10;

    // Obere rechte Position berechnen
    $x = $imageWidth - $textWidth - $paddingX;
    $y = $paddingY + $textHeight; // Y-Koordinate ist baseline

    // Schwarzen Rand dünner zeichnen (nur oben, unten, links, rechts)
    imagettftext($image, $fontSize, 0, $x - 1, $y, $black, $fontPath, $text); // links
    imagettftext($image, $fontSize, 0, $x + 1, $y, $black, $fontPath, $text); // rechts
    imagettftext($image, $fontSize, 0, $x, $y - 1, $black, $fontPath, $text); // oben
    imagettftext($image, $fontSize, 0, $x, $y + 1, $black, $fontPath, $text); // unten

    // Weißen Text darüber
    imagettftext($image, $fontSize, 0, $x, $y, $white, $fontPath, $text);

    return $image;
}

I want to add opacity of 70% to the final text, how can I do that?

1
  • 1
    The text is written four times in black, slightly offset each time, and then overlaid with a white version. If you made the white text have 70% opacity, then you will see the black text underneath through it. You'll probably need to paint this text into a secondary image with transparent background first, and then copy it over your final image, applying opacity in that step, sth. like imagecopyresampled with opacity Commented Sep 17 at 6:49

1 Answer 1

1

You may apply opacity to the text image by using the imagecolorallocatealpha function.

imagecolorallocatealpha() behaves identically to imagecolorallocate() with the addition of the transparency parameter alpha.

See official documentation

For example, setting alpha as 89 for nearly 70% transparency (127 is completely transparent), and setting as 64 will be 50% transparency

$color = imagecolorallocatealpha($image, 0, 0, 0, 64);

On the other hand, for having drop shadow on text, better refer to official documentation on imagettftext . See here (see example #1)

So a fully working example is:

<?php

header("Content-Type: image/jpeg");

function addWatermark($image, $text){

    $fontPath = "arial.ttf";

    $fontSize = 100; 

    $white = imagecolorallocatealpha($image, 255, 255, 255,64);
    $black = imagecolorallocatealpha($image, 0, 0, 0,64);

    $imageWidth = imagesx($image);
    $imageHeight = imagesy($image);

    if($imageWidth < 550){
        $fontSize = 16;
    }

    $bbox = imagettfbbox($fontSize, 0, $fontPath, $text);
    $textWidth = abs($bbox[2] - $bbox[0]);
    $textHeight = abs($bbox[7] - $bbox[1]);

    $paddingY = 10;
    $paddingX = 10;

    $x = $imageWidth - $textWidth - $paddingX;
    $y = $paddingY + $textHeight; // Y-Koordinate ist baseline


//    imagettftext($image, $fontSize, 0, $x - 1, $y, $black, $fontPath, $text); // links
//    imagettftext($image, $fontSize, 0, $x + 1, $y, $black, $fontPath, $text); // rechts
//    imagettftext($image, $fontSize, 0, $x, $y - 1, $black, $fontPath, $text); // oben
//    imagettftext($image, $fontSize, 0, $x, $y + 1, $black, $fontPath, $text); // unten


imagettftext($image, $fontSize, 0, $x+1, $y+1, $black, $fontPath, $text);

imagettftext($image, $fontSize, 0, $x, $y, $white, $fontPath, $text);

//    return  $image ;
    Imagejpeg($image, '', 100);
}

$imagePath = './earth.jpg';
$image = imagecreatefromjpeg($imagePath);
 
addWatermark($image, 'Happy');


?>

See demo

and See demo with no opacity set , i.e. without changing imagecolorallocate() to imagecolorallocatealpha()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.