2013-02-17 94 views
-2
$file_extension= explode('.', $file_name); 
    $file_extn= strtolower(end($file_extension)); 
    $old_file_path = $user_data['profile_pic']; 

     function change_profile_image($user_id, $file_temp, $file_extn, $old_file_path){ 
      $file_path = 'core/images/profile/'. substr(md5(time()), 0, 20) . '.' . $file_extn; 
      move_uploaded_file($file_temp, $file_path); 
      if(file_exists($old_file_path) === true){unlink($old_file_path);} 
      mysql_query("UPDATE `users` SET `profile_pic` = '". mysql_real_escape_string($file_path) ."' WHERE `user_id` = " . (int)$user_id); 

      if($file_extn == 'png'){ 
      list($width, $height) = getimagesize($old_file_path); 
      $new_width = $width; 
      $new_height = $height; 
      $image_p = imagecreatetruecolor($new_width, $new_height); 
      $image = imagecreatefrompng($old_file_path); 

      imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
      imagejpeg($image_p, $file_path); 
if(file_exists($old_file_path) === true){unlink($old_file_path);}   
mysql_query("UPDATE `users` SET `profile_pic` = '". mysql_real_escape_string($file_path) ."' WHERE `user_id` = " . (int)$user_id); 
      } 

      if($file_extn == 'jpg' || $file_extn == 'jpeg'){ 
      list($width, $height) = getimagesize($file_path); 
      $new_width = $width; 
      $new_height = $height; 
      $image_p = imagecreatetruecolor($new_width, $new_height); 
      $image = imagecreatefromjpeg($file_path); 

      imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
      imagejpeg($image_p, $file_path); 
if(file_exists($old_file_path) === true){unlink($old_file_path);}   
mysql_query("UPDATE `users` SET `profile_pic` = '". mysql_real_escape_string($file_path) ."' WHERE `user_id` = " . (int)$user_id); 
      } 
     } 

圖像上傳但問題是上傳後沒有轉換圖像。問題是什麼?我相信if語句不起作用。上傳後將.PNG圖像轉換爲.JPEG

+0

你應該使用不區分大小寫的字符串比較(或「真正的」字符串比較而不是'=='? – Floris 2013-02-17 19:57:41

+0

不要緊 - 我只注意到頂部附近的'strtolower'函數。 .. – Floris 2013-02-17 19:59:27

+0

是的,它很煩人,這是行不通的 – Cole 2013-02-17 20:01:16

回答

0
... 
move_uploaded_file($file_temp, $file_path); 
if(file_exists($old_file_path) === true){unlink($old_file_path);} 
... 
if($file_extn == 'png'){ 
     list($width, $height) = getimagesize($old_file_path); 
... 

我相信,因爲你使用getimagesize($old_file_path)之前刪除$old_file_path失敗,所以imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);無關的努力。

嘗試評論第一個文件刪除後,在move_uploaded_file之後。肯定是這個問題...

+0

不,沒有解決問題。 – Cole 2013-02-17 21:02:58