Skip to content

Commit

Permalink
Merge pull request tomaszsita#5 from noreabu/wp-amazon-s3-and-cloudfront
Browse files Browse the repository at this point in the history
Noreabu — Wp amazon s3 and cloudfront
  • Loading branch information
mcaskill authored Dec 6, 2016
2 parents a165d1c + 69fdc74 commit dea6b70
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
32 changes: 31 additions & 1 deletion lib/ManualImageCrop.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,17 @@ private function filterPostData() {
return $data;
}

/**
* Callback function for plugin: amazon-s3-and-cloudfront
*/
function get_attached_file_copy_back_to_local( $copy_back_to_local, $file, $attachment_id ) {
if ( !file_exists($file)) {
return true; // we want the image on the server
} else {
return false;
}
}

/**
* Crops the image based on params passed in $_POST array
*
Expand All @@ -192,6 +203,15 @@ public function cropImage( $data = null, $silent_result = false ) {
$data = $this->filterPostData();
}

$imageMetadata = wp_get_attachment_metadata($data['attachmentId']);

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) {
Expand Down Expand Up @@ -296,7 +316,6 @@ public function cropImage( $data = null, $silent_result = false ) {
}

//saves the selected area
$imageMetadata = wp_get_attachment_metadata($data['attachmentId']);
$imageMetadata['micSelectedArea'][$data['editedSize']] = array(
'x' => $data['select']['x'],
'y' => $data['select']['y'],
Expand All @@ -307,6 +326,12 @@ public function cropImage( $data = null, $silent_result = false ) {
wp_update_attachment_metadata($data['attachmentId'], $imageMetadata);

if ( function_exists('wp_get_image_editor') ) {

// get local file - possible improvement: change hooks, so one call is enough
if ( is_plugin_active('amazon-s3-and-cloudfront/wordpress-s3.php') ) {
$src_file = get_attached_file($data['attachmentId']);
}

$img = wp_get_image_editor( $src_file );

if ( ! is_wp_error( $img ) ) {
Expand Down Expand Up @@ -444,6 +469,11 @@ public function cropImage( $data = null, $silent_result = false ) {
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;
Expand Down
19 changes: 12 additions & 7 deletions lib/ManualImageCropEditorWindow.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,22 @@ public function renderWindow() {
$cropMethod = get_option($editedSize.'_crop');
}

$uploadsDir = wp_upload_dir();
if ( is_plugin_active('amazon-s3-and-cloudfront/wordpress-s3.php') ) {
add_filter( 'as3cf_get_attached_file_copy_back_to_local', 'ManualImageCrop::get_attached_file_copy_back_to_local', 10, 3 );

// function get_attached_file is called to trigger the hook above
get_attached_file($postId);
}

$metaData = wp_get_attachment_metadata($postId);

$src_file_url = wp_get_attachment_image_src($postId, 'full');
if (!$src_file_url) {
echo json_encode (array('status' => 'error', 'message' => 'wrong attachement' ) );
if (!$metaData) {
echo json_encode (array('status' => 'error', 'message' => 'could not get metadata for attachement(' . $postId . ') try reuploading the file' ) );
exit;
}
$src_file = str_replace($uploadsDir['baseurl'], $uploadsDir['basedir'], $src_file_url[0]);
$sizes = getimagesize($src_file);

$sizes[0] = $metaData["width"];
$sizes[1] = $metaData["height"];

$original[0] = $sizes[0];
$original[1] = $sizes[1];
Expand Down Expand Up @@ -215,7 +220,7 @@ public function renderWindow() {


<?php
$ext = strtolower( pathinfo($src_file, PATHINFO_EXTENSION) );
$ext = strtolower( pathinfo($metaData["file"], PATHINFO_EXTENSION) );
if ($ext == 'jpg' || $ext == 'jpeg') {
echo '<div class="mic-option">';
echo '<label for="micQuality">' . __('Target JPEG Quality', 'microp') . ':</label> ';
Expand Down

0 comments on commit dea6b70

Please sign in to comment.