font issue in watermark text on image in php - php

I have problem in watermark text on image in php
my code is
$im='myimage.jepeg'
header ("Content-type: image/jpeg");
$string = "text on image";
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefromjpeg($im);
$x = imagesx($im) - $width ;
$y = imagesy($im) - $height;
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
$textColor = imagecolorallocate ($im, 0, 0,0);
imagettftext ($im, $font, $x, $y, $string, $textColor);
imagejpeg($im);
i have change fonts size but can't work

There is no extension jepeg as you are using in your script. So replace the below script
$im='myimage.jepeg';
To
$im='myimage.jpeg';
Also you need to careful about semicolon ; php script will be stop and return error if missed to put the semicolon at the end of script.

Related

Write the output of a (shoutcast status) script to an image

I'm trying to write the following output of a (shoutcast status) script to an image using GD but it's not working. What am I doing wrong?
$string = "/home/test.txt";
Using the above just displays the path to the file not it's content.
output:
psytrance.value 37
breaks.value 8
dubstep.value 6
reggae.value 130
oldskool.value 5
ambient.value 81
test.value <- this should be ignored!
complete.value 267
php:
<?php
header ("Content-type: image/png");
$string = "/home/test.txt";
// try changing this as well
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("/home/banner2.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);
?>
The shoutcast Status is saved in test.txt? Then you have to write the content of the file into your PNG.
$content = file_get_contents ($string);
[...]
$lines = explode("\n", $content);
foreach ($lines as $line) {
if (strstr("test.value", $line) !== false) continue;
imagestring ($im, $font, $x, $y, $string, $textColor);
$y += 20;
}

Gd Library Image Style imagettftext

How to make Like This Image?
I just want to learn how do i get output like this using GD library,
Thanks for your help guys..
Here is your code, you could easily find it on google tho.
<?php
header ("Content-type: image/png");
$string = "your text"; // Change this text
$font = 4; // try changing this as well
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("/path/to/yourimagefile");
$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);
?>
Here's an example:
header('Content-Type: image/png'); // setting the content-type
$file = "yourimage.png";
$image = imagecreatefrompng($file); // creating the image
$font = "YourFont.ttf";
$size = 15; //pixels
$color = imagecolorallocate($image, 255, 255, 255); //white color
$text = "Your text here"
imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text
imagepng($image); // outputting the image
For more see imagettftext().
EDIT: An example of using multiple imagettftext():
header('Content-Type: image/png'); // setting the content-type
$file = "yourimage.png";
$image = imagecreatefrompng($file); // creating the image
$font = "YourFont.ttf";
$size = 15; //pixels
$color = imagecolorallocate($image, 255, 255, 255); //white color
$text = "Your text here"
imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text
$text = "Text 2";
imagettftext($image, 15, 0, 25, 45, $color, $font, $code); // adding the text
$text = "Text 3";
imagettftext($image, 15, 0, 30, 50, $color, $font, $code); // adding the text
imagepng($image); // outputting the image

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);

turning text into image - PHP/GD - save image

I'm using this script to simply create an image from text. What I would like to know is how to save the image instead of printing straight to browser;
// an email address in a string
$string = $post[$key];
// some variables to set
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
// lets begin by creating an image
$im = #imagecreatetruecolor ($width,$height);
//white background
$background_color = imagecolorallocate ($im, 255, 255, 255);
//black text
$text_color = imagecolorallocate ($im, 0, 0, 0);
// put it all together
$image = imagestring ($im, $font, 0, 0, $string, $text_color);
I know its probably just one line of code at the end but im not sure which GD function to use.
Any help would be much appreciated, Regards, Phil.
EDIT:
I have tried the following but just get a balnk black box;
// an email address in a string
$string = $post[$key];
// some variables to set
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
// lets begin by creating an image
$im = #imagecreatetruecolor ($width,$height);
//white background
$background_color = imagecolorallocate ($im, 255, 255, 255);
//black text
$text_color = imagecolorallocate ($im, 0, 0, 0);
// put it all together
imagestring ($im, $font, 0, 0, $string, $text_color);
imagepng($im, 'somefile.png');
imagedestroy($im);
Pass a filename to the appropriate image-generating image*() function:
imagepng($image, 'somefile.png');
Look at here http://www.php.net/manual/en/function.imagepng.php
take a look at imagepng() (or imagegif, imagejpeg...) - if you add a filename as second parameter, the image is saved as file.

not Display text

<?php
header ("Content-type: image/pjpeg");
$string = "manujagadeesh!!!";
$font =8;
$width = ImageFontWidth($font)* strlen($string) ;
$height = ImageFontHeight($font) ;
$im = ImageCreateFromjpeg("sachin.jpg");
$x=100;
$y=200;
$background_color = imagecolorallocate ($im, 255, 255, 255); //white background
$text_color = imagecolorallocate ($im, 0, 0,0);//black text
imagestring ($im, $font, $x, $y, $string, $text_color);
imagejpeg ($im);
?>
this is the add text to image in php
wen we inclue the my html page the text is not displaying
for eg
<?php
header ("Content-type: image/pjpeg");
$string = "manujagadeesh!!!";
$font =8;
$width = ImageFontWidth($font)* strlen($string) ;
$height = ImageFontHeight($font) ;
$im = ImageCreateFromjpeg("sachin.jpg");
$x=100;
$y=200;
$background_color = imagecolorallocate ($im, 255, 255, 255); //white background
$text_color = imagecolorallocate ($im, 0, 0,0);//black text
imagestring ($im, $font, $x, $y, $string, $text_color);
imagejpeg ($im);
?>
hi welcome
couldn't see the hi wlecome
You can't output both content of type image/jpeg and text/html from the same PHP script. Move your image generation code to another file eg stringImage.php and use the following code to include it:
<img src="stringImage.php" />
hi welcome
As long as stringImage.php only outputs the image data with the correct content type, and nothing else, you should get your expected result.

Categories