first commit
This commit is contained in:
245
wp-content/plugins/brizy/editor/asset/crop/cropper.php
Normal file
245
wp-content/plugins/brizy/editor/asset/crop/cropper.php
Normal file
@@ -0,0 +1,245 @@
|
||||
<?php
|
||||
|
||||
class Brizy_Editor_Asset_Crop_Cropper {
|
||||
|
||||
const BASIC_CROP_TYPE = 1;
|
||||
const ADVANCED_CROP_TYPE = 2;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $services;
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Asset_Crop_Cropper constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
$this->services = array(
|
||||
//'Brizy_Editor_Asset_Crop_ExternalService',
|
||||
'Brizy_Editor_Asset_Crop_WordpressService'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $source
|
||||
* @param $target
|
||||
* @param $callback
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function serviceLoop( $source, $target, $callback ) {
|
||||
foreach ( $this->services as $serviceClass ) {
|
||||
try {
|
||||
/**
|
||||
* @var Brizy_Editor_Asset_Crop_ServiceInterface $service ;
|
||||
*/
|
||||
$service = new $serviceClass( $source, $target );
|
||||
if ( $callback( $service ) ) {
|
||||
return true;
|
||||
}
|
||||
} catch ( Exception $e ) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $source
|
||||
* @param $target
|
||||
* @param $offsetX
|
||||
* @param $offsetY
|
||||
* @param $width
|
||||
* @param $height
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function serviceCrop( $source, $target, $offsetX, $offsetY, $width, $height ) {
|
||||
|
||||
return $this->serviceLoop( $source, $target, function ( Brizy_Editor_Asset_Crop_ServiceInterface $service ) use ( $offsetX, $offsetY, $width, $height ) {
|
||||
$result = $service->crop( $offsetX, $offsetY, $width, $height );
|
||||
|
||||
if ( $result ) {
|
||||
$result = $service->saveTargetImage();
|
||||
}
|
||||
|
||||
return $result;
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $source
|
||||
* @param $target
|
||||
* @param $width
|
||||
* @param $height
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function serviceResize( $source, $target, $width, $height ) {
|
||||
return $this->serviceLoop( $source, $target, function ( Brizy_Editor_Asset_Crop_ServiceInterface $service ) use ( $width, $height ) {
|
||||
$result = $service->resize( $width, $height );
|
||||
|
||||
if ( $result ) {
|
||||
$result = $service->saveTargetImage();
|
||||
}
|
||||
|
||||
return $result;
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $source
|
||||
* @param $target
|
||||
* @param $filterOptions
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function internalCrop( $source, $target, $filterOptions ) {
|
||||
if ( $filterOptions['format'] == "gif" ) {
|
||||
// do not resize
|
||||
return false;
|
||||
} else {
|
||||
list( $imgWidth, $imgHeight ) = $filterOptions['originalSize'];
|
||||
if ( $filterOptions['is_advanced'] === false ) {
|
||||
list( $requestedImgWidth, $requestedImgHeight ) = array_values( $filterOptions['requestedData'] );
|
||||
if ( $requestedImgWidth > $imgWidth && ( $requestedImgHeight == "any" || $requestedImgHeight == "*" ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->serviceResize( $source, $target, $requestedImgWidth, $requestedImgHeight );
|
||||
}
|
||||
|
||||
list( $requestedImgWidth, $requestedImgHeight, $requestedOffsetX, $requestedOffsetY, $containerWidth, $containerHeight ) = array_values( $filterOptions['requestedData'] );
|
||||
|
||||
if ( ( $containerWidth > $imgWidth || $containerHeight > $imgHeight ) || $requestedImgWidth > $imgWidth && $requestedImgHeight > $imgHeight ) {
|
||||
|
||||
$newOffsetX = $imgWidth * $requestedOffsetX / $requestedImgWidth;
|
||||
$newOffsetY = $imgHeight * $requestedOffsetY / $requestedImgHeight;
|
||||
|
||||
$newImgWidth = $imgWidth * $containerWidth / $requestedImgWidth;
|
||||
$newImgHeight = $imgHeight * $containerHeight / $requestedImgHeight;
|
||||
|
||||
return $this->serviceCrop( $source, $target, $newOffsetX, $newOffsetY, $newImgWidth, $newImgHeight );
|
||||
|
||||
} else {
|
||||
|
||||
return $this->serviceLoop( $source, $target, function ( Brizy_Editor_Asset_Crop_ServiceInterface $service ) use ( $source, $target, $requestedImgWidth, $requestedImgHeight, $requestedOffsetX, $requestedOffsetY, $containerWidth, $containerHeight ) {
|
||||
|
||||
$result = $service->resize( $requestedImgWidth, null );
|
||||
|
||||
$imageSizeAfterResize = $service->getSize();
|
||||
|
||||
$blackStripOnX = ( $containerWidth + $requestedOffsetX ) - $requestedImgWidth;
|
||||
$blackStripOnY = ( $containerHeight + $requestedOffsetY ) - $requestedImgHeight;
|
||||
|
||||
$requestedImgWidth = min( $imageSizeAfterResize['width'], $requestedImgWidth );
|
||||
$requestedImgHeight = min( $imageSizeAfterResize['height'], $requestedImgHeight );
|
||||
|
||||
// calculated the crop over boundary values
|
||||
$containerWidthStripDelta = $blackStripOnX > 0 ? $blackStripOnX : 0;
|
||||
$containerHeightStripDelta = $blackStripOnY > 0 ? $blackStripOnY : 0;
|
||||
|
||||
// make sure the requested crop offset and size does now go over image boundaries
|
||||
$requestedOffsetX = ( $blackStripOnX ) > 0 ? ( $requestedOffsetX - $containerWidthStripDelta ) : $requestedOffsetX;
|
||||
$requestedOffsetY = ( $blackStripOnY ) > 0 ? ( $requestedOffsetY - $containerHeightStripDelta ) : $requestedOffsetY;
|
||||
|
||||
// avoid the case when the image height or with is less that 1px (GD problem)
|
||||
$containerWidth = $containerWidth > $requestedImgWidth ? $requestedImgWidth : $containerWidth;
|
||||
$containerHeight = $containerHeight > $requestedImgHeight ? $requestedImgHeight : $containerHeight;
|
||||
|
||||
if ( $result && $service->crop( $requestedOffsetX, $requestedOffsetY, $containerWidth, $containerHeight ) ) {
|
||||
$result = $service->saveTargetImage();
|
||||
|
||||
if ( ! $result ) {
|
||||
@unlink( $target );
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
return false;
|
||||
} );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $source
|
||||
* @param $target
|
||||
* @param $filter
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function crop( $source, $target, $filter, $originalSizes ) {
|
||||
|
||||
try {
|
||||
wp_raise_memory_limit( 'image' );
|
||||
|
||||
return $this->internalCrop( $source, $target, $this->getFilterOptions( $source, $filter, $originalSizes ) );
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->error( $e->getMessage(), [ $e ] );
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $source
|
||||
* @param $filter
|
||||
*
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getFilterOptions( $source, $filter, $originalSizes ) {
|
||||
|
||||
parse_str( strtolower( $filter ), $output );
|
||||
$configuration = array();
|
||||
$configuration['format'] = pathinfo( basename( $source ), PATHINFO_EXTENSION );
|
||||
$configuration['originalSize'] = $originalSizes;
|
||||
|
||||
$cropType = $this->getCropType( $filter );
|
||||
|
||||
switch ( $cropType ) {
|
||||
case self::BASIC_CROP_TYPE:
|
||||
$configuration['requestedData']['imageWidth'] = (int) $output['iw'];
|
||||
$configuration['requestedData']['imageHeight'] = (int) $output['ih'];
|
||||
$configuration['is_advanced'] = false;
|
||||
break;
|
||||
case self::ADVANCED_CROP_TYPE:
|
||||
$configuration['requestedData']['imageWidth'] = (int) $output['iw'];
|
||||
$configuration['requestedData']['imageHeight'] = (int) $output['ih'];
|
||||
$configuration['requestedData']['offsetX'] = (int) $output['ox'];
|
||||
$configuration['requestedData']['offsetY'] = (int) $output['oy'];
|
||||
$configuration['requestedData']['cropWidth'] = (int) $output['cw'];
|
||||
$configuration['requestedData']['cropHeight'] = (int) $output['ch'];
|
||||
$configuration['is_advanced'] = true;
|
||||
break;
|
||||
}
|
||||
|
||||
return $configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $filter
|
||||
*
|
||||
* @return int|null
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getCropType( $filter ) {
|
||||
$regExAdvanced = "/^iW=[0-9]{1,4}&iH=[0-9]{1,4}&oX=[0-9]{1,4}&oY=[0-9]{1,4}&cW=[0-9]{1,4}&cH=[0-9]{1,4}$/is";
|
||||
$regExBasic = "/^iW=[0-9]{1,4}&iH=([0-9]{1,4}|any|\*{1})$/is";
|
||||
|
||||
if ( preg_match( $regExBasic, $filter ) ) {
|
||||
$cropType = self::BASIC_CROP_TYPE;
|
||||
} elseif ( preg_match( $regExAdvanced, $filter ) ) {
|
||||
$cropType = self::ADVANCED_CROP_TYPE;
|
||||
} else {
|
||||
throw new Exception( "Invalid size format." );
|
||||
}
|
||||
|
||||
return $cropType;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: alex
|
||||
* Date: 4/4/19
|
||||
* Time: 3:51 PM
|
||||
*/
|
||||
|
||||
class Brizy_Editor_Asset_Crop_ExternalService implements Brizy_Editor_Asset_Crop_ServiceInterface {
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $targetPath;
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Asset_Crop_WordpressService constructor.
|
||||
*
|
||||
* @param $sourcePath
|
||||
* @param $targetPath
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct( $sourcePath, $targetPath ) {
|
||||
|
||||
if ( ! file_exists( $sourcePath ) ) {
|
||||
throw new Exception( 'Unable to crop media. Source file not found.' );
|
||||
}
|
||||
|
||||
$this->targetPath = $targetPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offsetX
|
||||
* @param int $offsetY
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function crop( $offsetX, $offsetY, $width, $height ) {
|
||||
throw new Exception( 'Not implemented' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function resize( $width, $height ) {
|
||||
throw new Exception( 'Not implemented' );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function saveTargetImage() {
|
||||
throw new Exception( 'Not implemented' );
|
||||
}
|
||||
|
||||
public function getSize() {
|
||||
throw new Exception( 'Not implemented' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
interface Brizy_Editor_Asset_Crop_ServiceInterface {
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Asset_Crop_ServiceInterface constructor.
|
||||
*
|
||||
* @param string $sourcePath File paths
|
||||
* @param string $targetPath File paths
|
||||
*/
|
||||
public function __construct( $sourcePath, $targetPath );
|
||||
|
||||
/**
|
||||
* @param int $offsetX
|
||||
* @param int $offsetY
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function crop( $offsetX, $offsetY, $width, $height );
|
||||
|
||||
/**
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function resize( $width, $height );
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSize();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function saveTargetImage();
|
||||
}
|
||||
105
wp-content/plugins/brizy/editor/asset/crop/wordpress-service.php
Normal file
105
wp-content/plugins/brizy/editor/asset/crop/wordpress-service.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: alex
|
||||
* Date: 4/4/19
|
||||
* Time: 3:51 PM
|
||||
*/
|
||||
|
||||
class Brizy_Editor_Asset_Crop_WordpressService implements Brizy_Editor_Asset_Crop_ServiceInterface {
|
||||
|
||||
/**
|
||||
* @var WP_Image_Editor
|
||||
*/
|
||||
private $imageEditor;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $targetPath;
|
||||
|
||||
/**
|
||||
* Brizy_Editor_Asset_Crop_WordpressService constructor.
|
||||
*
|
||||
* @param $sourcePath
|
||||
* @param $targetPath
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct( $sourcePath, $targetPath ) {
|
||||
|
||||
if ( ! file_exists( $sourcePath ) ) {
|
||||
throw new Exception( 'Unable to crop media. Source file not found.' );
|
||||
}
|
||||
|
||||
$this->targetPath = $targetPath;
|
||||
$this->imageEditor = wp_get_image_editor( $sourcePath );
|
||||
|
||||
if ( $this->imageEditor instanceof WP_Error ) {
|
||||
Brizy_Logger::instance()->error( $this->imageEditor->get_error_message(), array( $this->imageEditor ) );
|
||||
throw new Exception( "Unable to obtain the image editor" );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offsetX
|
||||
* @param int $offsetY
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function crop( $offsetX, $offsetY, $width, $height ) {
|
||||
try {
|
||||
$this->imageEditor->crop( $offsetX, $offsetY, $width, $height );
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->error( $e->getMessage(), [ $e ] );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public function getSize() {
|
||||
try {
|
||||
return $this->imageEditor->get_size();
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->error( $e->getMessage(), [ $e ] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function resize( $width, $height ) {
|
||||
|
||||
try {
|
||||
$this->imageEditor->resize( $width, $height );
|
||||
} catch ( Exception $e ) {
|
||||
Brizy_Logger::instance()->error( $e->getMessage(), [ $e ] );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function saveTargetImage() {
|
||||
$result = $this->imageEditor->save( $this->targetPath );
|
||||
|
||||
if ( $result instanceof WP_Error ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user