doutu9810 2012-07-04 10:25 采纳率: 0%
浏览 63
已采纳

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

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 e.g. 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 e.g. 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!
            }
        }
    }
  • 写回答

2条回答 默认 最新

  • douzhenzu0247 2012-07-04 10:35
    关注

    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';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么