[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

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.
0
Russelauto
Asked:
Russelauto
  • 3
  • 2
1 Solution
 
wildzeroCommented:
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).

0
 
RusselautoAuthor Commented:
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">
0
 
RusselautoAuthor Commented:
<img src="images/users/{$user_id.}jpg"/> Sorry, left out the quotations.
0
 
wildzeroCommented:
The image wouldn't prevent hotlinking - however you can use htaccess to handle that
see -> http://www.google.co.nz/search?q=prevent+hot+linking

<img src="images/users/<?PHP echo $user_id; ?>.jpg"/> Sorry, left out the quotations.
or
<img src="images/users/<?=$user_id; ?>.jpg"/> Sorry, left out the quotations.

0
 
RusselautoAuthor Commented:
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now