function wppa_recuperate($id)
{
    global $thumb;
    global $wpdb;
    wppa_cache_thumb($id);
    $iptcfix = false;
    $exiffix = false;
    $file = wppa_get_source_path($id);
    if (!is_file($file)) {
        $file = wppa_get_photo_path($id);
    }
    if (is_file($file)) {
        // Not a dir
        $attr = getimagesize($file, $info);
        if (is_array($attr)) {
            // Is a picturefile
            if ($attr[2] == IMAGETYPE_JPEG) {
                // Is a jpg
                if (wppa_switch('wppa_save_iptc')) {
                    // Save iptc
                    if (isset($info["APP13"])) {
                        // There is IPTC data
                        $is_iptc = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM `" . WPPA_IPTC . "` WHERE `photo` = %s", $id));
                        if (!$is_iptc) {
                            // No IPTC yet and there is: Recuperate
                            wppa_import_iptc($id, $info, 'nodelete');
                            $iptcfix = true;
                        }
                    }
                }
                if (wppa_switch('wppa_save_exif')) {
                    // Save exif
                    $image_type = exif_imagetype($file);
                    if ($image_type == IMAGETYPE_JPEG) {
                        // EXIF supported by server
                        $is_exif = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM `" . WPPA_EXIF . "` WHERE `photo`=%s", $id));
                        if (!$is_exif) {
                            // No EXIF yet
                            $exif = @exif_read_data($file, 'EXIF');
                            //@
                            if (is_array($exif)) {
                                // There is exif data present
                                wppa_import_exif($id, $file, 'nodelete');
                                $exiffix = true;
                            }
                        }
                    }
                }
            }
        }
    }
    return array('iptcfix' => $iptcfix, 'exiffix' => $exiffix);
}
function wppa_make_the_photo_files($file, $id, $ext)
{
    global $wpdb;
    $thumb = wppa_cache_thumb($id);
    $src_size = @getimagesize($file, $info);
    // If the given file is not an image file, log error and exit
    if (!$src_size) {
        if (is_admin()) {
            wppa_error_message(sprintf(__('ERROR: File %s is not a valid picture file.', 'wp-photo-album-plus'), $file));
        } else {
            wppa_alert(sprintf(__('ERROR: File %s is not a valid picture file.', 'wp-photo-album-plus'), $file));
        }
        return false;
    }
    // Find output path photo file
    $newimage = wppa_get_photo_path($id);
    if ($ext) {
        $newimage = wppa_strip_ext($newimage) . '.' . strtolower($ext);
    }
    // If Resize on upload is checked
    if (wppa_switch('resize_on_upload')) {
        // Picture sizes
        $src_width = $src_size[0];
        // Temp convert to logical width if stereo
        if ($thumb['stereo']) {
            $src_width /= 2;
        }
        $src_height = $src_size[1];
        // Max sizes
        if (wppa_opt('resize_to') == '0') {
            // from fullsize
            $max_width = wppa_opt('fullsize');
            $max_height = wppa_opt('maxheight');
        } else {
            // from selection
            $screen = explode('x', wppa_opt('resize_to'));
            $max_width = $screen[0];
            $max_height = $screen[1];
        }
        // If orientation needs +/- 90 deg rotation, swap max x and max y
        $ori = wppa_get_exif_orientation($file);
        if ($ori >= 5 && $ori <= 8) {
            $t = $max_width;
            $max_width = $max_height;
            $max_height = $t;
        }
        // Is source more landscape or more portrait than max window
        if ($src_width / $src_height > $max_width / $max_height) {
            // focus on width
            $focus = 'W';
            $need_downsize = $src_width > $max_width;
        } else {
            // focus on height
            $focus = 'H';
            $need_downsize = $src_height > $max_height;
        }
        // Convert back to physical size
        if ($thumb['stereo']) {
            $src_width *= 2;
        }
        // Downsize required ?
        if ($need_downsize) {
            // Find mime type
            $mime = $src_size[2];
            // Create the source image
            switch ($mime) {
                // mime type
                case 1:
                    // gif
                    $temp = @imagecreatefromgif($file);
                    if ($temp) {
                        $src = imagecreatetruecolor($src_width, $src_height);
                        imagecopy($src, $temp, 0, 0, 0, 0, $src_width, $src_height);
                        imagedestroy($temp);
                    } else {
                        $src = false;
                    }
                    break;
                case 2:
                    // jpeg
                    if (!function_exists('wppa_imagecreatefromjpeg')) {
                        wppa_log('Error', 'Function wppa_imagecreatefromjpeg does not exist.');
                    }
                    $src = @wppa_imagecreatefromjpeg($file);
                    break;
                case 3:
                    // png
                    $src = @imagecreatefrompng($file);
                    break;
            }
            if (!$src) {
                wppa_log('Error', 'Image file ' . $file . ' is corrupt while downsizing photo');
                return false;
            }
            // Create the ( empty ) destination image
            if ($focus == 'W') {
                if ($thumb['stereo']) {
                    $max_width *= 2;
                }
                $dst_width = $max_width;
                $dst_height = round($max_width * $src_height / $src_width);
            } else {
                $dst_height = $max_height;
                $dst_width = round($max_height * $src_width / $src_height);
            }
            $dst = imagecreatetruecolor($dst_width, $dst_height);
            // If Png, save transparancy
            if ($mime == 3) {
                imagealphablending($dst, false);
                imagesavealpha($dst, true);
            }
            // Do the copy
            imagecopyresampled($dst, $src, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
            // Remove source image
            imagedestroy($src);
            // Save the photo
            switch ($mime) {
                // mime type
                case 1:
                    imagegif($dst, $newimage);
                    break;
                case 2:
                    imagejpeg($dst, $newimage, wppa_opt('jpeg_quality'));
                    break;
                case 3:
                    imagepng($dst, $newimage, 6);
                    break;
            }
            // Remove destination image
            imagedestroy($dst);
        } else {
            // No downsize needed, picture is small enough
            copy($file, $newimage);
        }
    } else {
        copy($file, $newimage);
    }
    // File successfully created ?
    if (is_file($newimage)) {
        // Make sure file is accessable
        wppa_chmod($newimage);
        // Optimize file
        wppa_optimize_image_file($newimage);
    } else {
        if (is_admin()) {
            wppa_error_message(__('ERROR: Resized or copied image could not be created.', 'wp-photo-album-plus'));
        } else {
            wppa_alert(__('ERROR: Resized or copied image could not be created.', 'wp-photo-album-plus'));
        }
        return false;
    }
    // Process the iptc data
    wppa_import_iptc($id, $info);
    // Process the exif data
    wppa_import_exif($id, $file);
    // GPS
    wppa_get_coordinates($file, $id);
    // Set ( update ) exif date-time if available
    $exdt = wppa_get_exif_datetime($file);
    if ($exdt) {
        wppa_update_photo(array('id' => $id, 'exifdtm' => $exdt));
    }
    // Check orientation
    wppa_orientate_image($id, wppa_get_exif_orientation($file));
    // Compute and save sizes
    wppa_get_photox($id, 'force');
    // Show progression
    if (is_admin() && !wppa('ajax')) {
        echo '.';
    }
    // Update CDN
    $cdn = wppa_cdn('admin');
    if ($cdn) {
        switch ($cdn) {
            case 'cloudinary':
                wppa_upload_to_cloudinary($id);
                break;
            default:
                wppa_dbg_msg('Missing upload instructions for ' . $cdn, 'red', 'force');
        }
    }
    // Create stereo images
    wppa_create_stereo_images($id);
    // Create thumbnail...
    wppa_create_thumbnail($id);
    // Clear (super)cache
    wppa_clear_cache();
    return true;
}
function wppa_recuperate($id)
{
    global $wpdb;
    $thumb = wppa_cache_thumb($id);
    $iptcfix = false;
    $exiffix = false;
    $file = wppa_get_source_path($id);
    if (!is_file($file)) {
        $file = wppa_get_photo_path($id);
    }
    if (is_file($file)) {
        // Not a dir
        $attr = getimagesize($file, $info);
        if (is_array($attr)) {
            // Is a picturefile
            if ($attr[2] == IMAGETYPE_JPEG) {
                // Is a jpg
                // Save iptc is on?
                if (wppa_switch('save_iptc')) {
                    // There is IPTC data
                    if (isset($info["APP13"])) {
                        // If this is a cron prcess, the table is not pre-emptied
                        if (wppa_is_cron()) {
                            // Replace or add data
                            wppa_import_iptc($id, $info);
                        } else {
                            wppa_import_iptc($id, $info, 'nodelete');
                        }
                        $iptcfix = true;
                    }
                }
                // Save exif is on?
                if (wppa_switch('save_exif')) {
                    $image_type = exif_imagetype($file);
                    // EXIF supported by server
                    if ($image_type == IMAGETYPE_JPEG) {
                        // Get exif data
                        $exif = exif_read_data($file, 'EXIF');
                        // Exif data found
                        if ($exif) {
                            // If this is a cron prcess, the table is not pre-emptied
                            if (wppa_is_cron()) {
                                // Replace or add data
                                wppa_import_exif($id, $file);
                            } else {
                                wppa_import_exif($id, $file, 'nodelete');
                            }
                            $exiffix = true;
                        }
                    }
                }
            }
        }
    }
    return array('iptcfix' => $iptcfix, 'exiffix' => $exiffix);
}