0

Here is my code;

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

?>

<?php

$loader = require __DIR__ . '/vendor/autoload.php';

?>

<?php

use mikehaertl\wkhtmlto\Image;

$image = new \mikehaertl\wkhtmlto\Image('https://www.google.co.uk/search?q=what+is+the+time&oq=what+is+the+time&aqs=chrome.0.69i59j0l5.1536j0j4&sourceid=chrome&ie=UTF-8');
$image->setOptions(array(
'binary' => '/usr/local/bin/wkhtmltoimage',
'type' => 'png'
));

$image->saveAs('tmp/page.png');

$image = 'tmp/page.png';
$imageData = base64_encode(file_get_contents($image));
echo '<img src="data:image/png;base64,'.$imageData.'">';

?>

I would like the file page.png located in (tmp/page.png) to adopt a random filename every time this script runs, I believe I need to do something like;

$filename = uniqid(rand(), true) . '.png';

Then replace;

$image->saveAs('tmp/page.png');

With;

$image->saveAs('tmp/$filename');

But I am not too sure how I lay that out when it comes to ( . / ' / " ) etc.. Thanks.

  • 1
    Take a look at [tempnam()](https://secure.php.net/manual/en/function.tempnam.php), see if that helps in any way. – brombeer Apr 27 '18 at 08:01
  • How would I use that within this; $image->saveAs('tmp/page.png');. I do not fully understand the dots (.), quotation marks (") and how I need to position it. – user9708574 Apr 27 '18 at 08:07
  • `uniqid()` "should" be random enough with the `$more_entropy` parameter enabled, there' no need to feed it with a random prefix. And variable interpolation does not happen inside single-quoted strings. Apart from that I'm not sure of what your doubt is... – Álvaro González Apr 27 '18 at 08:07
  • http://php.net/manual/en/language.operators.string.php – deceze Apr 27 '18 at 08:07
  • Alvaro, I am not disputing if anything is random enough. This question is in regards to how I should be using $filename within this specific line ($image->saveAs('tmp/HOWDOIFORMAT$FILENAME');). Deceze, this is one of them things I need to see, before I understand why I am doing it wrong. I know that link is relevant I just DO NOT understand it. – user9708574 Apr 27 '18 at 08:16
  • Well... You need **double** quotes `$image->saveAs("tmp/$filename");` :-? You want the variable **contents**, not its name. – Álvaro González Apr 27 '18 at 09:18
  • That was exactly what it needed, now I understand slightly. Should of been one of these ' instead of these "... I appreciate that. – user9708574 Apr 27 '18 at 09:55

0 Answers0