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"); ?>
Related
Summary
I am trying to use the IMAP functions to fetch the email attachments. Also, I am trying to fetch the inline images and show them out to the screen by using <img> tag. In order not to occupy so much server space, I decided not to save those fetched inline images as files on the server and show them on the screen. My decision is that after fetching the content of one of the inline image, print it out by using echo function, then use <img> tag to show the printed content out one by one. The code may look like the following part:
Codes
show-inline-image.php
<?php
...(omit)...
echo $image_content; //The variable $image_content is the content of fetched inline image
?>
show-image.php
<?php
...(omit)...
echo "<img src='show-inline-image.php'>"; //Show the image
...(omit)...
?>
Though this way could print the inline images out successfully. But I am not sure whether it is alright to show the inline images in this way. Is there any other better ways to impove it?
P.S. The output result of show-inline-image.php may look like the image below:
render.php
$form = '<b>Hi</b><br /><img src="test.jpg" /><br />New File';
echo $form;
header("Content-Type: image/png");
imagejpeg($form);
But this code not work
I want this page render to img file :
<img src="render.php" />
you are trying to output both HTML and image-data - that doesnt make any sense.
Instead you should put an HTML-img-tag into your website, the src-attribute being render.php which sets the header via header() and directly outputs image data - without any additional HTML.
https://stackoverflow.com/a/1851856/351861
You should use extra php file for images, something like:
<img src="render.php?id=15" />
Because if your php script returns only one picture it is used wrong. so you pass it what image to show.
And render.php might be something like
$pic_id = (int)$_GET['id'];// cast to int because only numbers are allowed here
$pic_path = 'assets/img/';
$f = $pic_path.$pic_id.".jpg";// it's just an example so it's simplyfied
header("Content-Type: image/png");
imagejpeg($form);
The point here is that you can check user's permissions to see the picture very flexible and log some data about downloader.
I have this problem, i have this script in php that creates a image on the fly, the problem is that the outputted image on the browser is allright, but i need to change it's name.
Ex: Index.php
<?php $url = "http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33" ?>
<img src="<?php echo $url ?>" />
The image_scrc.php is the file that creates the image, and as you can see i have several data that is passed by the get method.
In the image_scrc.php i have tryed
header('Content-type: image/jpg');
header('Content-Disposition:inline; filename="'.$random_name_jpeg.'"');
but the html link is is always appearing like this
http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33
if i select the image on browser and then select copy image link it copies just like this also.
however, when I save the image it assumes the random_name.jpg, but only on save!
i've tried everything, even htaccess rules but nothing seems to work !!
it's this possible to acomplish? transform this
http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33
to this
http://www.somesite.com/cls/random_name.jpg
i cant have the image on the server side! and must be displayed on the fly
Thanks in advance.
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
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");