I have a little blackout with the simplest of the simplest.
I have a folder called "userpics" and in that folder there are 100x100 profile images named eg user-25.jpg. In that folder there also is a picture called user-none.jpg with a silhouette, used as a picture on a comments function when non-registered users posts a comment.
What I want to do is when a user is registered the user-none.jpg (with the silhouette) is copied and named eg user-26.jpg. The user can then later upload a new profile image from his account.
I just can't figure out how to copy the image. I tried this:
function CreateDummyProfileImage($user_id) {
$new_dummy = "images/userpics/user-".$user_id.".jpg";
$dummy = "images/userpics/user-none.jpg";
list($current_width, $current_height) = getimagesize($dummy);
$this->imageSizeH = 100;
$this->imageSizeW = 100;
$canvas = imagecreatetruecolor($this->imageSizeW, $this->imageSizeH);
$current_image = imagecreatefromjpeg($dummy);
imagecopy($canvas, $current_image, 0, 0, 0, 0, $current_width, $current_height);
imagejpeg($canvas, $new_dummy, 100);
}
But that doesn't seem to work.
SOLVED :
/**********************************************************
* Create a user with the basic info the user posted
* in the creation form.
*
* @param string | The name of the user
* @param string | The users email address
* @param string | The password to login with
**********************************************************/
function CreateUser($name, $email, $password) {
/***
* First we need no check if the email already exists in the system.
* The statement right below will do that
***/
$check_if_user_exists_sql = $this->db->selectSQL("email", "tdic_users", "email = '".$email."'");
$check_if_user_exists_result = $this->db->SQLquery($check_if_user_exists_sql);
if(mysql_num_rows($check_if_user_exists_result) > 0) {
$this->main->txtOutput("The user with email ". $email ." is already registered", "TXT_ERR"); //The email already exists in the system! No further processing from here!
} else {
/*** If the email couldn't be found we will create a new user ***/
/*** Array containing the fields required for user creation ***/
$fields = array(
"name" => $name,
"email" => $email,
"password" => $this->main->SaltMe(sha1($password)),
"user_date_created" => time(), //The current time
"user_last_logged_in" => 0,
"profileimage" => "user-none.jpg" //SET THIS INSTEAD OF COPYING THE AND RENAMING THE IMAGE
);
$create_user_sql = $this->db->insertSQL('tdic_users', $fields);
$create_user_result = $this->db->SQLquery($create_user_sql);
if($create_user_result) {
$this->ActivationMail($name, $email);
$this->main->txtOutput("You are now registered. In shorty you will recieve an email with an activation link. Please click that link to activate your account.", "TXT_OK"); //Everything went well - we will tell the user that!
} else {
$this->main->txtOutput("An error occured", "TXT_ERR"); //Whoops! Something went wrong! This is unexpected!
}
}
}
You don't need to go near GD functions for this - you can just copy the file .
function CreateDummyProfileImage($user_id) {
$res = copy("images/userpics/user-none.jpg", "images/userpics/user-".$user_id.".jpg");
return 'image'.($res ? '' : ' not').' created';
}
However, the model you describe isn't exactly optimal - copying a file countless times. Beter would be to conditionally show one file or the other at the point of output:
$user_pic = 'path_to_user_pic.jpg';
$user_pic = file_exists($user_pic) ? $user_pic : 'path_to_silhouette.jpg';
Stream copy is relatively faster
function stream_copy($src, $dest)
{
$fsrc = fopen($src,'r');
$fdest = fopen($dest,'w+');
$len = stream_copy_to_stream($fsrc,$fdest);
fclose($fsrc);
fclose($fdest);
return $len;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.