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());
Related
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...
So I'm trying to make a Captcha form for my registration form and I'm not sure what's going wrong because whenever I open up the page I see the image shown when the url of an image is not found (I would post a picture, but I can't since this is my first question).
I'm not sure what I'm doing wrong because I was following a tutorial as I was making it and it seemed to work for them. Also, no errors are being displayed so I have no idea what could be wrong. My code is here:
<?php
session_start();
header('Content-type: image/jpeg');
$text = 4539;
$font_size = 4;
$image_height = 40;
$image_width = 200;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$font_colour = imagecolorallocate($image, 0, 0, 0);
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));
// Name the font to be used (note the lack of the .ttf extension)
$font = 'font';
imagettftext($image, 0, 15, 30, $font_colour, $font, $text);
?>
Also the $text variable will be set to random when I know the code works, the value is static for testing purposes. I would appreciate any help I can get. Thanks in advance
You need to output the image to the browser after you add the text to it:
imagettftext($image, 0, 15, 30, $font_colour, $font, $text);
imagepng($image);
imagedestroy($image);
The destroy may not be strictly needed as the php process exits after the script is done, but the examples have it so I included it.
Also, you can choose between png, gif and jpeg image formats by using either imagepng(), imagegif() or imagejpeg(). The png format should be good here.
<?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.
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
I am making use of GD2 and the image functions to take in a string and then convert that into an image using different fonts at different sizes. The function I use is below.
Currently, its pretty quick but not quick enough. The function gets called about 20 times per user and the images generated are always new ones (different) so caching isn't going to help!
I was hoping to get some ideas on how to make this function faster. Maybe supply more RAM to the script running? Anything else that is specific to this PHP function?
Anything else that I can do to tweak performance of this function?
function generate_image($save_path, $text, $font_path, $font_size){
$font = $font_path;
/*
* I have simplifed the line below, its actually a function that works out the size of the box
* that is need for each image as the image size is different based on font type, font size etc
*/
$measure = array('width' => 300, 'height'=> 120);
if($measure['width'] > 900){ $measure['width'] = 900; }
$im = imagecreatetruecolor($measure['width'], $measure['height']);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, $measure['width'], $measure['height'], $white);
imagettftext($im, $font_size, 0, $measure['left'], $measure['top'], $black, $font, ' '.$text);
if(imagepng($im, $save_path)){
$status = true;
}else{
$status = false;
}
imagedestroy($im);
return $status;
}
Thanks all for any help
I think its good
Instead of creating a new image each time, you could have a blank PNG file (we already know that the maximum width is 900px, do you have a fixed maximum height you can use?), open it, add your text, and then crop it (see imagecopy()).
I'm not sure, but it might be faster than what you are currently doing.