• PHP Help
  • Creating Image over watermark w/text

The following code outputs the watermark and overlay text, but not the other image, bwshoes.png.


<?php
//First we have to set the output of the page.
//Here we set it as image. Because we are going to show an image directly
header('content-type: image/jpeg');

//Then we set the image and watermark image.
$source_image = 'image.jpg';
//Important thing is we need to create watermark image as png image.
//Else the transparent ares will be white colored in our watermark image area.
//And also we need to use a png image to show transparency.
$my_watermark = imagecreatefrompng('bwshoes.png');

//Then we will get the width and height of the watermark image.
//We will use these dimensions to locate it fit position.
$watermark_width = imagesx($my_watermark);
$watermark_height = imagesy($my_watermark);

//So now we are going to create our image component from the source image.
$image = imagecreatefromjpeg($source_image);
//and we get its dimensions
$image_size = getimagesize($source_image);

//Extra we need to configure the watermark text 
//and font type, font size, color and text angle
$watermarktext = "Thecodeprogram";
$font = "ahronbd.ttf";
$fontsize = "25";
$color = imagecolorallocate($image2, 255, 255, 255);
$textangle = 0;


//Then we specify the position of the watermark.
//We will locate it right under corner of the image with 20px margin from edges.
$wm_a = $image_size[0] - $watermark_width - 94;
$wm_b = $image_size[1] - $watermark_height - 94;
$wm_x = $image_size[2] - $watermark_width - 94;
$wm_y = $image_size[3] - $watermark_height - 78;

//And then we write the text on the image.
imagettftext($image, $fontsize, $textangle, $wm_a, $wm_b, $color, $font, $watermarktext);


//Then we are copying the watermark image on created image component with configured locations and dimensions.
imagecopy($image, $my_watermark, $wm_x, $wm_y, $watermark_width, $watermark_height);


//Lastly show the image on the screen. Do not forget to set header as image of the page.
//echo '<div style="border-width:3px; border-style:solid; border-color:white;">'.imagejpeg($image).'</div>';


//Then show it on the page
imagejpeg($image);
imagedestroy($image);


?>

    According to your code, bwshoes.png is the watermark.

    Warning: Undefined variable $image2
    Uncaught TypeError: Unsupported operand types: string - int

    That's from the line where you try to use $image_size[3]. As the manual says, " Index 3 is a text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag. "

    Uncaught ArgumentCountError: imagecopy() expects exactly 8 arguments, 6 given

    I'm pretty sure there should be a couple of zeros in that line (to specify the top left corner of the watermark).

      Write a Reply...