2012-07-04 21 views
0

我用最簡單的最簡單的方法來處理一些小黑。複製現有圖像並使用php重命名

我有一個名爲「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! 
      } 
     } 
    } 
+0

錯誤參數在['imagecopy的()'](HTTP:// www.php.net/manual/en/function.imagecopy.php)和['imagejpeg()'](http://php.net/manual/en/function.imagejpeg.php) – diEcho

+0

錯誤的觀點如何? – Utkanos

+0

http://php.net/manual/en/function.copy.php – Cups

回答

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

但是,您描述的模型並非完全最佳 - 無數次複製文件。尤爲明顯。將有條件地顯示一個文件或另一個在輸出的點:

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

阿......當然!我沒有這樣想過! :) 我可以在用戶註冊時將用戶圖像設置爲user-none.jpg,並在用戶上傳新圖像時更新該字段! 少即是多! 非常感謝! –

+0

我已經編輯我的問題與解決方案根據您的答案:) 「profileimage」=>「user-none.jpg」//設置此複製和重新生成圖像是什麼魔術! –

+0

我已經使用了這段代碼,但它只複製文本中的名字,而不是整數。例如:我有圖像名稱168519.jpg,20121206-145501.jpg,bigimages.jpg。它只複製最後一張圖片。你能爲我提供任何解決方案嗎?提前致謝。 –

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