/**
 *
 * SUPPORT FOR WP 4.4
 *
 */
function wr2x_wp_calculate_image_srcset($srcset, $size)
{
    if (wr2x_getoption("disable_responsive", "wr2x_basics", false)) {
        return null;
    }
    $method = wr2x_getoption("method", "wr2x_advanced", 'Picturefill');
    if ($method == "none") {
        return $srcset;
    }
    $count = 0;
    $total = 0;
    $retinized_srcset = $srcset;
    if (empty($srcset)) {
        return $srcset;
    }
    foreach ($srcset as $s => $cfg) {
        $total++;
        $retina = wr2x_get_retina_from_url($cfg['url']);
        if (!empty($retina)) {
            $count++;
            $retinized_srcset[(int) $s * 2] = array('url' => $retina, 'descriptor' => 'w', 'value' => (int) $s * 2);
        }
    }
    wr2x_log("WP's srcset: " . $count . " retina files added out of " . $total . " image sizes");
    return $retinized_srcset;
}
 function wr2x_vt_resize($file_path, $width, $height, $crop, $newfile, $customCrop = false)
 {
     $crop_params = $crop == '1' ? true : $crop;
     $orig_size = getimagesize($file_path);
     $image_src[0] = $file_path;
     $image_src[1] = $orig_size[0];
     $image_src[2] = $orig_size[1];
     $file_info = pathinfo($file_path);
     $newfile_info = pathinfo($newfile);
     $extension = '.' . $newfile_info['extension'];
     $no_ext_path = $file_info['dirname'] . '/' . $file_info['filename'];
     $cropped_img_path = $no_ext_path . '-' . $width . 'x' . $height . "-tmp" . $extension;
     $image = wp_get_image_editor($file_path);
     if (is_wp_error($image)) {
         wr2x_log("Resize failure: " . $image->get_error_message());
         error_log("Resize failure: " . $image->get_error_message());
         return null;
     }
     // Resize or use Custom Crop
     if (!$customCrop) {
         $image->resize($width, $height, $crop_params);
     } else {
         $image->crop($customCrop['x'] * $customCrop['scale'], $customCrop['y'] * $customCrop['scale'], $customCrop['w'] * $customCrop['scale'], $customCrop['h'] * $customCrop['scale'], $width, $height, false);
     }
     $quality = wr2x_getoption("image_quality", "wr2x_advanced", "80");
     if (is_numeric($quality)) {
         $image->set_quality(intval($quality));
     }
     $saved = $image->save($cropped_img_path);
     if (rename($saved['path'], $newfile)) {
         $cropped_img_path = $newfile;
     } else {
         trigger_error("Retina: Could not move " . $saved['path'] . " to " . $newfile . ".", E_WARNING);
         error_log("Retina: Could not move " . $saved['path'] . " to " . $newfile . ".");
         return null;
     }
     $new_img_size = getimagesize($cropped_img_path);
     $new_img = str_replace(basename($image_src[0]), basename($cropped_img_path), $image_src[0]);
     $vt_image = array('url' => $new_img, 'width' => $new_img_size[0], 'height' => $new_img_size[1]);
     return $vt_image;
 }
Beispiel #3
0
function wr2x_delete_images($meta)
{
    if (!wr2x_is_image_meta($meta)) {
        return $meta;
    }
    $sizes = $meta['sizes'];
    if (!$sizes || !is_array($sizes)) {
        return $meta;
    }
    wr2x_log("* DELETE RETINA FOR ATTACHMENT '{$meta['file']}'");
    $originalfile = $meta['file'];
    $id = wr2x_get_attachment_id($originalfile);
    $pathinfo = pathinfo($originalfile);
    $uploads = wp_upload_dir();
    $basepath = trailingslashit($uploads['basedir']) . $pathinfo['dirname'];
    foreach ($sizes as $name => $attr) {
        $pathinfo = pathinfo($attr['file']);
        $retina_file = $pathinfo['filename'] . wr2x_retina_extension() . $pathinfo['extension'];
        if (file_exists(trailingslashit($basepath) . $retina_file)) {
            $fullpath = trailingslashit($basepath) . $retina_file;
            unlink($fullpath);
            do_action('wr2x_retina_file_removed', $id, $retina_file);
            wr2x_log("Deleted '{$fullpath}'.");
        }
    }
    // Remove full-size if there is any
    $pathinfo = pathinfo($originalfile);
    $retina_file = $pathinfo['filename'] . wr2x_retina_extension() . $pathinfo['extension'];
    if (file_exists(trailingslashit($basepath) . $retina_file)) {
        $fullpath = trailingslashit($basepath) . $retina_file;
        unlink($fullpath);
        do_action('wr2x_retina_file_removed', $id, $retina_file);
        wr2x_log("Deleted '{$fullpath}'.");
    }
    return $meta;
}
Beispiel #4
0
function wr2x_check_get_ajax_uploaded_file()
{
    if (!current_user_can('upload_files')) {
        echo json_encode(array('success' => false, 'message' => __("You do not have permission to upload files.", 'wp-retina-2x')));
        die;
    }
    $data = $_POST['data'];
    // Create the file as a TMP
    if (is_writable(sys_get_temp_dir())) {
        $tmpfname = tempnam(sys_get_temp_dir(), "wpx_");
        wr2x_log("Upload a temporary file in {$tmpfname} (sys_get_temp_dir).");
    } else {
        if (is_writable(wr2x_get_upload_root())) {
            if (!file_exists(trailingslashit(wr2x_get_upload_root()) . "wr2x-tmp")) {
                mkdir(trailingslashit(wr2x_get_upload_root()) . "wr2x-tmp");
            }
            $tmpfname = tempnam(trailingslashit(wr2x_get_upload_root()) . "wr2x-tmp", "wpx_");
            wr2x_log("Upload a temporary file in {$tmpfname} (wr2x_get_upload_root).");
        }
    }
    if ($tmpfname == null || $tmpfname == FALSE) {
        $tmpdir = get_temp_dir();
        error_log("Retina: The temporary directory could not be created.");
        wr2x_log("The temporary directory could not be created.");
        echo json_encode(array('success' => false, 'message' => __("The temporary directory could not be created.", 'wp-retina-2x')));
        die;
    }
    $handle = fopen($tmpfname, "w");
    fwrite($handle, base64_decode($data));
    fclose($handle);
    chmod($tmpfname, 0664);
    // Check if it is an image
    $file_info = getimagesize($tmpfname);
    if (empty($file_info)) {
        wr2x_log("The file is not an image or the upload went wrong.");
        unlink($tmpfname);
        echo json_encode(array('success' => false, 'message' => __("The file is not an image or the upload went wrong.", 'wp-retina-2x')));
        die;
    }
    $filedata = wp_check_filetype_and_ext($tmpfname, $_POST['filename']);
    if ($filedata["ext"] == "") {
        wr2x_log("You cannot use this file (wrong extension? wrong type?).");
        unlink($current_file);
        echo json_encode(array('success' => false, 'message' => __("You cannot use this file (wrong extension? wrong type?).", 'wp-retina-2x')));
        die;
    }
    wr2x_log("The temporary file was written successfully.");
    return $tmpfname;
}
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;
}
Beispiel #6
0
function wr2x_check_get_ajax_uploaded_file()
{
    if (!current_user_can('upload_files')) {
        echo json_encode(array('success' => false, 'message' => __("You do not have permission to upload files.", 'wp-retina-2x')));
        die;
    }
    $tmpfname = $_FILES['file']['tmp_name'];
    // Check if it is an image
    $file_info = getimagesize($tmpfname);
    if (empty($file_info)) {
        wr2x_log("The file is not an image or the upload went wrong.");
        unlink($tmpfname);
        echo json_encode(array('success' => false, 'message' => __("The file is not an image or the upload went wrong.", 'wp-retina-2x')));
        die;
    }
    $filedata = wp_check_filetype_and_ext($tmpfname, $_POST['filename']);
    if ($filedata["ext"] == "") {
        wr2x_log("You cannot use this file (wrong extension? wrong type?).");
        unlink($current_file);
        echo json_encode(array('success' => false, 'message' => __("You cannot use this file (wrong extension? wrong type?).", 'wp-retina-2x')));
        die;
    }
    wr2x_log("The temporary file was written successfully.");
    return $tmpfname;
}