Example #1
0
function wr2x_generate_images($meta)
{
    require 'wr2x_vt_resize.php';
    global $_wp_additional_image_sizes;
    $sizes = wr2x_get_image_sizes();
    if (!isset($meta['file'])) {
        return;
    }
    $originalfile = $meta['file'];
    $uploads = wp_upload_dir();
    $pathinfo = pathinfo($originalfile);
    $original_basename = $pathinfo['basename'];
    $basepath = trailingslashit($uploads['basedir']) . $pathinfo['dirname'];
    $ignore = wr2x_getoption("ignore_sizes", "wr2x_basics", array());
    $issue = false;
    $id = wr2x_get_attachment_id($meta['file']);
    wr2x_log("* GENERATE RETINA FOR ATTACHMENT '{$meta['file']}'");
    wr2x_log("Full-Size is {$original_basename}.");
    foreach ($sizes as $name => $attr) {
        $normal_file = "";
        if (in_array($name, $ignore)) {
            wr2x_log("Retina for {$name} ignored (settings).");
            continue;
        }
        // Is the file related to this size there?
        $pathinfo = null;
        $retina_file = null;
        if (isset($meta['sizes'][$name]) && isset($meta['sizes'][$name]['file'])) {
            $normal_file = trailingslashit($basepath) . $meta['sizes'][$name]['file'];
            $pathinfo = pathinfo($normal_file);
            $retina_file = trailingslashit($pathinfo['dirname']) . $pathinfo['filename'] . wr2x_retina_extension() . $pathinfo['extension'];
        }
        if ($retina_file && file_exists($retina_file)) {
            wr2x_log("Base for {$name} is '{$normal_file}'.");
            wr2x_log("Retina for {$name} already exists: '{$retina_file}'.");
            continue;
        }
        if ($retina_file) {
            $originalfile = trailingslashit($pathinfo['dirname']) . $original_basename;
            if (!file_exists($originalfile)) {
                wr2x_log("[ERROR] Original file '{$originalfile}' cannot be found.");
                return $meta;
            }
            // Maybe that new image is exactly the size of the original image.
            // In that case, let's make a copy of it.
            if ($meta['sizes'][$name]['width'] * 2 == $meta['width'] && $meta['sizes'][$name]['height'] * 2 == $meta['height']) {
                copy($originalfile, $retina_file);
                wr2x_log("Retina for {$name} created: '{$retina_file}' (as a copy of the full-size).");
            } else {
                if (wr2x_are_dimensions_ok($meta['width'], $meta['height'], $meta['sizes'][$name]['width'] * 2, $meta['sizes'][$name]['height'] * 2)) {
                    // Change proposed by Nicscott01, slighlty modified by Jordy (+isset)
                    // (https://wordpress.org/support/topic/issue-with-crop-position?replies=4#post-6200271)
                    $crop = isset($_wp_additional_image_sizes[$name]) ? $_wp_additional_image_sizes[$name]['crop'] : true;
                    $customCrop = null;
                    // Support for Manual Image Crop
                    // If the size of the image was manually cropped, let's keep it.
                    if (class_exists('ManualImageCrop') && isset($meta['micSelectedArea']) && isset($meta['micSelectedArea'][$name]) && isset($meta['micSelectedArea'][$name]['scale'])) {
                        $customCrop = $meta['micSelectedArea'][$name];
                    }
                    $image = wr2x_vt_resize($originalfile, $meta['sizes'][$name]['width'] * 2, $meta['sizes'][$name]['height'] * 2, $crop, $retina_file, $customCrop);
                }
            }
            if (!file_exists($retina_file)) {
                wr2x_log("[ERROR] Retina for {$name} could not be created. Full-Size is " . $meta['width'] . "x" . $meta['height'] . " but Retina requires a file of at least " . $meta['sizes'][$name]['width'] * 2 . "x" . $meta['sizes'][$name]['height'] * 2 . ".");
                $issue = true;
            } else {
                do_action('wr2x_retina_file_added', $id, $retina_file);
                wr2x_log("Retina for {$name} created: '{$retina_file}'.");
            }
        } else {
            if (empty($normal_file)) {
                wr2x_log("[ERROR] Base file for '{$name}' does not exist.");
            } else {
                wr2x_log("[ERROR] Base file for '{$name}' cannot be found here: '{$normal_file}'.");
            }
        }
    }
    // Checks attachment ID + issues
    if (!$id) {
        return $meta;
    }
    if ($issue) {
        wr2x_add_issue($id);
    } else {
        wr2x_remove_issue($id);
    }
    return $meta;
}
function wr2x_generate_images($meta)
{
    require 'wr2x_vt_resize.php';
    $sizes = wr2x_get_image_sizes();
    $originalfile = $meta['file'];
    $uploads = wp_upload_dir();
    $pathinfo = pathinfo($originalfile);
    $original_basename = $pathinfo['basename'];
    $basepath = trailingslashit($uploads['basedir']) . $pathinfo['dirname'];
    $ignore = wr2x_getoption("ignore_sizes", "wr2x_basics", array());
    $issue = false;
    $id = wr2x_get_attachment_id($meta['file']);
    wr2x_log("** RETINA INFO FOR ATTACHMENT '{$meta['file']}' **");
    wr2x_log("- Original: {$original_basename}");
    foreach ($sizes as $name => $attr) {
        if (in_array($name, $ignore)) {
            wr2x_log("- {$name} => IGNORED");
            continue;
        }
        // Is the file related to this size there?
        $pathinfo = null;
        $retina_file = null;
        if (isset($meta['sizes'][$name]) && isset($meta['sizes'][$name]['file'])) {
            $normal_file = trailingslashit($basepath) . $meta['sizes'][$name]['file'];
            $pathinfo = pathinfo($normal_file);
            $retina_file = trailingslashit($pathinfo['dirname']) . $pathinfo['filename'] . wr2x_retina_extension() . $pathinfo['extension'];
        }
        if ($retina_file && file_exists($retina_file)) {
            wr2x_log("- {$name}: {$normal_file} -> {$retina_file} => EXISTS");
            continue;
        }
        if ($retina_file) {
            $originalfile = trailingslashit($pathinfo['dirname']) . $original_basename;
            if (!file_exists($originalfile)) {
                wr2x_log("- The Original File '{$originalfile}' cannot be found.");
                return $meta;
            }
            // Maybe that new image is exactly the size of the original image.
            // In that case, let's make a copy of it.
            if ($meta['sizes'][$name]['width'] * 2 == $meta['width'] && $meta['sizes'][$name]['height'] * 2 == $meta['height']) {
                wr2x_log("- {$name}: {$originalfile} -> {$retina_file} => COPY");
                copy($originalfile, $retina_file);
            } else {
                if ($meta['sizes'][$name]['width'] * 2 <= $meta['width'] && $meta['sizes'][$name]['height'] * 2 <= $meta['height']) {
                    $image = wr2x_vt_resize($originalfile, $meta['sizes'][$name]['width'] * 2, $meta['sizes'][$name]['height'] * 2, $retina_file);
                }
            }
            if (!file_exists($retina_file)) {
                wr2x_log("- {$name}: {$normal_file} -> {$retina_file} => FAIL");
                $issue = true;
            } else {
                do_action('wr2x_retina_file_added', $id, $retina_file);
                wr2x_log("- {$name}: {$normal_file} -> {$retina_file} => RESIZE");
            }
        } else {
            wr2x_log("- {$name} => MISSING");
        }
    }
    // Checks attachment ID + issues
    if (!$id) {
        return $meta;
    }
    if ($issue) {
        wr2x_add_issue($id);
    } else {
        wr2x_remove_issue($id);
    }
    return $meta;
}