Link to home
Start Free TrialLog in
Avatar of Russelauto
RusselautoFlag for United States of America

asked on

How to create a unique image file for each unique user and display the image via html.

I am trying to write  a couple of lines in php that I'm going to insert into the "activate" function of a registration.php. The goal is that upon account activation a predefined image will be written into a predefined directory. The image name must be unipqe for each user. I was thinking along the lines of this:
$im = blanktemplate.png
$filename = "images/users/$user_id"
imagejpeg($im, $filename, 100);
Is this a good method? Am I thinking along the right lines here?

Then I want to display that image in an html page. Something like  <img src={"$user_id".jpg}/>  I want the image that's being displayed to be determined by the user that's logged in at the time.
Avatar of wildzero
wildzero

well there can only be 1 instance of user_id right? ie, it's an autoincrement field in a database so yes that method would be fine.

You could also do it like
$filename = "images/users/".md5($user_id."xx1");
and then
 <img src={md5($user_id."xx1.jpg"}/>

which you would do when you don' want a user to be able to see other users images (just by changing the number.

The xx1 could be any string that you want (just keep them both the same).

Avatar of Russelauto

ASKER

Would the md5 hash prevent them from linking to the image outside of the site?
Ok, I can create an image with the following:
$user_id = 1234; //instanciated for testing
$im = imagecreatefromjpeg("images/blank.jpg");
$filename = "images/users/$user_id.jpg";
imagejpeg($im, $filename, 100);

But I can't seem to view the file using a variable.
<img src=images/users/{$user_id.}jpg/> Does not work. Maybe I'm not instanciating the variable correctly.  I'm trying it like this: <user_id="1234">
<img src="images/users/{$user_id.}jpg"/> Sorry, left out the quotations.
ASKER CERTIFIED SOLUTION
Avatar of wildzero
wildzero

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok I think you're code is good. I just need to somehow instanciate the variable user_id. I'm going to create a new question.