Laminas Barcode not rendering correctly - php

I'm attempting to render barcodes for a MVC application in laminas.
I can build the barcode object, and the renderer, but when I render it it blacks out the whole screen with a single white square in the middle.
I tried replacing my code with the following from PHP Snippets, and I got the same issue. Am I missing a php extension or a composer package or something?
<?php use Common\Util;
use Laminas\Barcode\Object\Code128;
use Laminas\Barcode\Renderer\Image;
$barcode = new Code128([
'text' => 'PHP Snippets',
'barHeight' => 60,
'factor' => 2,
]);
$renderer = new Image([
'resource' => imagecreate($barcode->getWidth(), $barcode->getHeight()),
'barcode' => $barcode,
]);
ob_start();
$renderer->render();
$image = base64_encode(ob_get_contents());
ob_end_clean();
echo '<img src="data:image/png;base64,' . $image . '">';
?>
Here is the html that renders. For some reason it renders the whole <body> as a single image, from the bar code.
<body style="margin: 0px; background: #0e0e0e; height: 100%"><img style="-webkit-user-select: none;margin: auto;background-color: hsl(0, 0%, 90%);transition: background-color 300ms;" src="http://mymvcapp.com/orders"></body>
And here is what displays over the whole browser tab.

Related

How to display text in center of the page in mpdf

I want to display text in center of the page. But, it's not display proper. I used mpdf and i want to display using mpdf in php.
What I tried :
<?php
require_once __DIR__ . '/../bootstrap.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetDisplayMode('fullwidth');
$stylesheet = '<style>' . file_get_contents('example1.css') . '</style>'; // external css
$mpdf->WriteHTML($stylesheet, 1);
$mpdf->WriteHTML('<div id="center">
Hello World
</div>', 2);
$mpdf->Output('filename.pdf');
CSS :
#centrar{
margin:0;
width: 80%;
background-color: #000;
}
body { text-align: center }
Output :
enter image description here
It's display center in first line. I want to display in full page center part.
How to do that? Please help me. Thanks.

Get background image from webpage using DOM XPATH

I'm reading a webpage using PHP DOM/XPath and I've managed to get the text I need, but now I'm trying to get the src of the main image but I can't get it.
Also to complicate things, the source is different to the inspector.
Here is the source:
<div id="bg">
<img src="https://example.com/image.jpg" alt=""/>
</div>
And here is the element in the inspector:
<div class="media-player" id="media-player-0" style="width: 320px; height: 320px; background: url("https://example.com/image.jpg") center center / cover no-repeat rgb(208, 208, 208);" currentmouseover="16">
I've tried:
$img = $xpath->evaluate('substring-before(substring-after(//div[#id=\'bg\']/img, "\')")');
and
$img = $xpath->evaluate('substring-before(substring-after(//div[#class=\'media-player\']/#style, "background: url(\'"), "\')")');
but get nothing from either.
Here is my complete code:
$html = file_get_contents($externalurl);
$doc = new DOMDocument();
#$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$allChildNodesFromDiv = $xpath->query('//h1[#class="artist"]');
$releasetitle = $allChildNodesFromDiv->item(0)->textContent;
echo "</br>Title: " . $releasetitle;
$img = $xpath->evaluate('substring-before(substring-after(//div[#class=\'media-player\']/#style, "background: url(\'"), "\')")');
echo $image;
$img = $xpath->evaluate('substring-before(substring-after(//div[#id=\'bg\']/img, "\')")');
echo $image;
Not something I would normally suggest, but as the particular content you are after is loaded from javascript, BUT the content is in <script> tags, then it may be an easy one for a regex to extract. From your comment...
Ah yes, it appears in: poster :
'https://284fc2d5f6f33a52cd9f-ce476c3c56a27f320262daffab84f1af.ssl.cf3.rackcdn.com/artwork_5e74a44e1e004_CHAMPDL879D_5e74a44e4672b.jpg'
So this code looks the value of poster : '...',.
$html = file_get_contents($externalurl);
preg_match("/poster : '(.*)',/", $html, $matches);
echo $matches[1];
This can be prone to changes in the html, but it may work for now.

simplesoftwareio/simple-qrcode Image manipulation

I would like to know how to change image position using canvas in php
I'm trying to display the content inside the picture to bottom right
for($i=0;$i<$request->tables_count;$i++)
{
$milliseconds = round(microtime(true) * 1000);
$qrCodeName = 'storage/uploads/restaurants/' . $milliseconds . "-" . Carbon::now()->toDateString() . '.png';
$newTable=Table::create([
'qr_code' => $qrCodeName,
'num_table' => $i+1,
'status' => 1,
'restaurant_id' =>$restaurant->id,
]);
$numTableImage = Image::canvas(100, 350)->fill('rgba(0, 0, 0, 0.5)');
$numTableImage->text($newTable->num_table, 100, 200, function($font) {
$font->file(public_path('templates/backOffice/restaurant/fonts/OpenSans-Bold.ttf'));
$font->size(100);
$font->color('#fdf6e3');
$font->align('center');
});
$numTableImage->save('storage/uploads/restaurants/table'.$newTable->num_table.'.png');
$qrCode = str_random(10) . "-" . $newTable->id;
$data = $numTableImage->encode();
QrCode::format('png')->mergeString($data)->size(450)->generate($qrCode, $qrCodeName);
}
If I'm understanding what you are doing correctly... why not add the canvas element, inside a div, and then add a span after the canvas?
<style>
div {
position:relative;
}
span {
position: absolute;
bottom:0;
right:0;
}
</style>
<div>
<canvas></canvas>
<span>1</span>
</div>
This way, you can easily position the number image / whatever it is in the bottom right using CSS positioning.
The answer here: Position Relative vs Absolute? is useful for understanding CSS positioning.
Would this solution work for you? Or do you have constraints that mean it won't?
Or if they are separate elements created in the canvas, do the same, but set the element in the canvas as per the span.

Set Custom Page Title for HTML2PDF created pdf

I am using HTML2PDF library of PHP to create PDF from HTML. My page url is as below ::
http://domain.com/admin/invoice/invoicedownload
To create pdf using below code ::
ob_start();
$width_in_mm = 240;
$height_in_mm = 250;
$html2pdf = new HTML2PDF('P', array($width_in_mm,$height_in_mm), en, true, 'UTF-8', array(15, 10, 15, 10));
$html2pdf->setDefaultFont('Arial');
$html2pdf->pdf->SetTitle('Invoice Details');
$html2pdf->writeHTML($pdf_template);
$html2pdf->Output($file_name,'I');
die();
This code open pdf view in browser new tab.
Issue I am facing is that that new tab title is set as "Invoice Details - invoicedownload" but I want it as "Invoice Details". Always url's last part is append in title.
Please help me for this.
Thank you in advance.
In order to define your own tab title, you can use the 'embed' html tag.
File n°1 (main):
html, body, embed
{
width: 100%;
height: 100%;
}
body
{
margin: 0;
padding: 0;
}
<!DOCTYPE html>
<html>
<head>
<title>Your title</title>
</head>
<body>
<embed src=/path-to-your-pdf type='application/pdf'/>
</body>
</html>
File n°2 (/path-to-your-pdf):
<?php
$html2pdf = new Html2Pdf('P, 'A4', 'en');
$html2pdf->writeHTML($pdf_template);
$html2pdf->output();
Hope this helps ;)

Random background images in wordpress?

I am using WP 3.5.1, twenty twelve child theme and PHP 5.2 on my server.
I am trying to use a php script(that works on my other websites) in order to get random background-image but it's not working:
CSS:
body {
background: url(<?php include ("bg.php"); echo $selectedBg; ?>) no-repeat fixed;
}
PHP:
<?php
$bg = array('1.jpg','2.jpg','3.jpg','4.jpg','5.jpg');
$i = rand(0, count($bg)-1);
$selectedBg = "$bg[$i]";
?>
my php file is in the same folder as the jpg's.
Any ideas?
No errors. If I use background: url(1.jpg); (instead of php) it works fine but obviously shows 1 image.
Small solution:
We know that he have 5 images on the server:
'1.jpg','2.jpg','3.jpg','4.jpg','5.jpg'
So quick tip:
<body style="background: url('pahttoimages/<?= rand(1, 5) ?>.jpg') no-repeat top center;">
i think the CSS file can't explain your PHP code
try body {
background: url(<?php echo '1.jpg'; ?>) no-repeat fixed;
}
As far as I can see, your code is valid.
Except, you should really write the last line like:
$selectedBg = $bg[$i];
No need for quotes here.
I suspect this is what is causing the error:
my php file is in the same folder as the jpg's. Any ideas?
The background-image needs to be relative to the template-file you are using, not the PHP-file. You script will only work if the images are located in the same folder as the template-slices.
In my WP-installation, I have a template located in /wp-content/themes/mytemplate/ and template-graphics located in /wp-content/themes/mytemplate/images/. If I were to use your script, I would need to preappend /images/ before all the backgrounds in your array.
By the way, you should consider installing Firebug on Firefox and inspect the source. Is the background-name parsed into the template? Does loading the image return a 404 not found-error? Is the location and path correct?
background-image: url(<?php include ("bg.php"); echo $selectedBg; ?>);
background-position: fixed;
background-repeat: no-repeat;
Do this:
// bg.php
<?php
return array(
'1.jpg',
'2.jpg',
'3.jpg'
);
// Wordpress CSS
<?php
$imageUrls = include('bg.php');
$imageUrl = $imageUrls[ array_rand($imageUrls) ];
?>
.someClass {
background-image: url(<?php echo $imageUrl ?>);
}
background: url('<?php $a = array('darkred.jpg','red.gif','pink.png'); echo $a[array_rand($a)];?>');

Categories