复制现有图像并使用php重命名

时间:2012-07-04 10:25:25

标签: php image file user-profile

我有一个最简单的最简单的停电。

我有一个名为“userpics”的文件夹,在该文件夹中有100x100个配置文件图像,例如:用户25.jpg。在该文件夹中还有一张名为user-none.jpg的图片,带有一个轮廓,当非注册用户发表评论时,用作评论功能的图片。

我想要做的是当用户注册时,user-none.jpg(带有轮廓)被复制并命名为例如用户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!
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

你不需要靠近GD功能 - 你可以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';
}

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

$user_pic = 'path_to_user_pic.jpg';
$user_pic = file_exists($user_pic) ? $user_pic : 'path_to_silhouette.jpg';

答案 1 :(得分: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;
}