Minzkraut
Minzkraut

Reputation: 2187

Directly save image generated with php

Im generating Identicons using a php script. Showing the result using a <img> works fine

$input="create_image/identicon.php?size=48&hash=$hashvalue";
$output=$username."_userimage.png";

This works:

echo "<img src='$input'>";

But this doesn't: (It just creates an empty file)

file_put_contents($output, file_get_contents($input));

and throws a no such file or directory exception, tho its the same url as the one used for the src property showing the image.

What is the problem trying to save it like that?

Im not sure whether the problem is the file_put_contents or the file_get_contents

Upvotes: 0

Views: 254

Answers (3)

Charlotte Dunois
Charlotte Dunois

Reputation: 4680

You can use file_get_contents() to load a file from the filesystem or from a remote host over HTTP. But you need to give PHP the full url in order to make use of the HTTP functionality (the php.ini also needs to allow this). Otherwise it literally searchs for the file create_image/identicon.php?size=48&hash=$hashvalue (variables of course replaced) on the filesystem.

Upvotes: 1

B.G.
B.G.

Reputation: 6016

file_x_contents works with real paths, and your img src works with the webpath. So file_get_contents needs the UNIX-Path. EDIT: As it was pointed out in the comments it is possible to work with URL's if enabled in the php.ini

Upvotes: 0

Zakaria
Zakaria

Reputation: 170

try this imagepng($input, $output); instead of file_put_contents

Upvotes: 0

Related Questions