Using dynamic php generated images with MPDF - php

I am using php and mpdf to generate a particular pdf. I have some pie diagrams in the pdf. For that I am using pChart. I am generating pie diagrams dynamically for rendering in pdf.
In my pdf all the static images are rendered properly, but no dynamically generated pie diagrams.
This one(static) works,
<div class="chart">
<?php echo CHtml::image(Yii::app()->baseUrl.'/images/color-block2.png', 'Logo screenshot', array('class' => 'logo_image')); ?>
</div>
But not this(dynamic)
<div class="chart">
<?php echo CHtml::image(Yii::app()->baseUrl.'/images/pie.png', 'Logo screenshot', array('class' => 'logo_image')); ?>
</div>
Ps: I am using Yii url rules for a clean url for pie diagram.
When i rendered the pdf_template as html, both images are rendered properly.
How can i implement dynamic images using mpdf without the $mpdf->Image() method.
I suspect i cannot use the $mpdf->Image() while generating a pdf from html.
Update:
After
$mPDF->showImageErrors = true;
I'm getting
mPDF error: IMAGE Error (http://csrt.dev/images/pie.png): Could not find image file

Base64 encoded the image and embedded it in a <img> tag.
<div class="chart">
<img src="data:image/png;base64,xx5t.." />
</div>
I had the same problem using Yii, and found this to be the solution, however for other Yii/PHP users out there, you have to use base64_encode(your file contents) to get the part after base64, in the tag.
for example:
base64_encode(file_get_contents("../images/folder16.gif"));

I've had a similar issue.
Using:
<img src="http://domain.com/assets/img/header.png">
or
<img src="/assets/img/header.png">
give me the following error:
mPDF error: IMAGE Error (http://domain.com/assets/img/header.png): Could not find image file
Even though i can browse to that and see the image ok.
However using:
<img src="assets/img/header.png">
Seems to work fine.

Related

My image is not showing on php but it is when i preview it on a html file

So when i view my codeon a html file without the php code inside it and just the html the images show up but then when i upload my php file to the server and go to my website everything shows up except the images.
<div class="slideshow-container">
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img src="images/forexample.jpg" class="slideshow-images">
<div class="text">Caption Text</div>
</div>
</div>
what could be the problem here, because clearly my images show up when i runt he html but thne when i upload it to the server as a php with all my php codes inside as well it doesn't work, and yes I seperate the php correctly:
<?php
php code in here
?>
html things right below it
and that would be my file with the exact html that i run on my computer whcih all the image show up on, in the html things area that i showed above, what could be wrong, and yes I have my folders set exactly the same on the server as on my computer files for my site.
I also have #media screens on my css but that shouldn't be the problem, i think.... also like i said they are both the same html since i test it out on my computer first and then copy the html into the php file html area and have it linked to my css aswell obvs.

Linking images regardless of file type

I'm building a little site at the moment and users can upload their own profile picture but obviously some users pictures will be png and some jpeg, does anyone know anyway to be able to accept both when using the images as a background image in css. Here is my HTML:
<div class='avatar' style='background-image: url('users/avatars/$username.jpg')'></div>
Inside the users/avatars/ folder is every users profile picture named like so: "username.jpg" or "username.png".
Here is an example of my issue: http://cl.ly/image/3W311S2v0v0t
The first post loads the users profile image because it is a jpeg but the second post hasn't loaded the profile image as it is a png.
Thanks in advance.
You can do it in CSS like this:
background-image: url('users/avatars/$username.jpg'), url('users/avatars/$username.png');
If you absolutely must keep all the logic on the client side, in Javascript, you can catch the error if the image fails to load and attempt to load the other version.
For example:
<div class='avatar'>
<img src="users/avatars/$username.jpg" onerror='this.src="users/avatars/$username.png";' />
</div>
So if the JPG is not found and returns an error, the image will then load the PNG version.
If you are using PHP you can check the file extension and serve up different HTML. Here's how to read the file extension:
How to get the file extension in PHP?

dompdf one image not displaying

I am running a while loop and generating a var called $features
while(blah) {
$features .= "<li><img src='/srv/sitename.com/htdocs/images/sid.gif'> $features_bullet_text</li>";
} // THIS LOOP WORKS FINE, PULLING CONTENT, IMAGE DOES NOT SHOW
Then I am putting this var inside the code below. The content being pulled works fine but the image does not display. The image marked below "IMAGE SHOWS UP" is there when the PDF is generated along with several others but for some reason the one I tacked on to the front of my <li> will not show up. Anyone have any suggestions?
It is not permissions, the file path is correct on the server but I need to generate this mysql/loop of <li>s prior to putting into this PDF. Hope this makes sense to someone.
$html = <<<EOF
PROPER HTML ABOVE
<!-- IMAGE SHOWS UP -->
<img src="/srv/sitename.com/htdocs/images/ordering_information.jpg">
<!-- IMAGE DOES NOT SHOW UP IN LI -->
<ul>
$features
</ul>
PROPER HTML BELOW
EOF;
All features working properly. Only the image inside the <li> will not show. Also I have tried using double quotes and escaping them and that did not work. Tried no quotes around image and currently have single quotes in place.
According to this page, the GD library may need to be installed to display GIF images.
If you can't install that, try replacing your GIF with a JPG or PNG and see if that solves things.

How to embed a graph (jpgraph) in a web-page

I am using this script which is one of the examples provided by jpgraph itself. When I put this on a web-page (blank) by itself, it's drawing the graph. But when I embed the code in already existing web-page (with some content), it ain't drawing a graph.
GD is already enabled according to phpinfo(). Iam using jpgraph 3.5.0b1.
The problem is that you are mixing HTML/text output with image output.
Any time you have a PHP script generate graphical content you have to handle the output differently than normal HTML or text.
There are a few routes, I'll cover them briefly here.
Save the output to a file and use that filename in your HTML
//replace this line:
// Display the graph
//$graph->Stroke();
// with these lines:
// Default is PNG so use ".png" as suffix
$fileName = "/tmp/imagefile.png";
$graph->img->Stream($fileName);
.. then use $filename in an image tag, like this (for example):
print '<img src="'.$filename.'" />';
Create a standalone PHP script that will output the graphic
You can use the example script as-is, alone in a file called graph_render_script.php. Then, in your HTML, you use that script as a source:
<img src="graph_render_script.php" />
Output base-64 encoded data
Another route is to use base-64 encoded image data. This is relatively simple to do:
print '<img src="data:image/png;base64,'.base64_encode($graph->Stroke()).'" />';
As always, the documentation should be your guide!
Documentation
http://jpgraph.net/download/manuals/chunkhtml/ch05s05.html
base64_encode - http://php.net/manual/en/function.base64-encode.php
This worked for me:
putting the php code that generates the image in a file...Then on my html page I do this:
<img src="graph.php" >
embedding the graph inline is indeed possible. You'll have to use output buffering to capture the image data, then base64 encode that data, then use that base64-encoded string as the source in an <img>.
Try something like this:
$img = $graph->Stroke(_IMG_HANDLER);
ob_start();
imagepng($img);
$imageData = ob_get_contents();
ob_end_clean();
?><html>
<head>
<title>JpGraph Inline Image Example</title>
</head>
<body>
<h1>JpGraph Inline Image Example</h1>
<img src="data:image/png;base64,<?php echo(base64_encode($imageData)); ?>" />
</body>
</html>
ceejayoz made an excellent point in that this method is almost never what you want. I do not recommend embedding the image data like this unless you have a good reason to do so and understand the downsides, i.e. older browsers lack support for it, and the page size is dramatically increased (not only from the image data but the fact the image data is base64 encoded, which balloons the length). I've used this method in the field myself on a project last year, but it was only because the client refused to make a second request for the image.
But when I embed the code in already existing web-page (with some content), it ain't drawing a graph.
You can't do that - you can't output an image as raw binary data within a page.
You need to put the code that generates the graph in a separate file, and use an image tag.
<img src="path/to/jpgraph/chart.php" />
The graph needs to be on its own page, you can't embed it. It outputs a raw JPG and you need to have no other content sent and have the proper headers to tell the browser it's a JPG. To embed the graph you'd make a different page called stats.php for example, and on that page you'd make an image tag pointing to the stand alone graph.
<img src=graph.php>
I've had this problem many times, I've noticed it happens when you have require() or include() in your Chart's script and those scripts have Data Base connections or special configurations.
I've solved this problem separating the data retrieving and the Chart drawing, passing parameters to the script or using SESSIONS to get the values.
Example of Embed image Chart in your PHP or HTML file:
<img src="linear_graph_customer.php?values=1,2,3,4|1,2,3,4|1,2,3,4&title=CHART&width=500&height=300" width="500" height="300" class="img" />
Regards.

how can i embed php chart graphs into html

i'm using PHP Graph Lib PHPGraphLib
i created some graphs , and all i need is to embed these graphs into html,,
you know, inside and so on,,
every time i put the code within html it gives me errors
The image “http://localhost/chart2.php” cannot be displayed, because it contains errors.
although without the html code , it works fine ...
Assuming your image code is in chart2.php, you embed it into html with an img tag:
<img src="chart2.php" alt="This is a chart of x" />

Categories