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

Posted on 2007-10-03
Last Modified: 2008-01-09
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.
Question by:Russelauto
    LVL 10

    Expert Comment

    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).


    Author Comment

    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">

    Author Comment

    <img src="images/users/{$user_id.}jpg"/> Sorry, left out the quotations.
    LVL 10

    Accepted Solution

    The image wouldn't prevent hotlinking - however you can use htaccess to handle that
    see ->

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


    Author Comment

    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.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now