I am new in using Php with Images.
I had found the code to display Image
<<?php
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
// Save the image as 'simpletext.jpg'
imagejpeg($im);
// Free up memory
imagedestroy($im);
?>>
But firefox show some bogus code instead of that Image.here is the link to the Image that Firefox shows
http://tinypic.com/r/htw6cm/7
Here is the link to the Image
What are you seeing the the "bytes" of an image. By default your browser will think that your script is about to print some text. So you end up seeing some strange characters.
Since you are printing not text, but image, you need to tell the browser that the contents of your script will be an image. You can do this by sending a header like that:
<?php
header("Content-Type: image/jpeg"); // treat the script as an image
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
// Save the image as 'simpletext.jpg'
imagejpeg($im);
// Free up memory
imagedestroy($im);
Use
<?
header('Content-type: image/jpg');
at beginning of your script to send an header for images.
so the browser will handle the php output as jpg-image-data
header('Content-type: image/jpg');
not at beginning of your script, but right before starting actual output, i.e on the line above imagejpeg($im);
so, it will let you to see an error if any occurred
<?php
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
//send header
header('Content-type: image/jpg');
//output an image
imagejpeg($im);
also I removed some mistakes, errors and useless operators
Related
I need to write a function that returns a date:url picture with a random number, I’m absolutely confused, here are my attempts, tell me where to go next
$function = new \Twig\TwigFunction('picture', function () {
header("Content-Type: image/png");
$im = #imagecreate(50, 50)
or die("Невозможно создать поток изображения");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
$string = rand();
imagestring($im, 1, 5, 5, $string, $text_color);
imagepng($im);
imagedestroy($im);
$img64 = base64_encode($im);
return $img64;
});
$twig->addFunction($function);
Issues:
header("Content-Type: image/png"); doesn't make any sense. Your function returns plain text that isn't even meant to be returned to browser as-is but injected inside HTML. Get rid of this line.
The # in #imagecreate(50, 50) is the error control operator. By default it'll just hide useful error messages from you. You don't possibly want it either.
You define $background_color but never use it. You can feed it to imagefill() or, if you want it black, omit it altogether.
imagepng($im) immediately prints the image. That's bad when you want to process and return it. You can capture with e.g. output control functions.
base64_encode($im) triggers:
Warning: base64_encode() expects parameter 1 to be string, resource
You're incorrectly assuming that imagecreate() returns the final image bytes; it doesn't. It returns a resource to be passed as argument to other image functions. You don't need it for anything else. Additionally, if you didn't see the message it can mean that you haven't configured PHP to display error messages.
There's no need to imagedestroy(). The image is local to the function, builtin garbage collector will take care.
Don't store the image bytes in temporary variables for no reason, that will only increase memory consumption.
So you basically need to understand that your code is not a black box and ensure that every line you write has a purpose known to you and is used as the corresponding manual page explains.
$im = imagecreate(50, 50)
or die("Невозможно создать поток изображения");
$text_color = imagecolorallocate($im, 233, 14, 91);
$string = rand();
imagestring($im, 1, 5, 5, $string, $text_color);
ob_start();
imagepng($im);
return base64_encode(ob_get_clean());
I am trying to generate images containing data from my database, and I want to use a custom font for it, so I looked up the imagettftext. I am following its syntax, but it's not printing anything. What am I doing wrong?
When I use imagestring it works fine. But that does not use any custom font.
I have placed my font.tff file in the same folder as my script. Here is my code:
header('Content-Type: image/png');
$image = #imagecreate(400, 110);
$backgrund = imagecolorallocate($image, 70, 130, 180);
$textcolor = imagecolorallocate($image, 255, 255, 255);
$font = 'font.ttf';
// TTF
imagettftext($image, 10, 0, 15, 15, $textcolor, $font, 'Test TTF');
// Normal text
imagestring($image, 2, 15, 30, 'Test Normal Font', $textcolor);
imagepng($image, 'images/test.png');
imagedestroy($image);
Am I missing something? The first line is completely blank. I also tried adjusting the font size (it maybe is different than regular imagestring?) but still nothing. The font is a .ttf font file.
Website:
https://bimmr6696.000webhostapp.com/signs/Sign.php?Line1=&Line2=asd&Line3=&Line4=
PhpInfo:
https://bimmr6696.000webhostapp.com/signs/test.php
Currently I have the following code:
$img = LoadPNG('sign.png');
//$font = imageloadfont('minecraft.ttf');
// Add the text
imagettftext($img, 20, 0, 5,5, $black, 5, "Test Text");
imagestring($img, 5, 5, 50, 'Test Text', $text_color);
// Create image instances
$dest = imagecreatetruecolor(80, 40);
// Copy
imagecopy($dest, $img, 0, 0, 20, 13, 80, 40);
// Set the content type header - in this case image/png
header('Content-type: image/png');
// Output the image
imagejpeg($img);
imagejpeg($dest);
imagedestroy($dest);
imagedestroy($img);
When I use imagestring I'm able to get the text to show, but the text is too small which causes me to need imagettftext, although nothing shows when I use this. I've just about run out of ideas to try and solve this and so any help would be very appreciated.
TLDR: I need to either find out why imagettftext doesn't work or find a way to change font size with imagestring
Deleted everything then retyped it all out and for some reason that seemed to fix it... Because this isn't an actual answer I'm not going to post it below...
I tried to overlay some text on a youtube thumbnail using php GD, but that does'nt seem to work.
The code I have tried is:
<?php
$im = file_get_contents('http://i.ytimg.com/vi/6E9wBFl5o-c/mqdefault.jpg');
$image = imagecreatefromjpeg($im);
$font_size = 14;
$color = imagecolorallocate($image, 255,255,255);
$black = imagecolorallocate($image, 0,0,0);
// and now we do the overlay - the layers of text start top to bottom, so
// the drop shadow comes first
// $image - the base image file we specified above
// $font_size - Well duh. Its the size of the font
// 0 - the angle of the text - we don't want an angle, so we leave it at 0
// 56 - pixels to the right from the leftmost part of the image
// 36 - pixels down from the top of the image
// $black - the color we defined above
// "Test Text" - the text we're overlaying - you can also use a variable here
ImageTTFText ($image, $font_size, 0, 56, 36, $black, "font.ttf","Test Text");
// Now add the actual white text "on top"
ImageTTFText ($image, $font_size, 0, 55, 35, $color, "font.ttf","Test Text");
header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
So how can I get the image returned with text written on it.
imagecreatefromjpeg needs a filename as parameter, see http://php.net/manual/function.imagecreatefromjpeg.php
this should work:
$image = imagecreatefromjpeg('http://i.ytimg.com/vi/6E9wBFl5o-c/mqdefault.jpg');
To debug the script you can access the url in the browser. Any warnings or error messages will be readible.
A simple error is that you declare a JPG file in the header, but return a PNG file.
header("Content-type: image/jpg");
imagepng($image);
Also make sure that the font file is really accessible from the PHP file.
<?php
header("Content-Type: image/png");
$im = #imagecreate(110, 20) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>
When i executed the above code i have got an error like this
"The image can't be displayed because it contains errors "
Same error shows in server also
I'm going to go out on a limb here and guess you have some sort of content before and / or after your PHP tags.
Make sure your file starts with <?php with nothing before it (you may need to check for a byte order mark).
Also, omit the closing ?> tag. You don't need it and it will solve any trailing whitespace issues.