/**
  * Crops Image.
  *
  * @since 3.5.0
  * @access public
  *
  * @param string|int $src The source file or Attachment ID.
  * @param int $src_x The start x position to crop from.
  * @param int $src_y The start y position to crop from.
  * @param int $src_w The width to crop.
  * @param int $src_h The height to crop.
  * @param int $dst_w Optional. The destination width.
  * @param int $dst_h Optional. The destination height.
  * @param boolean $src_abs Optional. If the source crop points are absolute.
  * @return boolean|WP_Error
  */
 public function crop($src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false)
 {
     if (pte_is_crop_border_enabled($src_w, $src_h, $dst_w, $dst_h)) {
         // Crop the image to the correct aspect ratio...
         $ar = $src_w / $src_h;
         $dst_ar = $dst_w / $dst_h;
         if ($dst_ar > $ar) {
             // constrain to the dst_h
             $tmp_dst_h = $dst_h;
             $tmp_dst_w = $dst_h * $ar;
             $tmp_dst_y = 0;
             $tmp_dst_x = $dst_w / 2 - $tmp_dst_w / 2;
         } else {
             $tmp_dst_w = $dst_w;
             $tmp_dst_h = $dst_w / $ar;
             $tmp_dst_x = 0;
             $tmp_dst_y = $dst_h / 2 - $tmp_dst_h / 2;
         }
         // copy $this->image unto a new image with the right width/height.
         $img = wp_imagecreatetruecolor($dst_w, $dst_h);
         if (function_exists('imageantialias')) {
             imageantialias($img, true);
         }
         if (pte_is_crop_border_opaque()) {
             $c = self::getRgbFromHex($_GET['pte-fit-crop-color']);
             $color = imagecolorallocate($img, $c[0], $c[1], $c[2]);
         } else {
             PteLogger::debug("setting transparent/white");
             //$color = imagecolorallocate( $img, 100, 100, 100 );
             $color = imagecolorallocatealpha($img, 255, 255, 255, 127);
         }
         imagefilledrectangle($img, 0, 0, $dst_w, $dst_h, $color);
         imagecopyresampled($img, $this->image, $tmp_dst_x, $tmp_dst_y, $src_x, $src_y, $tmp_dst_w, $tmp_dst_h, $src_w, $src_h);
         if (is_resource($img)) {
             imagedestroy($this->image);
             $this->image = $img;
             $this->update_size();
             return true;
         }
         return new WP_Error('image_crop_error', __('Image crop failed.'), $this->file);
     }
     return parent::crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
 }
 /**
  * Crops Image.
  *
  * @since 3.5.0
  * @access public
  *
  * @param string|int $src The source file or Attachment ID.
  * @param int $src_x The start x position to crop from.
  * @param int $src_y The start y position to crop from.
  * @param int $src_w The width to crop.
  * @param int $src_h The height to crop.
  * @param int $dst_w Optional. The destination width.
  * @param int $dst_h Optional. The destination height.
  * @param boolean $src_abs Optional. If the source crop points are absolute.
  * @return boolean|WP_Error
  */
 public function crop($src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false)
 {
     if (pte_is_crop_border_enabled($src_w, $src_h, $dst_w, $dst_h)) {
         // Crop the image to the correct aspect ratio...
         $ar = $src_w / $src_h;
         $dst_ar = $dst_w / $dst_h;
         if ($dst_ar > $ar) {
             // constrain to the dst_h
             $tmp_dst_h = $dst_h;
             $tmp_dst_w = $dst_h * $ar;
             $tmp_dst_y = 0;
             $tmp_dst_x = $dst_w / 2 - $tmp_dst_w / 2;
         } else {
             $tmp_dst_w = $dst_w;
             $tmp_dst_h = $dst_w / $ar;
             $tmp_dst_x = 0;
             $tmp_dst_y = $dst_h / 2 - $tmp_dst_h / 2;
         }
         if (pte_is_crop_border_opaque()) {
             $color = new ImagickPixel($_GET['pte-fit-crop-color']);
         }
         try {
             // crop the original image
             $this->image->cropImage($src_w, $src_h, $src_x, $src_y);
             $this->image->scaleImage($tmp_dst_w, $tmp_dst_h);
             // Create a new image and then compose the old one onto it.
             $img = new Imagick();
             $img->newImage($dst_w, $dst_h, isset($color) ? $color : 'white');
             $img->setImageFormat($this->image->getImageFormat());
             if (!isset($color)) {
                 $img->setImageOpacity(0.0);
             }
             $img->compositeImage($this->image, Imagick::COMPOSITE_DEFAULT, $tmp_dst_x, $tmp_dst_y);
             $img->flattenImages();
             $this->image = $img;
         } catch (Exception $e) {
             return new WP_Error('image_crop_error', __('Image crop failed.'), $this->file);
         }
         return $this->update_size();
     }
     return parent::crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
 }
Esempio n. 3
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}")));
}