在php中以多种尺寸上传图片

时间:2014-03-27 13:53:07

标签: php

我的网站有FB登录功能。第一次当用户点击Facebook登录按钮时,我们会获取用户fb详细信息以及个人资料照片。

获取个人资料图片代码: -

function getprofilepic($token, $userid){
   //be sure to update this access token
    //must be https when using an access token
    $url="https://graph.facebook.com/$userid?fields=picture.type(large)&access_token=".$token;
    //load and setup CURL
    // Initialize session and set URL.
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);

    // Set so curl_exec returns the result instead of outputting it.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Get the response and close the channel.
    $response = curl_exec($ch);
    $res_val = json_decode($response);
    $prof_pic = $res_val->picture->data->url;
    curl_close($ch);
    return $prof_pic;
}

但现在我想以多种尺寸存储此个人资料图片。所以我为此创建了另一个功能, 代码: -

function resize($width, $height, $up_file_tmp, $up_file_name, $up_file_type){
    /* Get original image x y*/
    list($w, $h) = getimagesize($up_file_tmp);
    //echo 'Width:-'.$w.'Height:-'.$h; exit();

    /* calculate new image size with ratio */
    $ratio = max($width/$w, $height/$h);
    $h = ceil($height / $ratio);
    $x = ($w - $width / $ratio) / 2;
    $w = ceil($width / $ratio);

    /* new file name */
    $path = 'admin/img/'.$width.'x'.$height.'_'.$up_file_name;
    /* read binary data from image file */
    $imgString = file_get_contents($up_file_tmp);
    /* create image from string */
    $image = imagecreatefromstring($imgString);
    $tmp = imagecreatetruecolor($width, $height);
    imagecopyresampled($tmp, $image, 0, 0, $x, 0, $width, $height, $w, $h);
    /* Save image */
    switch ($up_file_type) {
        case 'image/jpeg':
            imagejpeg($tmp, $path, 100);
            break;
        case 'image/png':
            imagepng($tmp, $path, 0);
            break;
        case 'image/gif':
            imagegif($tmp, $path);
            break;
        default:
            exit;
            break;
    }
    return $path;
    /* cleanup memory */
    imagedestroy($image);
    imagedestroy($tmp); 
}

但是在上面的函数中我将如何传递image的临时名称。如果您有任何解决方案,请与我分享。

0 个答案:

没有答案