Skip to content
This repository has been archived by the owner on Nov 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #9 from statnews/master
Browse files Browse the repository at this point in the history
* statnews-master:
  Note parameters for mic_crop_done.
  Note that  variable is passed to mic_do_crop.
  Remove references to v1.13-bgmp. The version can be incremented once the changes are back in the mainline version of the plugin.
  Update version number in plugin to make it clear this is the forked version.
  Update readme to indicate pull requests.
  Provide dimensions to mic_do_crop filter.
  Provide  to mic_do_crop filter.
  Update readme.txt to document the action
  Trigger an action when cropping is done
  Document filters (tomaszsita#37).
  Document filters (tomaszsita#37).
  Add filters (addresses tomaszsita#37).
  • Loading branch information
mcaskill committed Dec 6, 2016
2 parents 58891dc + 75fbc05 commit ea90863
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 51 deletions.
112 changes: 67 additions & 45 deletions lib/ManualImageCrop.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function addAfterUploadAttachementEditLink() {
if ($editAttachment.length) {
$editAttachment.each(function(i, k) {
try {
var mRegexp = /\?post=([0-9]+)/;
var mRegexp = /\?post=([0-9]+)/;
var match = mRegexp.exec(jQuery(this).attr('href'));
if (!jQuery(this).parent().find('.edit-attachment.crop-image').length && jQuery(this).parent().find('.pinkynail').attr('src').match(/upload/g)) {
jQuery(this).after( '<a class="thickbox mic-link edit-attachment crop-image" rel="crop" title="<?php _e("Manual Image Crop","microp"); ?>" href="' + ajaxurl + '?action=mic_editor_window&postId=' + match[1] + '"><?php _e('Crop Image','microp') ?></a>' );
Expand All @@ -148,12 +148,12 @@ public function addAfterUploadAttachementEditLink() {
</script>
<?php
}

private function filterPostData() {
$imageSizes = get_intermediate_image_sizes();

$data = array(
'attachmentId' => filter_var($_POST['attachmentId'], FILTER_SANITIZE_NUMBER_INT),
'attachmentId' => 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),
Expand All @@ -162,7 +162,7 @@ private function filterPostData() {
'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'])) {
Expand All @@ -178,6 +178,26 @@ private function filterPostData() {
return $data;
}

public function cropSuccess( $data, $dst_file_url, $silent_result = false ) {
// update 'mic_make2x' option status to persist choice
if ( isset( $data['make2x'] ) && $data['make2x'] !== get_option('mic_make2x') ) {
update_option('mic_make2x', $data['make2x']);
}

// trigger s3 sync
if ( is_plugin_active('amazon-s3-and-cloudfront/wordpress-s3.php') ) {
wp_update_attachment_metadata($data['attachmentId'], $imageMetadata);
}

//returns the url to the generated image (to allow refreshing the preview)
if ( $silent_result ) {
return;
} else {
echo json_encode(array('status' => 'ok', 'file' => $dst_file_url[0] ) );
exit;
}
}

/**
* Callback function for plugin: amazon-s3-and-cloudfront
*/
Expand All @@ -191,7 +211,7 @@ function get_attached_file_copy_back_to_local( $copy_back_to_local, $file, $atta

/**
* Crops the image based on params passed in $_POST array
*
*
* Optional parameter $data can be used by plugins to call this method using previous configurations.
*
* @param null $data
Expand All @@ -207,16 +227,19 @@ public function cropImage( $data = null, $silent_result = false ) {

if ( is_plugin_active('amazon-s3-and-cloudfront/wordpress-s3.php') ) {
add_filter( 'as3cf_get_attached_file_copy_back_to_local', array( $this, 'get_attached_file_copy_back_to_local' ), 10, 3 );

// This funciton is called to trigger the hook above
get_attached_file($data['attachmentId']);
}

$dst_file_url = wp_get_attachment_image_src($data['attachmentId'], $data['editedSize']);

if (!$dst_file_url) {
if ( $silent_result ) return;
else exit;
if ( $silent_result ) {
return;
} else {
exit;
}
}

update_post_meta( $data['attachmentId'], '_mic_resizesize-' . $data['editedSize'], $data );
Expand All @@ -241,7 +264,7 @@ public function cropImage( $data = null, $silent_result = false ) {
if ( ! $src_file_url ) {
if ( $silent_result ) {
return;
}else{
} else {
echo json_encode( array( 'status' => 'error', 'message' => 'wrong attachment' ) );
exit;
}
Expand All @@ -251,19 +274,22 @@ public function cropImage( $data = null, $silent_result = false ) {
$dst_file = str_replace( $uploadsDir['baseurl'], $uploadsDir['basedir'], $dst_file_url[0] );
}

$dst_file = apply_filters( 'mic_dst_file_path', $dst_file, $data );
$dst_file_url[0] = apply_filters( 'mic_dst_file_url', $dst_file_url[0], $data );

//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);

Expand All @@ -283,7 +309,7 @@ public function cropImage( $data = null, $silent_result = false ) {
if (!$dst_w || !$dst_h) {
if ( $silent_result) {
return;
}else{
} else {
echo json_encode (array('status' => 'error', 'message' => 'wrong dimensions' ) );
exit;
}
Expand Down Expand Up @@ -325,6 +351,13 @@ public function cropImage( $data = null, $silent_result = false ) {
);
wp_update_attachment_metadata($data['attachmentId'], $imageMetadata);

$dims = array( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
$do_crop = apply_filters( 'mic_do_crop', true, $imageMetadata, $dims );
if ( !$do_crop ) {
// Another plugin has already taken care of the cropping.
$this->cropSuccess( $data, $dst_file_url );
}

if ( function_exists('wp_get_image_editor') ) {

// get local file - possible improvement: change hooks, so one call is enough
Expand All @@ -343,15 +376,15 @@ public function cropImage( $data = null, $silent_result = false ) {
if ( is_wp_error( $saveStatus ) ) {
if ( $silent_result ) {
return;
}else{
} else {
echo json_encode( array( 'status' => 'error', 'message' => 'WP_ERROR: ' . $saveStatus->get_error_message() ) );
exit;
}
}
}else {
if ( $silent_result ) {
return;
}else{
} else {
echo json_encode (array('status' => 'error', 'message' => 'WP_ERROR: ' . $img->get_error_message() ) );
exit;
}
Expand All @@ -360,8 +393,8 @@ public function cropImage( $data = null, $silent_result = false ) {
//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 = imagecreatefromgif ($src_file);
} elseif ($ext =="png"){
$src_img = imagecreatefrompng($src_file);
} else {
$src_img = imagecreatefromjpeg($src_file);
Expand All @@ -370,7 +403,7 @@ public function cropImage( $data = null, $silent_result = false ) {
if ($src_img === false ) {
if ( $silent_result ) {
return;
}else{
} else {
echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: Cannot create image from the source file' ) );
exit;
}
Expand All @@ -382,16 +415,16 @@ public function cropImage( $data = null, $silent_result = false ) {
if ($resampleReturn === false ) {
if ( $silent_result ) {
return;
}else{
} else {
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 = imagegif ($dst_img, $dst_file);
} elseif ($ext =="png"){
$imageSaveReturn = imagepng($dst_img, $dst_file);
} else {
$imageSaveReturn = imagejpeg($dst_img, $dst_file, $quality);
Expand All @@ -400,15 +433,15 @@ public function cropImage( $data = null, $silent_result = false ) {
if ($imageSaveReturn === false ) {
if ( $silent_result ) {
return;
}else{
} else {
echo json_encode (array('status' => 'error', 'message' => 'PHP ERROR: imagejpeg/imagegif/imagepng' ) );
exit;
}
}
}

// Generate Retina Image
if( isset( $data['make2x'] ) && $data['make2x'] === 'true' ) {
if ( isset( $data['make2x'] ) && $data['make2x'] === 'true' ) {
$dst_w2x = $dst_w * 2;
$dst_h2x = $dst_h * 2;

Expand All @@ -426,7 +459,7 @@ public function cropImage( $data = null, $silent_result = false ) {
}else {
if ( $silent_result ) {
return;
}else{
} else {
echo json_encode (array('status' => 'error', 'message' => 'WP_ERROR: ' . $img->get_error_message() ) );
exit;
}
Expand All @@ -438,48 +471,37 @@ public function cropImage( $data = null, $silent_result = false ) {
if ($resampleReturn === false ) {
if ( $silent_result ) {
return;
}else{
} else {
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 = imagegif ($dst_img2x, $dst_file2x);
} elseif ($ext =="png"){
$imageSaveReturn = imagepng($dst_img2x, $dst_file2x);
} else {
$imageSaveReturn = imagejpeg($dst_img2x, $dst_file2x, $quality);
}

if ($imageSaveReturn === false ) {
if ( $silent_result ) {
return;
}else{
} else {
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']);
}

// trigger s3 sync
if ( is_plugin_active('amazon-s3-and-cloudfront/wordpress-s3.php') ) {
wp_update_attachment_metadata($data['attachmentId'], $imageMetadata);
}
// run an action that other scripts can hook into, letting them
// know that the cropping is done for the given image
do_action('mic_crop_done', $data, $imageMetadata);

//returns the url to the generated image (to allow refreshing the preview)
if ( $silent_result ) {
return;
}else{
echo json_encode (array('status' => 'ok', 'file' => $dst_file_url[0] ) );
exit;
}
$this->cropSuccess( $data, $dst_file_url, $silent_result );
}
}
32 changes: 26 additions & 6 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Plugin allows you to manually crop all the image sizes registered in your WordPr

== Description ==
Plugin allows you to manually crop all the image sizes registered in your WordPress theme (in particular featured image).
Simply click on the "Crop" link next to any image in your media library.
Simply click on the "Crop" link next to any image in your media library.
The "lightbox" style interface will be brought up and you are ready to go.
Whole cropping process is really intuitive and simple.

Expand Down Expand Up @@ -47,9 +47,29 @@ Please contact me if you want to add a translation (or submit a pull request on
* Activate the plugin through the 'Plugins' menu in WordPress

= Automatically: =
* Navigate to the 'Plugins' menu inside of the wordpress wp-admin dashboard, and select AD NEW
* Search for 'Manual Imag Crop', and click install
* When the plugin has been installed, Click 'Activate'
* Navigate to the 'Plugins' menu inside of the wordpress wp-admin dashboard, and select AD NEW
* Search for 'Manual Imag Crop', and click install
* When the plugin has been installed, Click 'Activate'

== Filters ==
The plugin includes filters that can be used by other plugins:

=mic_do_crop=
Provides $do_crop (bool), $metadata (array), and $dims (array). Returning false for $do_crop will prevent Manual Image Crop from cropping the image. $metadata contains the crop parameters, so another plugin can take over the actual cropping.

=mic_dst_file_path=
Provides $path (string) and $data (array). Manual Image Crop will write the new image to $path and save that path to the image metadata. $data contains the crop parameters that the user chose in WordPress admin.

=mic_dst_file_url=
Provides $url (string) and $data (array). Manual Image Crop will return $url in an AJAX response if the image crop is successful. $data contains the crop parameters that the user chose in WordPress admin.

The admin screen uses this URL to display the updated image. This URL is not stored with the image or used elsewhere in WordPress. wp_get_attachment_image_src is used instead to generate the image URL.

== Actions ==
The plugin includes actions that can be used by other plugins:

= mic_crop_done =
Triggered after a crop has been successfully completed, immediately before the JSON response is sent to the browser. Provides $data (array) and $imageMetadata (array).

== Changelog ==
= 1.12 =
Expand All @@ -65,7 +85,7 @@ Please contact me if you want to add a translation (or submit a pull request on

= 1.09 =
* Dutch translation added
* Better error handling
* Better error handling
* Fixed overwriting of previously saved crops
* Minor tweaks all around

Expand Down Expand Up @@ -104,4 +124,4 @@ Please contact me if you want to add a translation (or submit a pull request on
* Improved compatibility with other plugins using 'thickbox'

= 1.0 =
* Initial version
* Initial version

0 comments on commit ea90863

Please sign in to comment.