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