Member Avatar

I have three lines of code;

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

I would like to have a random filename for page.png, so I have chosen to use the following;

uniqid()

How would I go about using uniqid() within each of those three lines. So it appears as;

$image->saveAs('tmp/randomfilename.png');
$image = 'tmp/randomfilename.png';
unlink('tmp/randomfilename.png');

The full page code is;

<?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.'">';

unlink('tmp/page.png');
die();

?>

Recommended Answers

http://php.net/manual/en/function.uniqid.php notes this fails for your needs. Read hackan's reply to see it returns same value (or string) when called too quickly.

That could create some very hard to trace bugs.

Jump to Post

All 2 Replies

The simplest solution could be to create a db table for your images (or files generally) first save a row in that file that has an auto increment id and then save it with that id

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.