English 中文(简体)
上传 facebook URL 照片, 载于 CodegIgniter PHP
原标题:Uploading facebook url photo in CodegIgniter PHP

我建立了一个网站,可以登录 与Facebook账户。

我可以轻易地提供基本的用户信息,但照片根本不有效。

这是我的上传照片的密码

$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 地址的图像地址 。

你能给我一个想法 如何让这一切发生吗?

问题回答

这不是做Facebook登录的好方法。 如果您想要在应用程序中显示用户照片, 您应该使用 API, 而不是将用户照片下载到服务器上。 只需做一些简单的事情, 比如 :

这将允许您直接使用来自 Facebook 的图像, 当用户更改其配置图片时会自动更新。 如果用户更新其配置, 您的代码不会更新用户的配置 。

如果您试图下载facebook 用户简介照片, 请尝试此代码, 但先创建名为图像的文件夹, 并确保它已启用写入权限 。

<?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);
?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签