Exemplo n.º 1
0
function pte_resize_images()
{
    $logger = PteLogger::singleton();
    global $pte_sizes;
    // Require JSON output
    pte_require_json();
    $id = intval($_GET['id']);
    $w = pte_check_int($_GET['w']);
    $h = pte_check_int($_GET['h']);
    $x = pte_check_int($_GET['x']);
    $y = pte_check_int($_GET['y']);
    $save = isset($_GET['save']) && strtolower($_GET['save']) === "true";
    if (pte_check_id($id) === false || $w === false || $h === false || $x === false || $y === false) {
        return pte_json_error("ResizeImages initialization failed: '{$id}-{$w}-{$h}-{$x}-{$y}'");
    }
    // Check nonce
    if (!check_ajax_referer("pte-resize-{$id}", 'pte-nonce', false)) {
        return pte_json_error("CSRF Check failed");
    }
    // Get the sizes to process
    $pte_sizes = $_GET['pte-sizes'];
    if (!is_array($pte_sizes)) {
        $logger->debug("Converting pte_sizes to array");
        $pte_sizes = explode(",", $pte_sizes);
    }
    $sizes = pte_get_all_alternate_size_information($id);
    // The following information is common to all sizes
    // *** common-info
    $original_file = _load_image_to_edit_path($id);
    $original_size = @getimagesize($original_file);
    // SETS $PTE_TMP_DIR and $PTE_TMP_URL
    extract(pte_tmp_dir());
    $thumbnails = array();
    if (!$original_size) {
        return pte_json_error("Could not read image size");
    }
    $logger->debug("BASE FILE DIMENSIONS/INFO: " . print_r($original_size, true));
    list($orig_w, $orig_h, $orig_type) = $original_size;
    // *** End common-info
    // So this never interrupts the jpeg_quality anywhere else
    add_filter('jpeg_quality', 'pte_get_jpeg_quality');
    add_filter('wp_editor_set_quality', 'pte_get_jpeg_quality');
    foreach ($sizes as $size => $data) {
        // Get all the data needed to run image_create
        //
        //	$dst_w, $dst_h
        extract(pte_get_width_height($data, $w, $h));
        $logger->debug("WIDTHxHEIGHT: {$dst_w} x {$dst_h}");
        // Set the cropped filename
        $transparent = pte_is_crop_border_enabled($w, $h, $dst_w, $dst_h) && !pte_is_crop_border_opaque();
        $basename = pte_generate_filename($original_file, $dst_w, $dst_h, $transparent);
        $tmpfile = "{$PTE_TMP_DIR}{$id}" . DIRECTORY_SEPARATOR . "{$basename}";
        // === CREATE IMAGE ===================
        // This function is in wp-includes/media.php
        // We've added a filter to return our own editor which extends the wordpress one.
        add_filter('wp_image_editors', 'pte_image_editors');
        $editor = wp_get_image_editor($original_file);
        if (is_a($editor, "WP_Image_Editor_Imagick")) {
            $logger->debug("EDITOR: ImageMagick");
        }
        if (is_a($editor, "WP_Image_Editor_GD")) {
            $logger->debug("EDITOR: GD");
        }
        $crop_results = $editor->crop($x, $y, $w, $h, $dst_w, $dst_h);
        if (is_wp_error($crop_results)) {
            $logger->error("Error creating image: {$size}");
            continue;
        }
        // The directory containing the original file may no longer exist when
        // using a replication plugin.
        wp_mkdir_p(dirname($tmpfile));
        $tmpfile = dirname($tmpfile) . '/' . wp_unique_filename(dirname($tmpfile), basename($tmpfile));
        $tmpurl = "{$PTE_TMP_URL}{$id}/" . basename($tmpfile);
        if (is_wp_error($editor->save($tmpfile))) {
            $logger->error("Error writing image: {$size} to '{$tmpfile}'");
            continue;
        }
        // === END CREATE IMAGE ===============
        // URL: wp_upload_dir => base_url/subdir + /basename of $tmpfile
        // This is for the output
        $thumbnails[$size]['url'] = $tmpurl;
        $thumbnails[$size]['file'] = basename($tmpfile);
    }
    // Did you process anything?
    if (count($thumbnails) < 1) {
        return pte_json_error("No images processed");
    }
    $ptenonce = wp_create_nonce("pte-{$id}");
    // If save -- return pte_confirm_images
    if ($save) {
        function create_pte_confirm($thumbnail)
        {
            return $thumbnail['file'];
        }
        $_REQUEST['pte-nonce'] = $ptenonce;
        $_GET['pte-confirm'] = array_map('create_pte_confirm', $thumbnails);
        $logger->debug("CONFIRM:");
        $logger->debug(print_r($_GET, true));
        return pte_confirm_images(true);
    }
    return pte_json_encode(array('thumbnails' => $thumbnails, 'pte-nonce' => $ptenonce, 'pte-delete-nonce' => wp_create_nonce("pte-delete-{$id}")));
}
Exemplo n.º 2
0
function pte_resize_images()
{
    $logger = PteLogger::singleton();
    global $pte_sizes;
    // Require JSON output
    pte_require_json();
    $id = pte_check_id($_GET['id']);
    $w = pte_check_int($_GET['w']);
    $h = pte_check_int($_GET['h']);
    $x = pte_check_int($_GET['x']);
    $y = pte_check_int($_GET['y']);
    if ($id === false || $w === false || $h === false || $x === false || $y === false) {
        return pte_json_error("ResizeImages initialization failed: '{$id}-{$w}-{$h}-{$x}-{$y}'");
    }
    // Get the sizes to process
    $pte_sizes = $_GET['pte-sizes'];
    $sizes = pte_get_all_alternate_size_information($id);
    // The following information is common to all sizes
    // *** common-info
    $dst_x = 0;
    $dst_y = 0;
    $original_file = get_attached_file($id);
    $original_image = wp_load_image($original_file);
    $original_size = @getimagesize($original_file);
    $uploads = wp_upload_dir();
    $PTE_TMP_DIR = $uploads['basedir'] . DIRECTORY_SEPARATOR . "ptetmp" . DIRECTORY_SEPARATOR;
    $PTE_TMP_URL = $uploads['baseurl'] . "/ptetmp/";
    if (!$original_size) {
        return pte_json_error("Could not read image size");
    }
    $logger->debug("BASE FILE DIMENSIONS/INFO: " . print_r($original_size, true));
    list($orig_w, $orig_h, $orig_type) = $original_size;
    // *** End common-info
    foreach ($sizes as $size => $data) {
        // Get all the data needed to run image_create
        //
        //	$dst_w, $dst_h
        extract(pte_get_width_height($data, $w, $h));
        //
        // Set the directory
        $basename = pte_generate_filename($original_file, $dst_w, $dst_h);
        // Set the file and URL's - defines set in pte_ajax
        $tmpfile = "{$PTE_TMP_DIR}{$id}" . DIRECTORY_SEPARATOR . "{$basename}";
        $tmpurl = "{$PTE_TMP_URL}{$id}/{$basename}";
        // === CREATE IMAGE ===================
        $image = pte_create_image($original_image, $orig_type, $dst_x, $dst_y, $dst_w, $dst_h, $x, $y, $w, $h);
        if (!isset($image)) {
            $logger->error("Error creating image: {$size}");
            continue;
        }
        if (!pte_write_image($image, $orig_type, $tmpfile)) {
            $logger->error("Error writing image: {$size} to '{$tmpfile}'");
            continue;
        }
        // === END CREATE IMAGE ===============
        // URL: wp_upload_dir => base_url/subdir + /basename of $tmpfile
        // This is for the output
        $thumbnails[$size]['url'] = $tmpurl;
        $thumbnails[$size]['file'] = basename($tmpfile);
    }
    // we don't need the original in memory anymore
    imagedestroy($original_image);
    // Did you process anything?
    if (count($thumbnails) < 1) {
        return pte_json_error("No images processed");
    }
    return pte_json_encode(array('thumbnails' => $thumbnails, 'pte-nonce' => wp_create_nonce("pte-{$id}"), 'pte-delete-nonce' => wp_create_nonce("pte-delete-{$id}")));
}