Why is this code not working ?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<?php
header('Content-type: image/png');
$myImage = imagecreate(200, 100);
$myGray = imagecolorallocate($myImage, 204, 204, 204);
$myBlack = imagecolorallocate($myImage, 0, 0, 0);
imageline($myImage, 15, 35, 120, 60, $myBlack);
imagepng($myImage);
imagedestroy($myImage);
?>
</body>
</html>
I always get error The image cannot be displayed because it contains errors.. I've already enabled php_gd2.dll and memory_limit in php.ini is also 128M. If i remove header('Content-type: image/png'); i don't get the error but i don't see the image either. All i see is this :-
‰PNG ��� IHDR���È���d���ùHíH���PLTEÌÌÌ���Ó33d���MIDATH‰c£Àx�§” Nf*k²Ã)Ãø�§”•5}À)ÅS†ÚšpJUà”a§²¦œ2ÔŽw<špJ‚Q0 †;�� uTBúŸ����IEND®B‚ `
You must not output anything before header(). Just start your document with <?php (as the first file characters), followed by the code for displaying the image. Skip the HTML tags. Do not even write a single blankline before header().
If you want to display an image inside the html document of yours, you must do it in two files. One, call it for example image.php, containing only the PHP code including the header. The second file, call it show.php or show.html, includes the HTML code you like, including <img src="image.php" alt="Your generated image" />.
If you base64 encode the output, you could use the image directly with a Data URI scheme.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<?php
$myImage = imagecreate(200, 100);
$myGray = imagecolorallocate($myImage, 204, 204, 204);
$myBlack = imagecolorallocate($myImage, 0, 0, 0);
imageline($myImage, 15, 35, 120, 60, $myBlack);
ob_start();
imagepng($myImage);
printf('<img src="data:image/png;base64,%s"/>',
base64_encode(ob_get_clean()));
imagedestroy($myImage);
?>
</body>
</html>
Note that Data URIs are not supported by all browsers (guess which).
You should output only the image. You are outputting a bunch of tags. Specifically
<!DOCTYPE HTML PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN">
<title></title>
</head>
<body>
and then the image as a binary. If you want to see this, wget the page from your server and try opening it in an editor. Your code should start at the <?php.
Removing the header gets rid of the notification to the client that this is an image so it will try out print it out as text.
While trying to output a jpg of a Facebook profile-picture from their Graph API with PHP,
I noticed that if the PHP file is saved with UTF-8 encoding - this error was returned,
but if the file was saved with ANSI encoding -
then it worked OK.
If you have this problem, try to delete any space character between the begin of the script and the php tag
> <?php
I spent several hours before realize this. And now it works OK. This happens because any character on the file alter the png format. This worked great for me.
Even if you think you have removed all the text from before header() your PHP file may contain a Byte Order Marker
This will be invisible to you in your editor, but your browser will see it and think the image is corrupt. You need to take the steps appropriate to your editor to remove any BOM.
This is all you need. You can not print anything else because it needs to look like it's own file. You could call this image.php and pass it a variable to define which image to output.
<?php
header('Content-type: image/png');
$myImage = imagecreate(200, 100);
$myGray = imagecolorallocate($myImage, 204, 204, 204);
$myBlack = imagecolorallocate($myImage, 0, 0, 0);
imageline($myImage, 15, 35, 120, 60, $myBlack);
imagepng($myImage);
imagedestroy($myImage);
?>
I isolated the PHP script into its own file (image.php) and it worked fine: I got a grey rectangle with a black slanted line. Your issue is trying to embed this within a HTML file.
You need the PHP in its own, separate file (as I did, call it image.php or something more description to your needs) and then create a HTML file as follows:
<html>
<head>
<title>Document Title</title>
</head>
<body>
<img src="image.php" alt="" />
</body>
</html>
You should get your desired output then.
I had the same problem and the solution was to change the charset of the code from UTF-8 to ansi or viceversa. If the server is set to UTF-8 and your code is in ansi this don't work and if your code is in ansi and the server is configured to UTF-8 neither.
If you are using Filezilla to upload your webpages make sure you select transfer type as 'Auto/Binary'.
Transfer -> Transfer type -> Auto/ Binary.
Related
I have an image URL, for example, http://URL/123.jpg. I can view the image in the browser, but it's not working when using the <img> tag in PHP. It's not working, and the idea is somehow not displayed.
echo "<img src=\"http://URL/123.jpg\" style=\"width:11%;height:auto;margin-left: 570px;margin-top: 170px;\">";
Any idea?
For php I use '' and for html the "" quotation marks.
Don't know if it's gonna solve your issue tho. Maybe the height:auto; is the problem. I think you can even delete it, if you set the width the height should be auto by default.
echo '<img src="http://test.com/123.jpg" style="width:11%;margin-left: 570px;margin-top: 170px;" />';
Use Normal HTML syntax, include your php then save file as .php
<!DOCTYPE html>
<html>
<body>
<img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse1.mm.bing.net%2Fth%3Fid%3DOIP.GONAZbNLqDhBj8q4CpTnCQHaLH%26pid%3DApi&f=1" style="width:100px;height:auto;margin-left: 570px;margin-top: 170px;"/>
<!--your php file-->
<?php include 'footer.php';?>
</body>
</html>
Don't forget you have to save this file as PHP.
I have found following way of creating a sample image via php in a separate php file which then is included in the img src attribute:
<img src="image_creator.php">
In the file "image_creator.php" I do following:
header ("Content-type: image/png");
$display_img=imagecreate(100, 50);
imagecolorallocate($display_img, 255, 255, 125);
imagepng($display_img);
imagedestroy($display_img);
This is a simplified version which works. I do something more about it which is not important for my question.
My question: how is it possible to include above php code from the separate file "image_creater.php" directly in the leading php file including the HTML img statement?
I tried to use the php code inside a php function and put that function inside the img src attribute but it did not work. The background of my question is that I additionally use a text code to be printed inside the image and I don't want to disclose this text code via "image_creator.php?code=xxx" which would be visible in the HTML code of the img of course.
If I'm understandingly correctly, you can include the image via base64 encoding:
<img src="<?php echo require('image_creator.php'); ?>">
And inside of image_creator.php you would do:
<?php
$display_img=imagecreate(100, 50);
imagecolorallocate($display_img, 255, 255, 125);
ob_start();
imagepng($display_img);
imagedestroy($display_img);
$img = ob_get_clean();
return 'data:image/png;base64,' . base64_encode($img);
?>
I am facing a problem with generating pdf in php by using TCPDF library. I need to show the bangla font correctly. I tried to add some bangla font(i.e. SolaimanLipi.ttf, SutonnyOMJ.ttf, Siyamrupali.ttf, Nikosh.ttf and so on). I can see the bangla font on pdf but the font is not display correctly. Its misplaced the word.
By adding this font I see the on the /fonts/ directory there successfully created 3 file “solaimanlipi.ctg.z”,”solaimanlipi.php” and “solaimanlipi.z”. As well as I can see the bangla font on pdf, but this font is misplaced/scattered. I am attaching a picture what I actually see.
This is how it should look (From browser screenshot):
Here is the code to show above result:
<?php
$strData = file_get_contents('./data3.txt');
?>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8" />
<style>
#font-face
{
font-family: myUniFont;
src: url(./SolaimanLipi_22-02-2012.ttf);
}
</style>
</head>
<body>
<span style="font-family: myUniFont;"><?php echo $strData; ?></span>
</body>
</html>
I use below code to use that very same font in my pdf:
$strBNFont = TCPDF_FONTS::addTTFfont('./SolaimanLipi_22-02-2012.ttf', 'TrueTypeUnicode', '', 32);
$pdf->SetFont($strBNFont, '', 8, '', 'false');
And It is how it look like in PDF: :(
Please advice me how can I display the bangla font correctly.
EDIT #1
wow! ;( OMG!
dear sir, problem is not on pdf/tcPDF library nor even in the font file it self.
please check the below php code:
<?php
header('Content-type: image/png');
$text = 'তোমাদের জন্য মুক্তিযুদ্ধের গল্প';
$font = './Fonts/SolaimanLipi_22-02-2012.ttf';
$im = imagecreatetruecolor(600, 300);
$bg_color = imagecolorallocate($im, 255, 255, 255);
$font_color = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 599, 299, $bg_color);
imagettftext($im, 20, 0, 10, 50, $font_color, $font, $font);
imagettftext($im, 20, 0, 10, 120, $font_color, $font, $text);
imagepng($im);
imagedestroy($im);
?>
this how it export/render the png image on browser:
when i try to print the text on image file using imagettftext function it also broke the character :(
as i am sure it's not font issue because i have just tested with 60+ fonts and all get broken.. while browser (html code) shows them very correctly.
so, i thing this is very bigger than my brain can contain/handle ;(
so, expert like you may only way out :)
thanks again for your times
TCPDF by itself can't handle Brahmic scripts.
I've posted similar for other languages: How can I create Malayalam PDF using TCPDF in PHP?
I believe mPDF has support for your text based on this example file: http://mpdf1.com/examples/example_utf8.pdf
I would suggest trying mPDF out if you're not dead-set on TCPDF. It's definitely easier if you can get it to work than the method I'm about to outline.
Another alternative, though, in my opinion far more complicated is to use ImageMagick with Pango to render your text as images and then include it in the PDF. This is different from ImageMagick's normal font rendering which as you saw is just as broken for your use. I'm including this more out of academic interest, I wouldn't necessarily suggest doing this unless you find a compelling reason to do so.
I basically had to do this from the shell after installing ImageMagick with Pango support:
#Install font for my user.
cp /host/SolaimanLipi_22-02-2012.ttf .fonts
#update the font-config cache
fc-cache
#Render the text with pango
convert -background white -size 400x pango:#/host/bangali.txt /host/out.gif
Where /host/bangali.txt contained <span font='18.5'>তোমাদের জন্য মুক্তিযুদ্ধের গল্প</span> *
Which then renders output like this, which I think is at least mostly correct:
This is because Pango's shaping engine is capable of doing so. There are a number of caveats though to do it this way. Not the least of which is getting font-config to behave properly from CGI or mod_php, which is doable, but tricky in my experience.
I didn't have to specify a font name since I only have one Bangala font, so font-config found and used the one I had installed.
I am trying to use ImageMagick class functions in PHP.
To get familiar with ImageMagick, I wrote a few lines of code in PHP to display a red square in my browser.
Instead of displaying a red square, I get garbled text.
I know ImageMagick is installed because I can use ImageMagick functions to save the red square to a file.
Thanks in advance for any help for this newbie to ImageMagick and StackOverflow!
Here's my PHP code:
$image = new Imagick();
$image->newImage(100, 100, new ImagickPixel('red'));
$image->setImageFormat('png');
$image->writeImage("MyOutput.png");
header('Content-type: image/png');
echo $image; //This causes just raw text to be displayed. :(
echo '<img src=MyOutput.png>'; //Displays a 100x100 red image!
//..So, ImageMagick IS installed.
Here's my full PHP file.
<!DOCTYPE html>
<head>
<title>ImageMagick Test</title>
</head>
<body>
<?php
$image = new Imagick();
$image->newImage(100, 100, new ImagickPixel('red'));
$image->setImageFormat('png');
header('Content-type: image/png');
echo $image;
?>
</body>
</html>
The main problem you have is that you're sending Content-Type: image/png after putting some output. Once you put any output, PHP immediately sends all response headers and proceeds to the body part of response. The default Content-Type is text/html on most servers, so your browser is interpreting image content as HTML, and thats why you're seeing some garbage. It's a bit like opening PNG file in notepad.
See this question for more information about sending response headers
Also you can't return HTML and image content upon one request, so do not put any HTML in your image-generating file. The image is independent file and it has to be returned upon it's own, separate request, containing only headers, image bytes and nothing more.
This should work:
<?php
$image = new Imagick();
$image->newImage(100, 100, new ImagickPixel('red'));
$image->setImageFormat('png');
header('Content-type: image/png');
echo $image;
Note, that this is whole PHP file and you can't send any other output neither from outside of <?php ?> nor by echoing.
If you want to include your image into HTML you need a second file (it may be plain HTML), which should look as follows:
<!DOCTYPE html>
<head>
<title>ImageMagick Test</title>
</head>
<body>
<!-- here we're including our image-generating PHP script -->
<img src="imagickTest.php">
</body>
</html>
Then you can refer either to PHP file if you want to see only image or to HTML file if you want to see image put into HTML context.
I am trying to convert a web page into jpeg image file. i had used following codes.
<?php //put your html code here
$html_code = "
<html>
<head>
<title>My test title</title>
<style>
body {
font-family:verdana;
font-size:11px;
color:black
}
</style>
</head>
<body>
this is the body
</body>
</html>";
// Create the image
$img = imagecreate("300", "600");
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,300,600,$c);
imageline($img,300,0,0,600,$c);
$white = imagecolorallocate($img, 255, 255, 255);
imagettftext($img, 9, 0, 1, 1, $white, "VERDANA.TTF", $html_code);
// Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
?>
Question: Is there any library to convert html page into image ?
You will need an HTML renderer for this. There exists a few such renderers, but most of them requires an X server on your web server, so check out Xvfb to run a framebuffer device without a screen.
GD does not do this natively, you can render a picture from your screen though but that is as far as it goes. I had painty in my links with a description of html to jpg php conversion.
You need to internally render the page. You can find possible solutions here.
When I needed to 'screen-capture' an HTML table as an image a few months back, I couldn't find anything suitable.
I eventually settled for writing my own code using PHP's GD library with GIF output.
It was for a very specialised purpose and I'm sure it wouldn't suit an application outputting jpegs.