0

我对最简单的最简单的有点停电。

我有一个名为“userpics”的文件夹,在该文件夹中有 100x100 的个人资料图像,名称为 user-25.jpg。在该文件夹中还有一张名为 user-none.jpg 的带有剪影的图片,在非注册用户发表评论时用作评论功能上的图片。

我想要做的是当用户注册时,user-none.jpg(带有剪影)被复制并命名为例如user-26.jpg。用户随后可以从他的帐户上传新的个人资料图像。

我只是不知道如何复制图像。我试过这个:

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);   
    }

但这似乎不起作用。

解决了 :

/**********************************************************
     * 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!
            }
        }
    }
4

2 回答 2

2

您不需要为此接近 GD 函数 - 您只需复制文件即可。

function CreateDummyProfileImage($user_id) {
    $res = copy("images/userpics/user-none.jpg", "images/userpics/user-".$user_id.".jpg");
    return 'image'.($res ? '' : ' not').' created';
}

但是,您描述的模型并不完全是最佳的 - 无数次复制文件。更好的是在输出点有条件地显示一个文件或另一个:

$user_pic = 'path_to_user_pic.jpg';
$user_pic = file_exists($user_pic) ? $user_pic : 'path_to_silhouette.jpg';
于 2012-07-04T10:35:19.417 回答
0

流复制相对较快

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;
}
于 2013-11-28T10:48:30.037 回答