how do I call create image function? - php

I found this topic: convert text to image in php
and I tested the code that Sourabh Shankar provide.
When I paste this code in a new empty php file and I adjust the font path, then it works perfect!
But when I put a echo before or after the header('Content-type: image/png'); line, then it all fails
Can someone provide me a code of how to call this image creator in a HTML file:
example:
<span>Here comes the image</span>
<?php [call function??] ?>
<span>and here we continue with some HTML</span>
Do I need to create a php function and if so:
what do I excactly return?
if I return this:
imagepng($im);
then he wont execute the
imagedestroy($im);
I'm not sure how to do this

The PHP code you linked to works as a stand-alone file, so that when it is requested in a browser it will send back an image. Setting the header with
header('Content-type: image/png');
means that the code will return content which the browser interprets as an image file, rather than a HTML file.
You couldn't use PHP to directly output a file into HTML. You would need to use that code in a separate file and link it to the src tag of an image in your code.

You need get image from other script via http.
<span>Here comes the image</span>
<img src="/path/to/image_handler.php"/>
<span>and here we continue with some HTML</span>
where image_handler.php - your php script:
<?php
…
header('Content-type: image/png');
imagepng($im);
NOTE: never output something before call header

Related

Can't get php to output an image

I want my PHP to output a simple image but when I do it gives me a white square, so I thought the IMG couldn't be found but when I put a simple HTML IMG tag it can be found the code is very simple. also if I try an image from the internet it also won't work.
<!--testing if image can be found-->
<!--<img src="img.jpeg">-->
<?php
header("Content-type: image/jpeg");
readfile("img.jpeg");
exit(0);
?>
Basically what's happening here is that you output some HTML comments before you output the image which means your image file is invalid and so your internet browser doesn't know what to display and defaults to what you're seeing (a white box in the middle of your screen).
Additionally when you use header you should ensure that you haven't already returned data!
If you check your source in your browser you'll see something like...
<!--testing if image can be found-->
<!--<img src="img.jpeg">-->
ÿØÿàJFIFÿá‹rExifII*Àж..................................
...with the additioonal data continuing for some time!
If you remove the comments from your PHP file then it will work as intended; your (entire) file should look something like:
<?php
header("Content-type: image/jpeg");
readfile("img.jpeg");
Note: You may want to add additional headers RE content length etc. but these aren't strictly necessary for most browsers.
To display an image using content type header, you must put only php on the page. If you add any other content it'll display a blank square
ex.
<?php header("Content-type: image/png"); readfile("./resources/img/favicon.png"); ?>

How to view the uploaded(.doc) file in a div?

I uploaded the .doc file using php code and saved it in a folder ,the path is stored in database.
When i tried to make a view of the doc file in a div ...a dialog box appears asking whether to save or openwith ..
I'm using wampserver ....I just tried like this
<iframe name="awindow" frameborder=2 width=580 height=440 src="www/siva_example/pdf/1_siva.doc"></iframe>
Any help regarding..
Is it possible to view .doc file in browser or i have to convert it to pdf format...
It's not possible to display a .doc[x] in HTML. You can try to convert the .doc[x] to HTML or to an image.
you can view doc file by adding this to your header at request time
<?php
header('Content-disposition: inline');
header('Content-type: application/msword');
// not sure if this is the correct MIME type
readfile('MyWordDocument.doc');
exit;
You could try using google docs for this, use the below code, it works perfectly for ppts, have not been able to test it for a doc yet
<iframe src="http://docs.google.com/gview?url=http://yourdomain.com/document.doc&embedded=true" style="width:550px; height:450px;" frameborder="0"></iframe>
Ofcourse you would have to update the url as per your program using PHP

php passing pdf content to an iframe using pdftk

I am generating a pdf using pdftk into an iframe with:
<iframe src="data:application/pdf, <?php passthru('pdftk example.pdf output -'); ?>"> </iframe>
However the browser (chrome) reports:
"The file was damaged and could not be repaired"
What am I doing wrong?
If I have a separate file, and reference that within the iframe it works fine. the separate filecontent is:
<?php
header("Content-type: application/pdf");
passthru('pdftk example.pdf output -');
?>
According to http://us1.php.net/manual/en/function.passthru.php, the passthru function is intended to return binary data. I'm not sure if there's enough room in the src attribute to fit all that binary data.
Take that PHP snippet, save as a something.php file, and use that as the src="something.php" - that should work.

Generating QR Code using PHP QrCode

After using the PHP QrCode lib I discovered that for some reason when using a dynamic page with scripts and dialog boxes (JQuery) that when trying to output a QR code in a .png format I get weird symbols instead of the actual generated .png file.
Heres what I have tried:
Created a seperate file with just:
<?php
//include only that one, rest required files will be included from it
include "phpqrcode/qrlib.php";
QRcode::png('barrda554');
?>
Works great,
Attempt 2:
File opened within a dialog box type using JQuery UI:
<?php
header stuff...
include "phpqrcode/qrlib.php";
...
?>
<html>
...
<?php
QRcode::png('barrda554');
?>
..
</html>
In this attempt I get multiple funky symbols for some reason:
�PNG IHDRWWKK/PLTE���U��~�IDAT8��ѱ � P# �c��n :V�L�#�k
y��)�|F��5`ڸzHF|l���
%Z"e�Ы�D{\�ގ����p`�f�eh�������k�[BJeJ�c����,�^�gu�m|Q��o��W����g�
�#�s�<�y��k�m��!v�.��(+�u���$s�-�n$߫>�gR�`IEND�B`�
This has stumped me and I am unsure on how I should approach this to fix it.
Let me know your ideas,
David
UPDATE:
After putting header('Content-Type: image/png'); within the file that JQuery opens, no cigar.
Here is the actual file:
http://jsfiddle.net/T4nEP/
The problem is here:
<html>
<?php
QRcode::png('barrda554');
?>
</html>
To understand what this is doing, imagine that you open a regular PNG file in a text editor, and just copy/paste the contents directly into your HTML file. It's not going to show an image - it'll just be garbage, like you're seeing.
To include an image in an HTML file, you need to use the <img> tag, and point to the URL of the image. In this case, the URL of the image would be a PHP script that outputs nothing except the PNG contents - like this:
<img src="qrcode.php">
And then in qrcode.php, generate the image:
<?php
include "phpqrcode/qrlib.php";
QRcode::png('barrda554');
?>
If you need some information from the HTML page in order to generate the image, you can include it as query parameters in the URL, like this:
<img src="qrcode.php?product=1&format=2">
And then get those values in your PHP like this:
<?php
include "phpqrcode/qrlib.php";
$product = $_GET['product'];
$format = $_GET['format']
// ...
// whatever you need to do to generate the proper code
// ...
QRcode::png('barrda554');
?>
And finally - there are ways to include the image data directly into the HTML page, but it's not supported by all browsers, and is not recommended because it makes the page size much larger and prevents the browser from being able to cache the image separately.
You can see more about base64 data URLs here and here.
Make sure you have the following php code to generate the correct Content-Type header so the browser knows how to render the data its receiving.
header("Content-Type: image/png");

Display images stored in a database without using header

I like the photo that I keep stored in my view using the following code but the problem is I can not play because I need the header('Content-Type: image/jpeg'); implementation would then display., But I have orders sure before header('Content-Type: image/jpeg'); What should I do to display images with no header.
If you're only outputting the image, then it doesn't matter if you have other PHP instructions before it since the only output should be the image.
If you are trying to output an image directly into an HTML document, consider using a data URI:
<img src="data:image/jpeg;base64,<?php echo base64_encode($imagedata); ?>" />

Categories