<?php

require_once 'src/facebook.php';

try {
    $facebook = new Facebook(array(
                'appId' => '495811327106485',
                'secret' => 'b5d0c4862fe1cfab1b1c6fc09ee0cbab',
                'cookie' => true
            ));


    if (!$facebook->getUser()) {
        header("Location:{$facebook->getLoginUrl(array('scope' => 'user_status,publish_stream,user_photos,user_videos'))}");
        exit;
    }
    $me = $facebook->api('/me');
} catch (Exception $e) {
    echo $e->getMessage();
    echo '<p>Please try clearing your browser cookies.</p>';
    die;
}


$image_width = 250;
$image_height = 150;

if ($_POST['albumphotodata'] != '') {
    
    for ($i = 0; $i < count($_POST['albumphotodata']); $i++) {
        $album_id = $_POST['albumphotodata'][$i];
        $photos = $facebook->api("/{$album_id}/photos");
        $j = 0;
        foreach ($photos['data'] as $photo) {
            //echo $photo['source'];
             $content = file_get_contents($photo['source']);
             $img_path = 'upimages/main/'.basename($photo['source']);
             
              if(!file_exists($img_path))
              file_put_contents($img_path, $content);
              $imgdata=basename($photo['source']);
              $var_thumb = upload_image($imgdata, $image_width, $image_height, 'thumb_', $sess, $image_width, $image_height, 'thumb_');
              //echo "<input type='checkbox' name='photodata[]' value='{$imgdata}' id='photodata' /><img width='100' height='100' src='{$img_path}' />"; 
            $j++;
        }
    }
    echo "Your Album Successfull uploaded and go to<a href='index.php'>Back</a>".$i.'------>'.$j;
}
else if ($_POST['photodata'] != '' && $_POST['album_id'] !='')
{
    for ($i = 0; $i < count($_POST['photodata']); $i++) {
             $content = file_get_contents($_POST['photodata'][$i]);
             $img_path = 'upimages/main/'.basename($_POST['photodata'][$i]);
              if(!file_exists($img_path))
              file_put_contents($img_path, $content);
              $imgdata=basename($_POST['photodata'][$i]);
              $var_thumb = upload_image($imgdata, $image_width, $image_height, 'thumb_', $sess, $image_width, $image_height, 'thumb_');
    }
    echo "Your Image Successfull uploaded and go to<a href='index.php'>Back</a>".$i;
}



function upload_image($filefield, $thumb_width, $thumb_height, $prefix, $sess, $large_width, $large_height, $prefix2 = '') {
    //echo $filefield;exit;///////////////// echo 'tttttttt'.$filefield;
    $file_photo = basename($filefield);

    $imagename = $sess . $file_photo;
    $uploaddir = 'upimages/thumb/';
    $file_photo = $sess . basename($filefield);
    $file = $uploaddir . $file_photo;

    $tmp = $filefield;
    $uploadedfile = 'upimages/main/' . $filefield;

    // Create an Image from it so we can do the resize
    $image_info = getimagesize($uploadedfile);

    switch ($image_info['mime']) {
        case 'image/gif':
            $img_create = 'ImageCreateFromGIF';
            break;
        case 'image/jpeg':
            $img_create = 'ImageCreateFromJPEG';
            break;
        case 'image/png':
            $img_create = 'ImageCreateFromPNG';
            break;
        case 'image/bmp':
            $img_create = 'ImageCreateFromBMP';
            break;
    }
    // $src = imagecreatefromjpeg($uploadedfile);
    $photoexe = explode('.', $imagename);
    $photoexename = $photoexe[count($photoexe) - 1];

    if ($photoexename == 'bmp') {

        $src = $this->configObj->glLibraryObj->imagecreatefrombmp($uploadedfile);
    } else {
        $src = @$img_create($uploadedfile);
    }
    // Capture the original size of the uploaded image
    list($width, $height) = getimagesize($uploadedfile);

    $newwidth = $width;
    $newheight = $height;

    $tmp = imagecreatetruecolor($newwidth, $newheight);

    // this line actually does the image resizing, copying from the original // image into the $tmp image
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    // now write the resized image to disk.
    imagejpeg($tmp, $file, 90);

    imagedestroy($src);
    imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request  // has completed.

    $source1 = $uploaddir . $file_photo;

    //$sess = time();
    $thumbfilename = $prefix . $sess . $file_photo;

    $target1 = $uploaddir . $prefix . $file_photo;

   // $target2 = $uploaddir . $prefix2 . $file_photo;

    if ($large_width > 0) {


        $returnthumbmsg1 = generate_thumb($source1, $thumb_width, false, false, $target1, false, null, $thumb_width, $thumb_height);
    }
    /*if ($large_width > 0) {
        $returnthumbmsg2 = generate_thumb($source1, $thumb_width, false, false, $target2, false, null, $large_width, $large_height);
    }*/

    return $imagename;
}

function generate_thumb($source_filename, $thumb_x, $square = false, $border = false, $cache_output = null, $watermark = false, $watermark_text = null, $req_width, $req_height) {

    $image_info = getimagesize($source_filename);
    $image_width = $image_info[0];
    $image_height = $image_info[1];

    if ($req_width != '' || $req_height != '') {
        // workaround for v1.6.2 where the GIF images arent recognized.
        $img_create = 'ImageCreateFromJPEG';
        //print_r($image_info);exit;
        switch ($image_info['mime']) {
            case 'image/gif':
                $img_create = 'ImageCreateFromGIF';
                break;
            case 'image/jpeg':
                $img_create = 'ImageCreateFromJPEG';
                break;
            case 'image/png':
                $img_create = 'ImageCreateFromPNG';
                break;
            case 'image/bmp':
                $img_create = 'ImageCreateFromBMP';
                break;
        }


        if (($image_width >= $req_width ) && ($image_height >= $req_height)) {
            $pref_height = intval(($req_width / $image_width) * $image_height);
            $pref_width = intval(($req_height / $image_height) * $image_width);

            if ($pref_height >= $req_height) {
                $size = $req_width . "*" . $pref_height;
                //                    $shtWidth=$req_width;
                //                    $shtHeight=$pref_height;
                $shtWidth = $pref_width;
                $shtHeight = $req_height;
                $returnmsg = 'true';
            } else if ($pref_width >= $req_width) {
                $size = $pref_width . "*" . $req_height;
                //                    $shtWidth=$pref_width;
                //                    $shtHeight=$req_height;
                $shtWidth = $req_width;
                $shtHeight = $pref_height;
                //$returnmsg= 'true';
                $returnmsg = 'true';
            }
        } else if (($image_width >= $req_width ) && ($image_height < $req_height)) {
            $pref_height = intval(($req_width * $image_height) / $image_width);
            $size = $pref_width . "*" . $req_height;
            $shtWidth = $req_width;
            $shtHeight = $pref_height;
            $returnmsg = 'INVALID';
        } else if (($image_height >= $req_height ) && ($image_width < $req_width)) {
            $pref_width = intval(($req_height * $image_width) / $image_height);
            $size = $pref_width . "*" . $req_height;
            $shtWidth = $pref_width;
            $shtHeight = $req_height;
            $returnmsg = 'INVALID';
        } else {
            $shtWidth = $image_width;
            $shtHeight = $image_height;
            $returnmsg = 'INVALID';
        }

        $start_x = 0;
        $start_y = 0;

        $thumb_input = @$img_create($source_filename);

        if (!$thumb_input) /* See if it failed */ {
            $thumb_input = imagecreate($thumb_x, $thumb_y); /* Create a blank image */

            $white_color = imagecolorallocate($thumb_input, 255, 255, 255);
            $black_color = imagecolorallocate($thumb_input, 0, 0, 0);

            imagefilledrectangle($thumb_input, 0, 0, 150, 30, $white_color);

            imagestring($thumb_input, 1, 5, 5, 'Error loading ' . $source_filename, $black_color); /* Output an errmsg */

            imagejpeg($thumb_input, '', 90);
            imagedestroy($thumb_input);
        } else {
            $thumb_output = image_create_function($shtWidth, $shtHeight);
            $border_x = $thumb_x - 1;
            $border_y = $thumb_y - 1;
            $background_color = imagecolorallocate($thumb_output, 255, 255, 255);
            imagefill($thumb_output, 0, 0, $background_color);

            image_copy_function($thumb_output, $thumb_input, $start_x, $start_y, 0, 0, $shtWidth, $shtHeight, $image_width, $image_height);

            if ($border) {
                $border_color = imagecolorallocate($thumb_output, 0, 0, 0);
                imagerectangle($thumb_output, 0, 0, $border_x, $border_y, $border_color);
            }

            if ($watermark) {
                // Get identifier for white
                $white = imagecolorallocate($thumb_output, 255, 255, 255);

                // Add text to image
                imagestring($thumb_output, 20, 5, $thumb_y - 20, $watermark_text, $white);
            }

            touch($cache_output);
            imagejpeg($thumb_output, $cache_output, 90);

            imagedestroy($thumb_output);
        }
    }
    //echo $returnmsg;exit;
    return $returnmsg;
}

function image_create_function($x_size, $y_size) {
    $image_create_function = 'ImageCreate';

    if (gd_version() >= 2.0) {
        $image_create_function = 'ImageCreateTrueColor';
    }

    if (!function_exists($image_create_function)) {
        return false;
    }
    return $image_create_function($x_size, $y_size);
}

function gd_version() {
    global $code;

    if (empty($result)) {
        if (!function_exists('gd_info')) {
            $gd_info = $this->gd_info_alternate();
        } else {
            $gd_info = gd_info();
        }

        if (substr($gd_info['GD Version'], 0, strlen('bundled (')) == 'bundled (') {
            $result = (float) substr($gd_info['GD Version'], strlen('bundled ('), 3);
        } else {
            $result = (float) substr($gd_info['GD Version'], 0, 3);
        }
    }
    return $result;
}

function image_copy_function($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {
    $image_copy_function = 'ImageCopyResized';

    if (gd_version() >= 2.0) {
        $image_copy_function = 'ImageCopyResampled';
    }

    if (!function_exists($image_copy_function)) {
        return false;
    }

    return $image_copy_function($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
}

?>