I have a captcha Code which works perfectly but I really dont know why it displays funny.
Can anyone help me figure out what might be the cause of this? Here is the code:
<?php
session_start();
$text = rand(10000,99999);
$_SESSION["vercode"] = $text;
$height = 25;
$width = 65;
$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 14;
imagestring($image_p, $font_size, 5, 5, $text, $white);
imagejpeg($image_p, null, 80);
?>
This is what is displays:
kX’OýÓx£Ç> ñ=¨µÖ/b{ct÷¯½¬6É-ÀiH¡ä csdòyäæcÆ^"Ö|7¥è¦ss¤i™û%³‘¶>02q–Ú2q;A!p+Ÿ¢€:¿øX>'þÂþÈþÓÿDûöo™öx¾Ñö]Û¼?o›ågø7mÇËŒqF‡ñÄú•Ÿ¦j~M½·öWkx¤šÓÎ]²ù²™!Ü:ìeä“Ô“\¥QEQEÿÙ
You're missing the header. Add the following after session_start():
header('Content-Type: image/jpeg');
The output will be something like:
When you're creating an image like this, you need to tell the browser what type of content you're wanting to display. Hence the reason:
header("Content-type: image/jpeg");
is needed. Read the Wiki article on MIME if this confuses you. You'll also find that you'll need to specify the content type whenever you're generating other file types as well (for example: PDF files generated on the fly with PHP will require header('Content-type: application/pdf'); )
Related
Good day. Why is that when I try to run my code on an external page, it works perfectly. But when I used wordpress to add it on my page, it gives me strange errors. Why is that and how do I fix that?
code:
<?php
// (A) OPEN IMAGE
$img = imagecreatefromjpeg('https://images.unsplash.com/photo-1550684376-efcbd6e3f031?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80');
// (B) WRITE TEXT
$white = imagecolorallocate($img, 255, 255, 255);
$txt = "sad";
$font = realpath('arial.ttf');
//(IMAGE, FONT SIZE, TILT ANGLE, X, Y, COLOR, FONT, TEXT)
imagettftext($img, 12, 0, 253, 234, $white, $font, $txt);
// (C) OUTPUT IMAGE
header('Content-Type: image/jpeg');
imagejpeg($img);
// OR SAVE TO A FILE
// THE LAST PARAMETER IS THE QUALITY FROM 0 to 100
imagejpeg($img, "test.jpg", 100);
?>
Here's the result I got when trying it on my wordpress page:
Image here
The issue is that you are running this in a shortcode and change the header of the output, that you already created, namely the page. It depends on what you want to do, if you want to display the image in the site:
ob_start();
imagejpeg( $img, NULL, 100 );
imagedestroy( $img );
$i = ob_get_clean();
echo "<img src='data:image/jpeg;base64," . base64_encode( $i )."'>";
Want to display the half letters like "અર્થ" and "અસત્ય" in imagefttext function PHP but the result something different. Like this.
Or anyone can help me the alternate way?
<?php
header('Content-type: text/html; charset=utf-8');
#Declare fixed values
$textY = 190;
$textX = 130;
$textY2 = 280;
$textX2 = 130;
$textFont = './fonts/notoSans/NotoSansGujarati-Bold.ttf';
// $textFont = './fonts/Shrikhand-Regular.ttf';
$textSize = 54;
$imagesFolder = './images';
$text = "અર્થ";
$text2 = "અસત્ય";
//header("Content-Type: image/png");
header("Content-Type: image/png");
//Creating background image
$im = imagecreatefrompng('https://i.stack.imgur.com/A9Oll.png');
//$im = imagecreatetruecolor(512,512);
// Create the clours to be used.
$yellow = imagecolorallocate( $im, 255, 255, 0 );
imagefttext( $im, $textSize, 0, $textX, $textY, $yellow, $textFont, $text );
imagefttext( $im, $textSize, 0, $textX2, $textY2, $yellow, $textFont, $text2 );
$imageFile = $imagesFolder.'/'.rand(0,500).'.png';
imagepng( $im, $imageFile );
// Unload resources.
imagedestroy( $im );
?>
=> Edits 0.1:
Issue is getting displaying the half letters in the image.
- Output I needed (I did that in photoshop) : desired Output image
- But I'm getting through this code : image getting from code
Looks like this is not possible with Unicode, and has been a problem for a long time. From a PHP developer on a 10 year old bug:
Thanks, now I got it. The problem isn't particularly related to
Bengali, but rather to combining characters in general, which are
not supported by imagettftext().
I attempted this with the Imagick extension, but had the same output.
A possible workaround is to find a font that uses another encoding such as ISCII, and then convert the text to that encoding using mb_convert_encoding() or similar function.
Or perhaps have the image generated by a command-line tool run with exec().
I want to create an image with text and store in temp without output to browser
I have used the following code:
header("Content-type: image/jpeg");
$imgPath = 'images/certificate.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
$string = $userid;
$fontSize = 3;
$x = 115;
$y = 185;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image,'images/certi.jpg');
imagedestroy($image);
When using imagejpeg to save a file, you do not need the header header("Content-type: image/jpeg"); line
If you are not returning image data, then you should not be setting the header.
Remove the header("Content-type: image/jpeg");
I'm not clear on exactly what you're trying to do, but if the work has been done, there is no need to return any output, if output is not desired.
I wrote this function to create png file from text :
function pngfromtext($text){
$fontsize = 5;
$width = imagefontwidth($fontsize)* strlen($text);
$height = imagefontheight($fontsize);
$img = imagecreate($width, $height);
// Transparent background
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
// Red text
$red = imagecolorallocate($img, 255, 255, 255);
imagestring($img, $fontsize, 0, 0, $text, $red);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
}
I put codes to functions.php file, When use this function in another page I get this error:
Warning: Cannot modify header information - headers already sent by (output started at ..\functions.php on line 58
�PNG IHDRZ^%JPLTE����ٟ�tRNS#��f�IDAT�c` Hȱ�7�H��'��`c��s�����i��$���Hl`8��Ɛ�� ��#�c��p�� q�3f�òm�� �g�ـ�6fF ���h�bc�sXd4c�A4����?|�¦����r+���!IEND�B`�
What is wrong?
Set the header at the point where you know that your output is going to be an image. This means set this statement
header('Content-type: image/png');
at the beginning of your php script.
There is also a possibility that the header-command was already executed before this point here.
You can't use multiple header in on page.
before header('Content-type: image/png'); put ob_clean();
that cleans the response object so you can add the headers again
Can anyone tell me why I can't use the function imagettftext when I'm trying to create a captcha? It works fine with imagestrings, but the font-size is to small so I have to use imagettftext... Is this a problem with Wamp or what is it? Thanks for your time!
PHP code(so far):
<?php
session_start();
header("Content-Type: image/png");
$text = $_SESSION['secure'];
$font_size = 30;
$image_width = 200;
$image_height = 40;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'arial.ttf', $text);
imagepng($image);
?>
Index.php:
<?php
session_start();
$_SESSION['secure'] = rand(1000,9999);
?>
<img src="createanimage.php" />
From what little info you've given, the code initially didn't work for me, I changed the font path from arial.ttf to ./arial.ttf. It worked!
An exact replica of your code: http://tekinozbek.net/so_imagettftext.php
Beware some spammers use image analysis to recognize text paths and determine the CAPTCHA image! I would certainly suggest you use something like reCAPTCHA for better protection against spam.