function wppa_get_photo_url($id, $system = 'flat', $x = '0', $y = '0')
{
    global $blog_id;
    global $wppa_supported_stereo_types;
    // Does photo exist?
    $thumb = wppa_cache_thumb($id);
    if (!$thumb) {
        return '';
    }
    // Set owner if required
    wppa_set_owner_to_name($id);
    // Must re-get cached thumb
    $thumb = wppa_cache_thumb($id);
    if (is_feed() && wppa_switch('feed_use_thumb')) {
        return wppa_get_thumb_url($id, $system);
    }
    // If in the cloud...
    $for_sm = wppa('for_sm');
    // Social media do not accept cloud images
    $is_old = wppa_too_old_for_cloud($id);
    if (wppa_cdn('front') && !wppa_is_multi($id) && !$is_old && !wppa_is_stereo($id) && !$for_sm) {
        switch (wppa_cdn('front')) {
            case 'cloudinary':
                $x = round($x);
                $y = round($y);
                $prefix = is_multisite() && !WPPA_MULTISITE_GLOBAL ? $blog_id . '-' : '';
                $t = wppa_switch('enlarge') ? 'fit' : 'limit';
                $q = wppa_opt('jpeg_quality');
                $sizespec = $x && $y ? 'w_' . $x . ',h_' . $y . ',c_' . $t . ',q_' . $q . '/' : '';
                $s = is_ssl() ? 's' : '';
                $url = 'http' . $s . '://res.cloudinary.com/' . get_option('wppa_cdn_cloud_name') . '/image/upload/' . $sizespec . $prefix . $thumb['id'] . '.' . $thumb['ext'];
                return $url;
                break;
        }
    }
    // Stereo?
    if (wppa_is_stereo($id)) {
        // Get type from cookie
        $st = isset($_COOKIE["stereotype"]) ? $_COOKIE["stereotype"] : 'color';
        if (!in_array($st, $wppa_supported_stereo_types)) {
            $st = '_flat';
        }
        // Get glass from cookie
        $sg = 'rc';
        if (isset($_COOKIE["stereoglass"]) && $_COOKIE["stereoglass"] == 'greenmagenta') {
            $sg = 'gm';
        }
        // Create the file if not present
        if (!is_file(wppa_get_stereo_path($id, $st, $sg))) {
            wppa_create_stereo_image($id, $st, $sg);
        }
        // Build the url
        if ($st == '_flat') {
            $url = WPPA_UPLOAD_URL . '/stereo/' . $id . '-' . $st . '.jpg' . '?ver=' . get_option('wppa_photo_version', '1');
        } else {
            $url = WPPA_UPLOAD_URL . '/stereo/' . $id . '-' . $st . '-' . $sg . '.jpg' . '?ver=' . get_option('wppa_photo_version', '1');
        }
        // Done
        return $url;
    }
    if (get_option('wppa_file_system') == 'flat') {
        $system = 'flat';
    }
    // Have been converted, ignore argument
    if (get_option('wppa_file_system') == 'tree') {
        $system = 'tree';
    }
    // Have been converted, ignore argument
    if (!is_numeric($id) || $id < '1') {
        wppa_dbg_msg('Invalid arg wppa_get_photo_url(' . $id . ')', 'red');
    }
    if ($system == 'tree') {
        return WPPA_UPLOAD_URL . '/' . wppa_expand_id($thumb['id']) . '.' . $thumb['ext'] . '?ver=' . get_option('wppa_photo_version', '1');
    } else {
        return WPPA_UPLOAD_URL . '/' . $thumb['id'] . '.' . $thumb['ext'] . '?ver=' . get_option('wppa_photo_version', '1');
    }
}
function wppa_create_stereo_image($id, $type, $glass)
{
    static $f299;
    static $f587;
    static $f114;
    // Init
    if ($glass == 'rc') {
        $glass = 'redcyan';
    }
    if (!is_array($f299)) {
        $i = 0;
        while ($i < 256) {
            $f299[$i] = floor(0.299 * $i + 0.5);
            $f587[$i] = floor(0.587 * $i + 0.5);
            $f114[$i] = floor(0.114 * $i + 0.5);
            $i++;
        }
    }
    // Feature enabled?
    if (!wppa_switch('enable_stereo')) {
        return;
    }
    // Init.
    $is_stereo = wppa_is_stereo($id);
    if (!$is_stereo) {
        return;
    }
    $stereodir = WPPA_UPLOAD_PATH . '/stereo';
    if (!is_dir($stereodir)) {
        wppa_mkdir($stereodir);
    }
    $fromfile = wppa_get_photo_path($id);
    $tofile = wppa_get_stereo_path($id, $type, $glass);
    $sizes = getimagesize($fromfile);
    $width = $sizes['0'] / 2;
    $height = $sizes['1'];
    $fromimage = wppa_imagecreatefromjpeg($fromfile);
    $toimage = imagecreatetruecolor($width, $height);
    if ($is_stereo == 1) {
        $offset1 = 0;
        $offset2 = $width;
    } else {
        $offset1 = $width;
        $offset2 = 0;
    }
    // Do the dirty work
    switch ($type) {
        case 'color':
            for ($y = 0; $y < $height; $y++) {
                for ($x = 0; $x < $width; $x++) {
                    $rgb1 = imagecolorat($fromimage, $x + $offset1, $y);
                    $r1 = $rgb1 >> 16 & 0xff;
                    $g1 = $rgb1 >> 8 & 0xff;
                    $b1 = $rgb1 & 0xff;
                    $rgb2 = imagecolorat($fromimage, $x + $offset2, $y);
                    $r2 = $rgb2 >> 16 & 0xff;
                    $g2 = $rgb2 >> 8 & 0xff;
                    $b2 = $rgb2 & 0xff;
                    // Red - Cyan glass
                    if ($glass == 'redcyan') {
                        $newpix = $r2 << 16 | $g1 << 8 | $b1;
                    } else {
                        $newpix = $r1 << 16 | $g2 << 8 | $b1;
                    }
                    imagesetpixel($toimage, $x, $y, $newpix);
                }
            }
            imagejpeg($toimage, $tofile, wppa_opt('jpeg_quality'));
            break;
        case 'gray':
            for ($y = 0; $y < $height; $y++) {
                for ($x = 0; $x < $width; $x++) {
                    $rgb1 = imagecolorat($fromimage, $x + $offset1, $y);
                    $r1 = $rgb1 >> 16 & 0xff;
                    $g1 = $rgb1 >> 8 & 0xff;
                    $b1 = $rgb1 & 0xff;
                    $rgb2 = imagecolorat($fromimage, $x + $offset2, $y);
                    $r2 = $rgb2 >> 16 & 0xff;
                    $g2 = $rgb2 >> 8 & 0xff;
                    $b2 = $rgb2 & 0xff;
                    // Red - Cyan glass
                    if ($glass == 'redcyan') {
                        $r = $f299[$r2] + $f587[$g2] + $f114[$b2];
                        $g = $f299[$r1] + $f587[$g1] + $f114[$b1];
                        $b = $f299[$r1] + $f587[$g1] + $f114[$b1];
                        $newpix = $r << 16 | $g << 8 | $b;
                    } else {
                        $r = $f299[$r1] + $f587[$g1] + $f114[$b1];
                        $g = $f299[$r2] + $f587[$g2] + $f114[$b2];
                        $b = $f299[$r1] + $f587[$g1] + $f114[$b1];
                        $newpix = $r << 16 | $g << 8 | $b;
                    }
                    imagesetpixel($toimage, $x, $y, $newpix);
                }
            }
            imagejpeg($toimage, $tofile, wppa_opt('jpeg_quality'));
            break;
        case 'true':
            for ($y = 0; $y < $height; $y++) {
                for ($x = 0; $x < $width; $x++) {
                    $rgb1 = imagecolorat($fromimage, $x + $offset1, $y);
                    $r1 = $rgb1 >> 16 & 0xff;
                    $g1 = $rgb1 >> 8 & 0xff;
                    $b1 = $rgb1 & 0xff;
                    $rgb2 = imagecolorat($fromimage, $x + $offset2, $y);
                    $r2 = $rgb2 >> 16 & 0xff;
                    $g2 = $rgb2 >> 8 & 0xff;
                    $b2 = $rgb2 & 0xff;
                    // Red - Cyan glass
                    if ($glass == 'redcyan') {
                        $r = $f299[$r1] + $f587[$g1] + $f114[$b1];
                        $g = 0;
                        $b = $f299[$r2] + $f587[$g2] + $f114[$b2];
                        $newpix = $r << 16 | $g << 8 | $b;
                    } else {
                        $r = $f299[$r2] + $f587[$g2] + $f114[$b2];
                        $g = $f299[$r1] + $f587[$g1] + $f114[$b1];
                        $b = 0;
                        $newpix = $r << 16 | $g << 8 | $b;
                    }
                    imagesetpixel($toimage, $x, $y, $newpix);
                }
            }
            imagejpeg($toimage, $tofile, wppa_opt('jpeg_quality'));
            break;
        case 'halfcolor':
            for ($y = 0; $y < $height; $y++) {
                for ($x = 0; $x < $width; $x++) {
                    $rgb1 = imagecolorat($fromimage, $x + $offset1, $y);
                    $r1 = $rgb1 >> 16 & 0xff;
                    $g1 = $rgb1 >> 8 & 0xff;
                    $b1 = $rgb1 & 0xff;
                    $rgb2 = imagecolorat($fromimage, $x + $offset2, $y);
                    $r2 = $rgb2 >> 16 & 0xff;
                    $g2 = $rgb2 >> 8 & 0xff;
                    $b2 = $rgb2 & 0xff;
                    // Red - Cyan glass
                    if ($glass == 'redcyan') {
                        $r = $f299[$r1] + $f587[$g1] + $f114[$b1];
                        //	$g = $g2;
                        //	$b = $b2;
                        $newpix = $r << 16 | $g2 << 8 | $b2;
                    } else {
                        $r = $f299[$r2] + $f587[$g2] + $f114[$b2];
                        //						$g = $g1;
                        //						$b = $b2;
                        $r = ceil(min($r, 255));
                        $newpix = $r << 16 | $g1 << 8 | $b2;
                    }
                    imagesetpixel($toimage, $x, $y, $newpix);
                }
            }
            imagejpeg($toimage, $tofile, wppa_opt('jpeg_quality'));
            break;
        case 'optimized':
            for ($y = 0; $y < $height; $y++) {
                for ($x = 0; $x < $width; $x++) {
                    $rgb1 = imagecolorat($fromimage, $x + $offset1, $y);
                    $r1 = $rgb1 >> 16 & 0xff;
                    $g1 = $rgb1 >> 8 & 0xff;
                    $b1 = $rgb1 & 0xff;
                    $rgb2 = imagecolorat($fromimage, $x + $offset2, $y);
                    $r2 = $rgb2 >> 16 & 0xff;
                    $g2 = $rgb2 >> 8 & 0xff;
                    $b2 = $rgb2 & 0xff;
                    // Red - Cyan glass
                    if ($glass == 'redcyan') {
                        $r = 0.7 * $g1 + 0.3 * $b1;
                        $g = $g2;
                        $b = $b2;
                        $r = ceil(min($r, 255));
                        $newpix = $r << 16 | $g << 8 | $b;
                    } else {
                        $r = 0.7 * $g2 + 0.3 * $b2;
                        $g = $g1;
                        $b = $b2;
                        $r = ceil(min($r, 255));
                        $newpix = $r << 16 | $g << 8 | $b;
                    }
                    imagesetpixel($toimage, $x, $y, $newpix);
                }
            }
            imagejpeg($toimage, $tofile, wppa_opt('jpeg_quality'));
            break;
        case '_flat':
            imagecopy($toimage, $fromimage, 0, 0, 0, 0, $width, $height);
            imagejpeg($toimage, $tofile, wppa_opt('jpeg_quality'));
            break;
        default:
            break;
    }
    // accessable
    wppa_chmod($tofile);
    // Optimized
    wppa_optimize_image_file($tofile);
    // Bump version
    wppa_bump_photo_rev();
}