Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the focuspoint compatible with the image editor #33

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 187 additions & 0 deletions resources/EditService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

namespace ImageFocus;

/**
* The class responsible for resizing the images trough default WordPress functionality
*
* Class EditService
* @package ImageFocus
*/
class EditService
{
private $focusPoint = [
'x' => 50,
'y' => 50
];
private $attachmentId;

public function __construct()
{
$this->addHooks();
}

/**
* Make sure all hooks are being executed.
*/
private function addHooks()
{
add_filter('wp_save_image_editor_file', [$this, 'editFocusPoint'], 10, 5);
add_filter('wp_update_attachment_metadata', [$this, 'updateAttachments'], 10, 2);
}

/**
* If the image is edited in the WP media editor the focus point also needs to change
*
* @param $override
* @param $filename
* @param $image
* @param $mimeType
* @param $attachmentId
* @return null
*/
public function editFocusPoint($override, $filename, $image, $mimeType, $attachmentId)
{
$this->attachmentId = $attachmentId;

// Get the history from the supervariable to check if changes have been executed
$changes = getSuperData('history');

// Bail early if there are no changes
if (empty($changes)) {
return $override;
}

$this->getFocusPoint();

$changes = stripslashes_deep($changes);
$changes = preg_replace('/:\s*(\-?\d+(\.\d+)?([e|E][\-|\+]\d+)?)/', ': "$1"', $changes);
$changes = json_decode($changes, true);

// Loop trough all the changes and check what changes we need to apply to the focus point
foreach ((array)$changes as $change) {
foreach ((array)$change as $type => $unit)
switch ($type) {
case 'r':
$this->rotateFocusPoint($unit);
break;
case 'f':
$this->flipFocusPoint($unit);
break;
}
}

// Save the image
$override = $image->save($filename, $mimeType);
$this->cropAttachment();

// Overwrite the actual save
return $override;
}

/**
* Change the focus point after rotation
*
* @param $degrees
* @return $this
*/
private function rotateFocusPoint($degrees)
{
// Depending on the direction of the rotation we need to switch the axis
$axisX = (int)$degrees > 0 ? 'y' : 'x';
$axisY = (int)$degrees > 0 ? 'x' : 'y';

// Rotate the focuspoint
$rotatedFocusPoint[$axisX] = (($this->focusPoint[$axisY] - 50) * -1) + 50;
$rotatedFocusPoint[$axisY] = $this->focusPoint[$axisX];

$this->focusPoint = $rotatedFocusPoint;

return $this;
}

/**
* Change the focus point after flip
*
* @param $direction
* @return $this
*/
private function flipFocusPoint($direction)
{
$direction = (int)$direction === 2 ? 'x' : 'y';

$this->focusPoint[$direction] = (($this->focusPoint[$direction] - 50) * -1) + 50;

return $this;
}

/**
* Catch the wp_update_attachment_metadata filter.
* And make sure all the resizing goes trough the ImageFocus crop service.
*
* @param $filename
* @return mixed
*/
public function cropAfterSave($filename)
{
$this->getAttachmentIdByUrl($filename);

// Get the focus point
if ($this->attachmentId) {
$this->getFocusPoint();

// Crop the attachment trough the crop service
$this->cropAttachment();
}

return $filename;
}

/**
* Catch the wp_update_attachment_metadata filter.
* And make sure all the resizing goes trough the ImageFocus crop service.
*
* @param $data
* @param $attachmentId
* @return mixed
*/
public function updateAttachments($data, $attachmentId)
{
$this->attachmentId = $attachmentId;
$this->getFocusPoint();

// Crop the attachment trough the crop service
$this->cropAttachment();

return $data;
}

/**
* Set the focuspoint (post_meta) for the crop
*
* @return $this
*/
public function getFocusPoint()
{
$focusPoint = get_post_meta($this->attachmentId, 'focus_point', true);

if ($focusPoint) {
$this->focusPoint = $focusPoint;
}

return $this;
}

/**
* Execute the crop
*
* @return $this
*/
private function cropAttachment()
{
$crop = new CropService();
$crop->crop($this->attachmentId, $this->focusPoint);

return $this;
}
}
7 changes: 2 additions & 5 deletions resources/FocusPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ private function focusPointL10n()
{
return [
'cropButton' => __('Crop image', IMAGEFOCUS_TEXTDOMAIN),
'cropButtonProgress' => __('Cropping image ...', IMAGEFOCUS_TEXTDOMAIN),
'cropButtonSuccess' => __('Image cropped', IMAGEFOCUS_TEXTDOMAIN),
'cropButtonFailed' => __('Image crop failed', IMAGEFOCUS_TEXTDOMAIN),
];
}

Expand All @@ -58,7 +55,7 @@ private function focusPointL10n()
public function getFocusPoint()
{
// Get $_POST['attachment']
$attachment = getGlobalPostData('attachment');
$attachment = getSuperData('attachment');

// Get the post meta
$attachment['focusPoint'] = get_post_meta($attachment['id'], 'focus_point', true);
Expand All @@ -83,7 +80,7 @@ public function getFocusPoint()
public function initializeCrop()
{
// Get $_POST['attachment']
$attachment = getGlobalPostData('attachment');
$attachment = getSuperData('attachment');

$die = json_encode(['success' => false]);

Expand Down
2 changes: 1 addition & 1 deletion resources/ImageFocus.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function loadClasses()
* Load the resice service even if the current user is not allowed to upload files.
* This is to prevent WordPress from falsely resizing images back to the default focus point.
*/
new ResizeService();
new EditService();

if (current_user_can('upload_files') === false) {
return false;
Expand Down
49 changes: 0 additions & 49 deletions resources/ResizeService.php

This file was deleted.

6 changes: 3 additions & 3 deletions resources/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* @param null $postData
* @return null
*/
if (!function_exists('getGlobalPostData')) {
function getGlobalPostData($postDataKey = null, $postData = null)
if (!function_exists('getSuperData')) {
function getSuperData($postDataKey = null, $postData = null)
{
// Check if we need to fill the post data with $_POST
if ($postData === null) {
Expand All @@ -24,7 +24,7 @@ function getGlobalPostData($postDataKey = null, $postData = null)

// Call the same function if it's an array
if (is_array($data)) {
$postData[$key] = getGlobalPostData(null, $data);
$postData[$key] = getSuperData(null, $data);

continue;
}
Expand Down