Watermarking: Display the same text on multiple parts of the image - php

I want to add a text to a image. The text should be displayed in multiple areas of the image (not just one).
For example I want to watermark with a text stack. Stack should be displayed in the image at least 8 times in different areas in the image.
I just learned about imagestring() and imagettftext(), but these two only displays my text on a single spot.
Image is not fixed size, so i cannot specify exact and multiple location in advance. It should work on all sizes of images
<?php
/*
image.php
*/
header("Content-type: image/jpeg");
$imgPath = 'olximage.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
$string = "stack overflow";
$fontSize = 3;
$x = 15;
$y = 185;
imagestring($image, $fontSize, $x, $y, $string, $color);
$x = 15;
$y = 175;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image);
?>
Thanks in advance

For example:
<?php
/*
image.php
*/
header("Content-type: image/jpeg");
$imgPath = 'olximage.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
$string = "stack overflow";
$fontSize = 3;
$imageHeight = imagesy($image);
$distanceY = 10;
$maxImageStrings = max(8, $imageHeight / $distanceY);
$x = 15;
for ($i = 0; $i < $maxImageStrings; $i++) {
$y = $i * $distanceY;
imagestring($image, $fontSize, $x, $y, $string, $color);
}
imagejpeg($image);
You can finetune calculations for your needs.

I'm using Imagick extension for same. If you want to go with this then follow detail:
PHP:
// Create objects
$image = new Imagick('image.png');
$watermark = new Imagick();
// Watermark text
$text = 'Copyright';
// Create a new drawing palette
$draw = new ImagickDraw();
$watermark->newImage(140, 80, new ImagickPixel('none'));
// Set font properties
$draw->setFont('Arial');
$draw->setFillColor('grey');
$draw->setFillOpacity(.5);
// Position text at the top left of the watermark
$draw->setGravity(Imagick::GRAVITY_NORTHWEST);
// Draw text on the watermark
$watermark->annotateImage($draw, 10, 10, 0, $text);
// Position text at the bottom right of the watermark
$draw->setGravity(Imagick::GRAVITY_SOUTHEAST);
// Draw text on the watermark
$watermark->annotateImage($draw, 5, 15, 0, $text);
// Repeatedly overlay watermark on image
for ($w = 0; $w < $image->getImageWidth(); $w += 140) {
for ($h = 0; $h < $image->getImageHeight(); $h += 80) {
$image->compositeImage($watermark, Imagick::COMPOSITE_OVER, $w, $h);
}
}
// Set output image format
$image->setImageFormat('png');
// Output the new image
header('Content-type: image/png');
echo $image;
although there are plenty of command-line examples to be found on the ImageMagick website, so that is where we shall begin.

Related

Write text over image dynamically and center the text

I have a script that is writing a text over an image.
It works fine, but the text is aligned to the left.
I am trying to center the text horizontally, but not sure how can I do that.
Here is the code that I have at the moment
//content type
header('Content-Type: image/png');
if (isset($_GET['text'])){$text = $_GET['text'];}else{$text = "";}
//font
$font = 'Ubuntu-Medium.ttf';
//font size
$font_size = 18;
//image width
$width = 400;
//text margin
$margin = 90;
//explode text by words
$text_a = explode(' ', $text);
$text_new = '';
foreach($text_a as $word){
//Create a new text, add the word, and calculate the parameters of the text
$box = imagettfbbox($font_size, 0, $font, $text_new.' '.$word);
//if the line fits to the specified width, then add the word with a space, if not then add word with new line
if($box[2] > $width - $margin*2){
$text_new .= "\n".$word;
} else {
$text_new .= " ".$word;
}
}
//trip spaces
$text_new = trim($text_new);
//new text box parameters
$box = imagettfbbox($font_size, 0, $font, $text_new);
//new text height
$height = $box[1] + $font_size + $margin * 2;
//create image
$im = imagecreatefromjpeg('source.jpg');
//create colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 51, 95, 32);
//color image
imagefilledrectangle($im, 0, 0, $width, $black);
// determine the size of the text so we can center it
$box = imagettfbbox($font_size, 0, $font, $text_new);
$text_width = abs($box[2]) - abs($box[0]);
$text_height = abs($box[5]) - abs($box[3]);
$image_width = imagesx($im);
$image_height = imagesy($im);
$x = ($image_width - $text_width) / 2;
$y = ($image_height + $text_height) / 2;
//add text to image
imagettftext($im, $font_size, 0, $x, $y, $black, $font, $text_new);
//return image
imagepng($im);
//frees any memory associated with image
imagedestroy($im);
Any ideas on how can I center the text?
I already centered the box that the text is placed, so the box is in the center of the image. The problem is that the text is aligned left and I need it in the center.

PHP error when creating image using imagecreatetruecolor

I got a simple HTML that calls a image generated from a PHP file but the image is not being displayed. The HTML part is:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<img src="imgcaptcha.php"/>
</body>
</html>
imgcaptcha.php is:
<?php
//error_reporting(E_ALL);
//header('Content-Type: image/png');
//Generates rando string
function geraStringAleatoria($length = 10) {
$charSet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$stringAleatoria = '';
for ($i = 0; $i < $length; $i++) {
$stringAleatoria .= $charSet[rand(0, strlen($charSet) - 1)];
}
return $stringAleatoria;
}
//Generate the captcha image
function geraImagemCaptcha($text = 'good'){
// Set the content-type
$width = 200;
$height = 30;
// Create the image
$im = imagecreatetruecolor($width, $height) or die('display nao instalado');;
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, $width-1, $height-1, $white);
//ADD NOISE - DRAW background squares
$square_count = 6;
for($i = 0; $i < $square_count; $i++){
$cx = rand(0,$width);
$cy = (int)rand(0, $width/2);
$h = $cy + (int)rand(0, $height/5);
$w = $cx + (int)rand($width/3, $width);
imagefilledrectangle($im, $cx, $cy, $w, $h, $white);
}
//ADD NOISE - DRAW ELLIPSES
$ellipse_count = 5;
for ($i = 0; $i < $ellipse_count; $i++) {
$cx = (int)rand(-1*($width/2), $width + ($width/2));
$cy = (int)rand(-1*($height/2), $height + ($height/2));
$h = (int)rand($height/2, 2*$height);
$w = (int)rand($width/2, 2*$width);
imageellipse($im, $cx, $cy, $w, $h, $grey);
}
// Replace path by your own font path
$font = 'flushout.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
}
geraImagemCaptcha(geraStringAleatoria());
?>
I'm using PHP 7
Tried already commenting out header(); call and add error_reporting(E_ALL); but no error message was displayed. Tried also many other different font.ttf files according to this, also didn't worked, so I guess that the problem is in another part of the code rather than the font because I tried many sites and also some other .ttf files I got from other programs installed. The problem is happening in both all browsers I tested. What could be missing in the code? Btw, I got this generate captcha image from here.

Create round png image in PHP using transparency and center a letter in it

I'd like to create profile images for my website. The image should be rounded, a solid background color and a single big letter exactly at the center.
So far I've been able to create a square png image and a letter in it but it's not centered. Due to the fact that different letters have different width I get some letters slightly off-center.
I'm going to implement"round" png images in PHP using transparency.
This is the complete script and should work out of the box. You need Arial.tff if you want to use a custom font but it's not strictly needed.
There is a lookup table with some colors. The single letter get hashed (yeah, pretty stupid I know, but this way it's more flexible as I could pass any string I like) and a color is picked.
<?php
$text = "a";
$posx = 40;
$posy = 150;
$size = 120;
$displaytext = strtoupper($text);
$image = imagecreatetruecolor(200, 200);
// pick a background at random
$binhash = md5($text, true);
$numhash = unpack('N2', $binhash);
$index = abs($numhash[1]) % 16;
$palette = array(
array(0xde,0x5a,0xa4),
array(0xae,0xc6,0xcf),
array(0x96,0x6f,0xd6),
array(0xff,0xb3,0x47),
array(0xff,0x69,0x61),
array(0x77,0xdd,0x77),
array(0x03,0xc0,0x3c),
array(0xcf,0xcf,0xc4),
array(0xc2,0x3b,0x22),
array(0xfd,0xfd,0x96),
array(0x83,0x69,0x53),
array(0x77,0x9e,0xcb),
array(0xb1,0x9c,0xd9),
array(0xb3,0x9e,0xb5),
array(0xf4,0x9a,0xc2),
array(0xff,0xd1,0xdc)
);
$r = $palette[$index][0];
$g = $palette[$index][1];
$b = $palette[$index][2];
$image = imageCreate(200, 200);
imageColorAllocate($image, $r, $g, $b);
$color = imageColorAllocate($image, 255, 255, 255);
$font = 'arial.ttf';
$box = imagettfbbox(200, 0, "ARIAL", $displaytext);
$width = abs($box[2] - $box[0]);
$height = abs($box[5] - $box[1]);
$image = imageCreate(200, 200);
imageColorAllocate($image, $r, $g, $b);
$color = imageColorAllocate($image, 255, 255, 255);
$font = 'arial.ttf';
imagettftext($image, 200, 0, 0 + (200/2 - floor($width/2)), 200, $color, "ARIAL", $displaytext);
header("content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
How can I modify such code in order to:
Have round corner. Shall I create a transparent background and a solid disc at the center?
Have the letter centered, no matter which letter it is
This can help you for centering the letter
<?php
$im = new Imagick(); /* Create a new Imagick object */
$draw = new ImagickDraw(); /* Create an ImagickDraw object */
$draw->setFont('/path/to/font.ttf'); /* Set the font */
var_dump($im->queryFontMetrics($draw, "K")); /* Dump the font metrics*/
?>
Using fontmetrics you can calculate the width and height of a specific letter/text with specific font and you can access the properties.
Hope that will help you.

php change font of ImageString()

I am working with my little project which need the following:
There must be a text at the front of image.
The text is on centered.
My code follows the instruction but i want to change the font family of the text..
ImageString($background, $font_size, $position_center, $position_middle, $text, $textcolor);
Here is my full code:
header('Content-type: image/png');
$background1 = "files/bg.png";
$background = imagecreatefrompng($background1);
$width = 630;
$height = 315;
$text = "EXAMPLE TEXT";
$font_size = 50;
$font_family = "files/tahoma.ttf";
$font_width = ImageFontWidth($font_size);
$font_height = ImageFontHeight($font_size);
$text_width = $font_width * strlen($text);
$position_center = ceil(($width - $text_width) / 2); //CENTER THE TEXT
$text_height = $font_height;
$position_middle = ceil(($height - $text_height) / 2); // MIDDLE THE TEXT
$textcolor = ImageColorAllocate($background, 255, 255, 255);
$image_string = ImageString($background, $font_size, $position_center, $position_middle, $text, $textcolor);
imagepng($background);
imagedestroy($background);
Have a look at the last 2 statements, $image_string is not used.
imagettftext can be used to write text to an image using a TrueType font.
The position parameters for the functino refers to the left edge, to get it centered you need to calculate the correct coordinates, take a look at this php.net comment for how to do it: http://no2.php.net/manual/en/function.imagettftext.php#83248

imagecreatefrompng makes imported image block-ish?

I'm trying to use the following PHP in order to apply randomly generated text files to an image. (I'm just using a random image right now.)
<?php
header ("Content-type: image/png");
$textfile = "quote.txt";
$quotes = array();
if(file_exists($textfile)){
$quotes = file($textfile);
srand ((float) microtime() * 10000000);
$string = $quotes[array_rand($quotes)];
$string = substr($string,0,strlen($string)-1);
}
else{
$string = "No 'Quote' available at this time.";
}
//$string = "your text";
// try changing this as well
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("test.png");
$x = imagesx($im) - $width ;
$y = imagesy($im) - $height;
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
$textColor = imagecolorallocate ($im, 0, 0,0);
imagestring ($im, $font, $x, $y, $string, $textColor);
imagepng($im);
ImageDestroy($im);
?>
However, when I run that code the imported image just becomes very blockish.
Here is the image I am testing with:
http://i.stack.imgur.com/LhNkv.png
And here is how it actually appears:
http://i.stack.imgur.com/AAcHZ.png
My research shows that "imagecreate" generates a "palette" image - and I thought that might have something to do with my error, but I have seen plenty of examples where the base image is by no means distorted.
Thanks in advance for your insights.
(Ugh. It wouldn't let me post images but let me upload them just fine?)
Update
Changing the code to:
<?php
header ("Content-type: image/png");
$textfile = "quote.txt";
$quotes = array();
if(file_exists($textfile)){
$quotes = file($textfile);
srand ((float) microtime() * 10000000);
$string = $quotes[array_rand($quotes)];
$string = substr($string,0,strlen($string)-1);
}
else{
$string = "No 'Quote' available at this time.";
}
//$string = "your text";
// try changing this as well
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("test.png");
//$x = imagesx($im) - $width ;
//$y = imagesy($im) - $height;
//$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
//$textColor = imagecolorallocate ($im, 0, 0,0);
//imagestring ($im, $font, $x, $y, $string, $textColor);
imagepng($im);
ImageDestroy($im);
?>
Produces the same block-like effects as above, except now no text is being written to the image, either (obviously?).
Could be an alpha blending problem. Try adding these before saving the image:
imagealphablending($im, true);
imagesavealpha($im, true);

Categories