phpqrcode library return image as a string - php

http://sourceforge.net/projects/phpqrcode/, is a great library but i can't find how to return the png image as a string, the basic examples are
QRcode::png('code data text', 'filename.png'); // creates file
QRcode::png('some othertext 1234'); // creates code image and outputs it directly into browser
i checked the documentation and nothing, help! :B

ob_start();
QRCode::png('text', null);
$imageString = base64_encode( ob_get_contents() );
ob_end_clean();

$qrTempDir = 'path/to/your/temp';
$filePath = $qrTempDir.'/'.uniqid();
QRcode::png('some text', $filePath);
$qrImage = file_get_contents($filePath);
unlink($filePath);
This should be what you're looking for. You can extend it to show an image like that:
<img src="data:image/png;base64,<?php echo base64_encode($qrImage) ?>" />
Unfortunately, the library does not support any other method at the moment, because calling the QRcode::png function without the file parameter not only makes it send those headers, but it also exits the code execution, so there is no retracting or overwriting the headers.

I ran into the same problem as #iim.hlk
This is what i did slightly modified #Lusitanian his answer to this
ob_start();
QRCode::png($string);
$imageString = base64_encode( ob_get_clean() );
header('Content-Type: text/html');
This fixes the header issue by simply overwriting it. Not clean or anything but it works for the purpose.

this works for me
include '../phpqrcode/qrlib.php';
$content = "any content";
ob_start();
QRcode::png($content);
$result_qr_content_in_png = ob_get_contents();
ob_end_clean();
// PHPQRCode change the content-type into image/png... we change it again into html
header("Content-type: text/html");
$result_qr_content_in_base64 = base64_encode($result_qr_content_in_png);
then in your html file
<img src="data:image/jpeg;base64,$result_qr_content_in_base64'"/>

Related

get generated php output data in html generated file

I have a php file that has data coming from the database called Casino-review.php
I just want to copy the php generated output data from this file to a new html file. I have tried lots of other thinks like OB functions, File contents functions, Copy functions, Fopen functions but, nothing work for me. Everthing is working properly creating file copying content but it's showing php code in it. Please help me.
here is the Output.
here is my code
$fileName = $title.".html";
$to = "casinos/all/".$fileName;
$from = "casinos/casino-review.php";
$newcontent = file_get_contents("$from");
if ($createFile = fopen($to, "w+")) {
fread($createFile, filesize($from));
fwrite($createFile, $newcontent);
fclose($createFile);
// echo "Wait for 5 seconds";
// setcookie("file", json_encode($casinoDetails), time() + 7400);
// header("REFRESH: 5;profile/$username/profile.php");
// echo "good";
}
I have finally found my answer
ob_start();
include("$from");
file_put_contents($to, ob_get_contents());
ob_end_clean();
Thanks #Innovin

Use PHP to "parse" image with response header "content-type: image/jpeg"

I'm requesting an image from graph.facebook.com
The response header uses "content-type: image/jpeg" and prompts me to download instead of displaying it in the browser.
Is there a way (using PHP) to "intercept" the download and convert it to an image format?
Cheers.
UPDATE:
This kind of works. It returns the image to the browser (visible in Chrome's Network tab), but I'm not sure how to display it in an <img> tag:
<?php
$data = file_get_contents("https://www.petmd.com/sites/default/files/what-does-it-mean-when-cat-wags-tail.jpg");
// $data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
$qwe = imagepng($im);
imagedestroy($im);
return $qwe;
}
else {
echo 'An error occurred.';
}
?>
You code is invalid. You do not use return from non function as it makes no sense. If your case you need to replace
return $qwe;
with just plain
echo $qwe;
I'd also follow it by instant die(); and remove closing ?>. That should be all you need to make it work.
Not sure what is the point of fetching JPEG file but outputting PNG thought. You are gaining nothing but wasting time and resources, so the whole conversion code is pretty useless and echoing what you just downloaded should be perfecly sufficient:
header('Content-Type: image/jpeg');
$data = file_get_contents(".....");
if ($data !== false) {
echo $data;
}
die();
PS: As a homework, get rid of using file_get_contets() for network access and use cURL, esp. the network wrapper the former uses can be disabled.

Get generated html content of php script on same server

I have a php page that outputs html to the browser based on a query string that is parsed. The issue I am having is that I need to retrieve this html source code dynamically via php.
The following code will not work because it tries to resolve an absolute path as it's on the same server:
$url = 'http://example.com/myScript.php';
$html = file_get_contents($url);
If I manually set the absolute path it just returns the php contents as text (not executed like a browser would do):
$url = '/dir1/dir2/dir3/dir4/myScript.php';
$html = file_get_contents($url);
I then researched it and found that using ob_get_contents could work. The code below works as expected, executing the script and returning the html output.
$url = '/dir1/dir2/dir3/dir4/myScript.php';
ob_start();
include($url);
$html = ob_get_contents();
ob_end_clean();
The problem with the above solution is that as soon as I put the query string on the end it fails. I think this is because it's treating the query string as part of the file name.
Use PHPs ob_get_contents
<?php
ob_start();
$original_get = $_GET;
$_GET = ["query" => "tags", "you" => "need", "in" => "file.php"];
$file = "file.php";
include($file);
$_GET = $original_get;
$content = ob_get_contents();
ob_clean();
echo $content;

CP1251 Encoding issue in html2pdf

I was download and setup script from:http://www.html2pdf.fr/en
It works fine with default encoding, but when I try to generate docs with characters in CP-1251, I got white spaces instead of characters.
Also All my files in CP-1251,data in base in CP-1251 and as you can see I use simple font -Arial
Please, maybe exist some solution to get it to work.
P/s sorry for my english
ob_start();
include(dirname(__FILE__).'/res/exemple00.php');
$content = ob_get_clean();
require_once(dirname(__FILE__).'/../html2pdf.class.php');
try
{
$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->setDefaultFont('Arial');
$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
$content1=$html2pdf->Output('', 'S');
// Some php code
$db->query("set names cp1251");
$query="SELECT data from files Where id=$file_id ";
$result=$db->query($query);
$row=$result->fetch_assoc();
$content=($row['data']);
header('Content-Type: application/pdf');
header("Content-Length: ".strlen(content));
header('Content-Disposition: attachment; filename=Invoice#'.$invoice_id.'.pdf');
print $content;
}
catch(HTML2PDF_exception $e) { echo $e; } enter code here
I am not using this library, but are you setting your encoding in the HTML2PDF constructor?
$html2pdf = new HTML2PDF('P','A4','fr', true, 'CP-1251');
If it does not work, try 'cp1251' or 'CP1251', I do not find list of recognized encodings in the documentation. You can also use iconv and convert to UTF-8 as it seems to be a default.

PHP - print content from file after manipulation

I'm struggling trying to read a php file inside a php and do some manipulation..after that have the content as a string, but when I try to output that with echo or print all the php tags are literally included on the file.
so here is my code:
function compilePage($page,$path){
$contents = array();
$menu = getMenuFor($page);
$file = file_get_contents($path);
array_push($contents,$menu);
array_push($contents,$file);
return implode("\n",$contents);
}
and this will return a string like
<div id="content>
<h2>Here is my title</h2>
<p><? echo "my body text"; ?></p>
</div>
but this will print exactly the content above not compiling the php on it.
So, how can I render this "compilePage" making sure it returns a compiled php result and not just a plain text?
Thanks in advance
function compilePage($page, $path) {
$contents = getMenuFor($page);
ob_start();
include $path;
$contents .= "\n".ob_get_clean();
return $contents;
}
To evaluate PHP code in a string you use the eval function, but this comes highly unadvised. If you have a file containing PHP code, you can evaluate it with include, include_once, require, or require_once depending on your need. To capture the output of an included file - or required, or whichever method - you need to enable output buffering.
You can use output buffering for this, and include the file normally:
function compilePage($page,$path){
$contents = array();
$menu = getMenuFor($page);
ob_start();
include $path;
$file = ob_get_contents();
ob_end_clean();
array_push($contents,$menu);
array_push($contents,$file);
return implode("\n",$contents);
}
The include() call will include the PHP file normally, and <?php blocks will be parsed and executed. Any output will be captured by the buffer created with ob_start(), and you can get it later with the other ob_* functions.
You need to use include() so it will execute. You can couple this with output buffering to get the return in a string.
function compilePage($page,$path){
$contents = array();
$menu = getMenuFor($page);
//output buffer
ob_start();
include($path);
$file = ob_get_contents();
ob_end_clean();
array_push($contents,$menu);
array_push($contents,$file);
return implode("\n",$contents);
}

Categories