post_mime_type)) { $links['crop'] = '' . __('Crop','microp') . ''; } return $links; } /** * Adds link below "Remove featured image" in post editing form */ public function addCropFeatureImageEditorLink($content, $post) { $content .= '' . __('Crop featured image','microp') . ' '; return $content; } /** * Adds link in the ligthbox media library */ public function addAttachementEditLink() { ?> filter_var($_POST['attachmentId'], FILTER_SANITIZE_NUMBER_INT), 'editedSize' => in_array($_POST['editedSize'], $imageSizes) ? $_POST['editedSize'] : null, 'select' => array( 'x' => filter_var($_POST['select']['x'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION), 'y' => filter_var($_POST['select']['y'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION), 'w' => filter_var($_POST['select']['w'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION), 'h' => filter_var($_POST['select']['h'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION), ), 'previewScale' => filter_var($_POST['previewScale'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION) ); if (isset($_POST['mic_quality'])) { $data['mic_quality'] = filter_var($_POST['mic_quality'], FILTER_SANITIZE_NUMBER_INT); } else { $data['mic_quality'] = 60; } if (isset($_POST['make2x'])) { $data['make2x'] = filter_var($_POST['make2x'], FILTER_VALIDATE_BOOLEAN); } return $data; } /** * Crops the image based on params passed in $_POST array */ public function cropImage() { global $_wp_additional_image_sizes; $data = $this->filterPostData(); $dst_file_url = wp_get_attachment_image_src($data['attachmentId'], $data['editedSize']); if (!$dst_file_url) { exit; } $uploadsDir = wp_upload_dir(); // checks for ssl. wp_upload_dir does not handle ssl (ssl admin trips on this and subsequent ajax success to browser) if (is_ssl()) { $uploadsDir['baseurl'] = preg_replace('#^http://#i', 'https://', $uploadsDir['baseurl']); } if ( function_exists( '_load_image_to_edit_path' ) ) { // this function is consider as private, but it return proper image path. Notice it is in function_exists condition $src_file = _load_image_to_edit_path( $data['attachmentId'], 'full' ); $dst_file = _load_image_to_edit_path( $data['attachmentId'], $data['editedSize'] ); } else { $src_file_url = wp_get_attachment_image_src( $data['attachmentId'], 'full' ); if ( ! $src_file_url ) { echo json_encode( array( 'status' => 'error', 'message' => 'wrong attachment' ) ); exit; } $src_file = str_replace( $uploadsDir['baseurl'], $uploadsDir['basedir'], $src_file_url[0] ); $dst_file = str_replace( $uploadsDir['baseurl'], $uploadsDir['basedir'], $dst_file_url[0] ); } //checks if the destination image file is present (if it's not, we want to create a new file, as the WordPress returns the original image instead of specific one) if ($dst_file == $src_file) { $attachmentData = wp_generate_attachment_metadata( $data['attachmentId'], $dst_file ); //overwrite with previous values $prevAttachmentData = wp_get_attachment_metadata($data['attachmentId']); if (isset($prevAttachmentData['micSelectedArea'])) { $attachmentData['micSelectedArea'] = $prevAttachmentData['micSelectedArea']; } //saves new path to the image size in the database wp_update_attachment_metadata( $data['attachmentId'], $attachmentData ); //new destination file path - replaces original file name with the correct one $dst_file = str_replace( basename($attachmentData['file']), $attachmentData['sizes'][ $data['editedSize'] ]['file'], $dst_file); //retrieves the new url to file (needet to refresh the preview) $dst_file_url = wp_get_attachment_image_src($data['attachmentId'], $data['editedSize']); } //sets the destination image dimensions if (isset($_wp_additional_image_sizes[$data['editedSize']])) { $dst_w = min(intval($_wp_additional_image_sizes[$data['editedSize']]['width']), $data['select']['w'] * $data['previewScale']); $dst_h = min(intval($_wp_additional_image_sizes[$data['editedSize']]['height']), $data['select']['h'] * $data['previewScale']); } else { $dst_w = min(get_option($data['editedSize'].'_size_w'), $data['select']['w'] * $data['previewScale']); $dst_h = min(get_option($data['editedSize'].'_size_h'), $data['select']['h'] * $data['previewScale']); } if (!$dst_w || !$dst_h) { echo json_encode (array('status' => 'error', 'message' => 'wrong dimensions' ) ); exit; } //prepares coordinates that will be passed to cropping function $dst_x = 0; $dst_y = 0; $src_x = max(0, $data['select']['x']) * $data['previewScale']; $src_y = max(0, $data['select']['y']) * $data['previewScale']; $src_w = max(0, $data['select']['w']) * $data['previewScale']; $src_h = max(0, $data['select']['h']) * $data['previewScale']; $size = wp_get_image_editor( $src_file )->get_size(); $is_higher = ( $dst_h > $size["height"] ); $is_wider = ( $dst_w > $size["width"] ); if ( $is_higher || $is_wider ) { if ( $is_higher ) { $scale = $src_h / $size["height"]; } else { $scale = $src_w / $size["width"]; } $src_w = $src_w / $scale; $src_h = $src_h / $scale; $src_x = $src_x / $scale; $src_y = $src_y / $scale; } //saves the selected area $imageMetadata = wp_get_attachment_metadata($data['attachmentId']); $imageMetadata['micSelectedArea'][$data['editedSize']] = array( 'x' => $data['select']['x'], 'y' => $data['select']['y'], 'w' => $data['select']['w'], 'h' => $data['select']['h'], 'scale' => $data['previewScale'], ); wp_update_attachment_metadata($data['attachmentId'], $imageMetadata); if ( function_exists('wp_get_image_editor') ) { $img = wp_get_image_editor( $src_file ); if ( ! is_wp_error( $img ) ) { $img->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, false ); $img->set_quality( $data['mic_quality'] ); $saveStatus = $img->save( $dst_file ); if ( is_wp_error( $saveStatus ) ) { echo json_encode( array( 'status' => 'error', 'message' => 'WP_ERROR: ' . $saveStatus->get_error_message() ) ); exit; } }else { echo json_encode (array('status' => 'error', 'message' => 'WP_ERROR: ' . $img->get_error_message() ) ); exit; } } else { //determines what's the image format $ext = pathinfo($src_file, PATHINFO_EXTENSION); if ($ext == "gif"){ $src_img = imagecreatefromgif($src_file); } else if($ext =="png"){ $src_img = imagecreatefrompng($src_file); } else { $src_img = imagecreatefromjpeg($src_file); } if ($src_img === false ) { echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: Cannot create image from the source file' ) ); exit; } $dst_img = imagecreatetruecolor($dst_w, $dst_h); $resampleReturn = imagecopyresampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); if ($resampleReturn === false ) { echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: imagecopyresampled' ) ); exit; } $imageSaveReturn = true; if ($ext == "gif"){ $imageSaveReturn = imagegif($dst_img, $dst_file); } else if($ext =="png"){ $imageSaveReturn = imagepng($dst_img, $dst_file); } else { $imageSaveReturn = imagejpeg($dst_img, $dst_file, $quality); } if ($imageSaveReturn === false ) { echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: imagejpeg/imagegif/imagepng' ) ); exit; } } // Generate Retina Image if( isset( $data['make2x'] ) && $data['make2x'] === 'true' ) { $dst_w2x = $dst_w * 2; $dst_h2x = $dst_h * 2; $dot = strrpos($dst_file,"."); $dst_file2x = substr($dst_file,0,$dot).'@2x'.substr($dst_file,$dot); // Check image size and create the retina file if possible if ( $src_w > $dst_w2x && $src_h > $dst_h2x) { if ( function_exists('wp_get_image_editor') ) { $img = wp_get_image_editor( $src_file ); if ( ! is_wp_error( $img ) ) { $img->crop( $src_x, $src_y, $src_w, $src_h, $dst_w2x, $dst_h2x, false ); $img->set_quality( $quality ); $img->save($dst_file2x); }else { echo json_encode (array('status' => 'error', 'message' => 'WP_ERROR: ' . $img->get_error_message() ) ); exit; } } else { $dst_img2x = imagecreatetruecolor($dst_w2x, $dst_h2x); $resampleReturn = imagecopyresampled($dst_img2x, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w2x, $dst_h2x, $src_w, $src_h); if ($resampleReturn === false ) { echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: imagecopyresampled' ) ); exit; } $imageSaveReturn = true; if ($ext == "gif"){ $imageSaveReturn = imagegif($dst_img2x, $dst_file2x); } else if($ext =="png"){ $imageSaveReturn = imagepng($dst_img2x, $dst_file2x); } else { $imageSaveReturn = imagejpeg($dst_img2x, $dst_file2x, $quality); } if ($imageSaveReturn === false ) { echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: imagejpeg/imagegif/imagepng' ) ); exit; } } } } // update 'mic_make2x' option status to persist choice if( isset( $data['make2x'] ) && $data['make2x'] !== get_option('mic_make2x') ) { update_option('mic_make2x', $data['make2x']); } //returns the url to the generated image (to allow refreshing the preview) echo json_encode (array('status' => 'ok', 'file' => $dst_file_url[0] ) ); exit; } }