| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package WPEmergeAppCore |
| 4 |
* @author Atanas Angelov <hi@atanas.dev> |
| 5 |
* @copyright 2017-2020 Atanas Angelov |
| 6 |
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
| 7 |
* @link https://wpemerge.com/ |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace WPEmergeAppCore\Image; |
| 11 |
|
| 12 |
use WPEmerge\Helpers\MixedType; |
| 13 |
|
| 14 |
class Image { |
| 15 |
/** |
| 16 |
* Filesystem. |
| 17 |
* |
| 18 |
* @var \WP_Filesystem_Base |
| 19 |
*/ |
| 20 |
protected $filesystem = null; |
| 21 |
|
| 22 |
/** |
| 23 |
* Constructor. |
| 24 |
* |
| 25 |
* @param \WP_Filesystem_Base $filesystem |
| 26 |
*/ |
| 27 |
public function __construct( \WP_Filesystem_Base $filesystem ) { |
| 28 |
$this->filesystem = $filesystem; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get a suitable name for a resized version of an image file. |
| 33 |
* |
| 34 |
* @param string $filepath |
| 35 |
* @param integer $width |
| 36 |
* @param integer $height |
| 37 |
* @param boolean $crop |
| 38 |
* @return string |
| 39 |
*/ |
| 40 |
protected function getResizedFilename( $filepath, $width, $height, $crop ) { |
| 41 |
$filename = basename( $filepath ); |
| 42 |
|
| 43 |
// match filename extension with dot |
| 44 |
// only the last extension will match when there are multiple ones |
| 45 |
$extension_pattern = '/(\.[^\.]+)$/'; |
| 46 |
|
| 47 |
// add width, height and crop to filename |
| 48 |
$replacement = '-' . $width . 'x' . $height . ( $crop ? '-cropped' : '' ) . '$1'; |
| 49 |
|
| 50 |
return preg_replace( $extension_pattern, $replacement, $filename ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Resize and store a copy of an image file. |
| 55 |
* |
| 56 |
* @param string $source |
| 57 |
* @param string $destination |
| 58 |
* @param integer $width |
| 59 |
* @param integer $height |
| 60 |
* @param boolean $crop |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
protected function store( $source, $destination, $width, $height, $crop ) { |
| 64 |
if ( $this->filesystem->exists( $destination ) ) { |
| 65 |
return $destination; |
| 66 |
} |
| 67 |
|
| 68 |
$editor = wp_get_image_editor( $source ); |
| 69 |
|
| 70 |
if ( is_wp_error( $editor ) ) { |
| 71 |
return ''; |
| 72 |
} |
| 73 |
|
| 74 |
$editor->resize( $width, $height, $crop ); |
| 75 |
$editor->save( $destination ); |
| 76 |
|
| 77 |
return $destination; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Dynamically generate a thumbnail (if one is not already available) and return the url. |
| 82 |
* |
| 83 |
* @param integer $attachment_id |
| 84 |
* @param integer $width |
| 85 |
* @param integer $height |
| 86 |
* @param boolean $crop |
| 87 |
* @return string |
| 88 |
*/ |
| 89 |
public function thumbnail( $attachment_id, $width, $height, $crop = true ) { |
| 90 |
$width = absint( $width ); |
| 91 |
$height = absint( $height ); |
| 92 |
|
| 93 |
$upload_dir = wp_upload_dir(); |
| 94 |
$attachment = wp_get_attachment_metadata( $attachment_id ); |
| 95 |
$source = MixedType::normalizePath( get_attached_file( $attachment_id ) ); |
| 96 |
|
| 97 |
if ( ! $attachment || ! $this->filesystem->exists( $source ) ) { |
| 98 |
return ''; |
| 99 |
} |
| 100 |
|
| 101 |
$attachment_subdirectory = preg_replace( '/\/?[^\/]+\z/', '', $attachment['file'] ); |
| 102 |
$filename = $this->getResizedFilename( $source, $width, $height, $crop ); |
| 103 |
$destination = MixedType::normalizePath( MixedType::normalizePath( $upload_dir['basedir'] ) . DIRECTORY_SEPARATOR . $attachment_subdirectory ) . DIRECTORY_SEPARATOR . $filename; |
| 104 |
|
| 105 |
$stored = $this->store( $source, $destination, $width, $height, $crop ); |
| 106 |
|
| 107 |
if ( empty( $stored ) ) { |
| 108 |
return ''; |
| 109 |
} |
| 110 |
|
| 111 |
$fileurl = trailingslashit( $upload_dir['baseurl'] ) . $attachment_subdirectory . '/' . $filename; |
| 112 |
|
| 113 |
return $fileurl; |
| 114 |
} |
| 115 |
} |
| 116 |
|