/** * 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) { $ar = $src_w / $src_h; $dst_ar = $dst_w / $dst_h; if (isset($_GET['pte-fit-crop-color']) && abs($ar - $dst_ar) > 0.01) { PteLogger::debug(sprintf("IMAGICK - AR: '%f'\tOAR: '%f'", $ar, $dst_ar)); // Crop the image to the correct aspect ratio... 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; } //$color = this::getImagickPixel( $_GET['pte-fit-crop-color'] ); if (preg_match("/^#[a-fA-F0-9]{6}\$/", $_GET['pte-fit-crop-color'])) { $color = new ImagickPixel($_GET['pte-fit-crop-color']); } //else { // PteLogger::debug( "setting transparent/white" ); // $color = new ImagickPixel( 'white' ); // //$color->setColorValue( Imagick::COLOR_ALPHA, 0 ); //} 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); }
/** * 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) { $ar = $src_w / $src_h; $dst_ar = $dst_w / $dst_h; if (isset($_GET['pte-fit-crop-color']) && abs($ar - $dst_ar) > 0.01) { PteLogger::debug(sprintf("AR: '%f'\tOAR: '%f'", $ar, $dst_ar)); // Crop the image to the correct aspect ratio... 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 (preg_match("/^#[a-fA-F0-9]{6}\$/", $_GET['pte-fit-crop-color'])) { $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); }
function pte_get_jpeg_quality($quality) { $options = pte_get_options(); $jpeg_compression = $options['pte_jpeg_compression']; if (isset($_GET['pte-jpeg-compression'])) { $tmp_jpeg = intval($_GET['pte-jpeg-compression']); if (0 <= $tmp_jpeg && $tmp_jpeg <= 100) { $jpeg_compression = $tmp_jpeg; } } PteLogger::debug("COMPRESSION: " . $jpeg_compression); return $jpeg_compression; }
function pte_body($id) { ob_start(); $logger = PteLogger::singleton(); $options = pte_get_options(); // Get the information needed for image preview // (See wp-admin/includes/image-edit.php) $nonce = wp_create_nonce("image_editor-{$id}"); $meta = wp_get_attachment_metadata($id, true); if (is_array($meta) && isset($meta['width'])) { $big = max($meta['width'], $meta['height']); } else { $logger->error(sprintf(__("Invalid meta data for POST #%d: %s"), $id, print_r($meta, true))); $logger->error(__("Please contact support", PTE_DOMAIN)); } $sizer = $big > 400 ? 400 / $big : 1; $sizer = sprintf("%.8F", $sizer); PteLogger::debug("PTE-VERSION: " . PTE_VERSION . "\nUSER-AGENT: " . $_SERVER['HTTP_USER_AGENT'] . "\nWORDPRESS: " . $GLOBALS['wp_version']); $script_url = PTE_PLUGINURL . 'php/load-scripts.php?load=jquery,imgareaselect,jquery-json,pte'; $style_url = PTE_PLUGINURL . 'php/load-styles.php?load=imgareaselect,pte'; if ($options['pte_debug']) { $style_url .= "&d=1"; $script_url .= "&d=1"; } require PTE_PLUGINPATH . "html/pte.php"; return ob_get_clean(); }
function pte_ajax() { // Move all adjuntant functions to a separate file and include that here require_once PTE_PLUGINPATH . 'php/functions.php'; PteLogger::debug("PARAMETERS: " . print_r($_REQUEST, true)); switch ($_GET['pte-action']) { case "iframe": pte_init_iframe(); break; case "resize-images": pte_resize_images(); break; case "confirm-images": pte_confirm_images(); break; case "delete-images": pte_delete_images(); break; case "get-thumbnail-info": $id = (int) $_GET['id']; if (pte_check_id($id)) { print json_encode(pte_get_all_alternate_size_information($id)); } break; case "change-options": pte_update_user_options(); break; } die; }