2

I have an image on my server and I want to write text to it. Like a watermark. I am able to write text to the image, but I want to add a background to the text so it's easy to read. Here is what I have so far.

header("Content-type: image/jpeg");
$imgPath = 'pic.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 224, 73, 87);
$string = "Please type the word in the circle.";
$fontSize = 8;
$x = 25;
$y = 200;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image);
Sandesh
  • 1,190
  • 3
  • 23
  • 41
Daniel
  • 4,202
  • 11
  • 50
  • 68
  • You can check here: http://stackoverflow.com/questions/2235152/add-watermark-to-images-with-php Might answer your question. – Bat_Programmer Mar 30 '13 at 06:30

2 Answers2

1

in this class you need to have a background base.png and font arial.ttf you can have a diffident font but must be a ttf .if you want to have diffident font format you must make change on code

 class SecurityImg{
    static function Image_Create($basename){//Create image
       $im =imagecreatefrompng ($basename); 
       //only replace imagecreatefrompng with imagecreatefromjpeg for open jpg instead of png 
       return($im);
    }
    static function PutTextOnImage($text,$baseimage,$angel,$xi,$yi){
       // Create some colors
       $text_color= imagecolorallocate($baseimage, 255, 50, 150);
       // Replace path by your own font path
       $font = 'arial.ttf';
       // Add the text
       imagettftext($baseimage, 15, $angel, $xi, $yi, $text_color, $font, $text);
       return($baseimage);

     }
     static function Create($imgbase,$TEXT){
          $ifp=self::Image_Create($imgbase);
          $im=self::PutTextOnImage($TEXT,$ifp,0,10,20);
          return($im);
     }
  }
  $Securityimg=new SecurityImg(); 
  $im=$Securityimg->Create("base.png","test");
  // Output the image
  // Set the content-type
  header('Content-Type: image/jpeg');

  imagejpeg($im);
  // Using imagepng() results in clearer text compared with imagejpeg()
  imagedestroy($im);
mohammad mohsenipur
  • 3,218
  • 2
  • 17
  • 22
0

Check out this :

<?php

$stamp = imagecreatefrompng('stampimg.png');
$im = imagecreatefrompng('mainimage.png');


$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

$imgx = imagesx($im);
$imgy = imagesy($im);
$centerX=round($imgx/2);
$centerY=round($imgy/2);


imagecopy($im, $stamp, $centerX, $centerY, 0, 0, imagesx($stamp), imagesy($stamp));


header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Sandesh
  • 1,190
  • 3
  • 23
  • 41