2012-05-25 74 views
0

我做了一個可以用Facebook帳號登錄的網站。在CodegIgniter中上傳facebook url照片PHP

我可以很容易地放入基本的用戶信息,但照片根本不起作用。

這裏是我上傳照片的代碼。

$imageURL = 'https://graph.facebook.com/'.$fb_id.'/picture?type=large'; 

$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/'.$this->config->item('cf_dir_uphoto_temp'); 
$config['allowed_types'] = 'gif|png|jpg'; 
$config['file_name'] = $this->session->userdata(SESSION_USERID).time(); 
$config['max_filename'] = '70'; 
$config['overwrite'] = FALSE; 
$config['remove_spaces'] = TRUE; 

$this->load->library('upload', $config); 

if($this->upload->do_upload('photo')) 
{ 
$data = $this->upload->data(); 

$src_width = $src_height = ($data['image_width'] <= $data['image_height'])? $data['image_width'] : $data['image_height']; 
$buffer_width = $buffer_height = 150; 

$buffer_x = 0; 
$buffer_y = 0; 

$src_x = sprintf('%d', ($data['image_width'] - $src_width)/2); 
$src_y = sprintf('%d', ($data['image_height'] - $src_height)/2); 

$save_cropfile = $_SERVER['DOCUMENT_ROOT'].'/'.$this->config->item('cf_dir_uphoto_original').$data['file_name']; 
$quality = 100; 

if($data['file_type'] == 'image/jpeg') 
{ 
    $src_img = imagecreatefromjpeg($data['full_path']); 
    $buffer_img = ImageCreateTrueColor($buffer_width, $buffer_height); 

    imagecopyresampled($buffer_img, $src_img, 
         $buffer_x, $buffer_y, $src_x, $src_y, 
         $buffer_width, $buffer_height, $src_width, $src_height 
    ); 

    imagejpeg($buffer_img, $save_cropfile, $quality); 
}else if($data['file_type'] == 'image/gif') 
{ 
    $src_img = imagecreatefromgif($data['full_path']); 
    $buffer_img = ImageCreateTrueColor($buffer_width, $buffer_height); 

    imagecopyresampled($buffer_img, $src_img, 
         $buffer_x, $buffer_y, $src_x, $src_y, 
         $buffer_width, $buffer_height, $src_width, $src_height 
    ); 

    imagegif($buffer_img, $save_cropfile); 
}else if($data['file_type'] == 'image/png') 
{ 
    $src_img = imagecreatefrompng($data['full_path']); 
    $buffer_img = ImageCreateTrueColor($buffer_width, $buffer_height); 

    imagecopyresampled($buffer_img, $src_img, 
         $buffer_x, $buffer_y, $src_x, $src_y, 
         $buffer_width, $buffer_height, $src_width, $src_height 
    ); 
    imagepng($buffer_img, $save_cropfile); 
} 

$config['image_library'] = 'gd2'; 
$config['source_image'] = $save_cropfile; 
$config['new_image'] = $_SERVER['DOCUMENT_ROOT'].'/'.$this->config->item('cf_dir_uphoto_thumb'); 
$config['width'] = 50; 
$config['height'] = 50; 

$this->load->library('image_lib', $config); 
$this->image_lib->resize(); 

unlink($data['full_path']); 

$p_data = $this->user->set_user_photo($data['file_name']); 


}else 
    echo $this->upload->display_errors(); 

這是非常錯誤的代碼。

$ imageURL是將被轉換爲jpg地址的圖像地址。

你能給我一個想法如何做到這一點?謝謝。

回答

2

這不是做Facebook登錄的好方法。如果您想在應用中顯示用戶的照片,則應使用該API,而不要將用戶的照片下載到服務器。只是不喜歡簡單的東西:

<img src="https://graph.facebook.com/{$fb_id}/picture?type=large" alt="{$username}" />

這將讓你從Facebook使用的圖像挺直,當用戶改變他們的個人資料圖片會自動更新。如果用戶更新了他們的個人資料,您的代碼將不會更新用戶的照片。

0

如果您正在嘗試下載facebook用戶個人資料照片,請嘗試使用此代碼,但首先要創建一個名爲image的文件夾並確保其已啓用寫入權限。

<?php 

$fullpath = 'image/image.jpg'; 
$url = "https://graph.facebook.com/{$user_id}/picture?type=large"; 

function save_image($img, $fullpath) { 
    $rawdata = file_get_contents($img); 
    $fp = fopen($fullpath, 'w+'); 
    chmod($fullpath, 0777); 
    fwrite($fp, $rawdata); 
    fclose($fp); 
} 



$headers = get_headers($url, 1); 

if (isset($headers['Location'])) { 
$url1 = $headers['Location']; // string 
} else { 
$url1 = false; // nothing there? .. weird, but okay! 
} 
save_image($url1, $fullpath); 
?>