okaybueno /
images
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace OkayBueno\Images\Services\src; |
||||||
| 4 | |||||||
| 5 | use Illuminate\Support\Facades\Storage; |
||||||
| 6 | use OkayBueno\Images\Events\ImageWasCreated; |
||||||
| 7 | use OkayBueno\Images\Events\ImageWasProcessed; |
||||||
| 8 | use OkayBueno\Images\Events\ImageWasDeleted; |
||||||
| 9 | use OkayBueno\Images\Models\Image; |
||||||
| 10 | use OkayBueno\Images\Services\ImageProcessingServiceInterface; |
||||||
| 11 | use OkayBueno\Images\Services\ImageServiceInterface; |
||||||
| 12 | use OkayBueno\Images\Services\Validation\ImageValidatorInterface; |
||||||
| 13 | |||||||
| 14 | /** |
||||||
| 15 | * Class ImageService |
||||||
| 16 | * @package OkayBueno\Images\Services\src |
||||||
| 17 | */ |
||||||
| 18 | class ImageService implements ImageServiceInterface |
||||||
| 19 | { |
||||||
| 20 | protected $imageValidator; |
||||||
| 21 | protected $imageProcessingService; |
||||||
| 22 | |||||||
| 23 | /** |
||||||
| 24 | * @param ImageValidatorInterface $imageValidatorInterface |
||||||
| 25 | * @param ImageProcessingServiceInterface $imageProcessingServiceInterface |
||||||
| 26 | */ |
||||||
| 27 | public function __construct( |
||||||
| 28 | ImageValidatorInterface $imageValidatorInterface, |
||||||
| 29 | ImageProcessingServiceInterface $imageProcessingServiceInterface |
||||||
| 30 | ) |
||||||
| 31 | { |
||||||
| 32 | $this->imageValidator = $imageValidatorInterface; |
||||||
| 33 | $this->imageProcessingService = $imageProcessingServiceInterface; |
||||||
| 34 | } |
||||||
| 35 | |||||||
| 36 | |||||||
| 37 | /** |
||||||
| 38 | * @param int $imageId |
||||||
| 39 | * @return array|Image |
||||||
| 40 | */ |
||||||
| 41 | public function findImageById( $imageId ) |
||||||
| 42 | { |
||||||
| 43 | $data = [ |
||||||
| 44 | 'id' => $imageId |
||||||
| 45 | ]; |
||||||
| 46 | |||||||
| 47 | if ( $this->imageValidator->with( $data )->passes( ImageValidatorInterface::EXISTS_BY_ID ) ) |
||||||
| 48 | { |
||||||
| 49 | return Image::find( $imageId ); |
||||||
| 50 | } |
||||||
| 51 | |||||||
| 52 | return $this->imageValidator->errors(); |
||||||
| 53 | } |
||||||
| 54 | |||||||
| 55 | |||||||
| 56 | /** |
||||||
| 57 | * @param mixed $imageDataOrImageURL |
||||||
| 58 | * @param array $options |
||||||
| 59 | * @return array|bool|Image |
||||||
| 60 | */ |
||||||
| 61 | public function createImage( $imageDataOrImageURL, array $options = [] ) |
||||||
| 62 | { |
||||||
| 63 | $path = @$options['path']; |
||||||
| 64 | |||||||
| 65 | $imagePath = $this->imageProcessingService->createImageFromImageDataOrURL( $imageDataOrImageURL, $path, $options ); |
||||||
| 66 | |||||||
| 67 | if ( $imagePath ) |
||||||
| 68 | { |
||||||
| 69 | $filename = basename( $imagePath ); |
||||||
| 70 | |||||||
| 71 | $data = [ |
||||||
| 72 | 'path' => $imagePath, |
||||||
| 73 | 'filename' => $filename, |
||||||
| 74 | 'processed' => FALSE, |
||||||
| 75 | 'type' => @$options['type'], |
||||||
| 76 | ]; |
||||||
| 77 | |||||||
| 78 | if ( $this->imageValidator->with( $data )->passes( ImageValidatorInterface::CREATE ) ) |
||||||
| 79 | { |
||||||
| 80 | $image = Image::create( $data ); |
||||||
| 81 | |||||||
| 82 | event( new ImageWasCreated( $image, $options ) ); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 83 | |||||||
| 84 | return $image; |
||||||
| 85 | } |
||||||
| 86 | |||||||
| 87 | return $this->imageValidator->errors(); |
||||||
| 88 | } |
||||||
| 89 | |||||||
| 90 | return FALSE; |
||||||
| 91 | |||||||
| 92 | } |
||||||
| 93 | |||||||
| 94 | |||||||
| 95 | /** |
||||||
| 96 | * @param $imageId |
||||||
| 97 | * @param bool|TRUE $skipValidation |
||||||
| 98 | * @return array|bool |
||||||
| 99 | */ |
||||||
| 100 | public function deleteImage( $imageId, $skipValidation = TRUE ) |
||||||
| 101 | { |
||||||
| 102 | $data = [ |
||||||
| 103 | 'id' => $imageId |
||||||
| 104 | ]; |
||||||
| 105 | |||||||
| 106 | if ( $skipValidation || $this->imageValidator->with( $data )->passes( ImageValidatorInterface::EXISTS_BY_ID ) ) |
||||||
| 107 | { |
||||||
| 108 | $image = Image::find( $imageId ); |
||||||
| 109 | |||||||
| 110 | event( new ImageWasDeleted( $image ) ); |
||||||
|
0 ignored issues
–
show
The function
event was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 111 | |||||||
| 112 | return TRUE; |
||||||
| 113 | } |
||||||
| 114 | |||||||
| 115 | return $this->imageValidator->errors(); |
||||||
| 116 | } |
||||||
| 117 | |||||||
| 118 | |||||||
| 119 | /** |
||||||
| 120 | * @param mixed $imageIdOrImage |
||||||
| 121 | * @param array $options |
||||||
| 122 | * @return array |
||||||
| 123 | */ |
||||||
| 124 | public function processImage( $imageIdOrImage, array $options = [] ) |
||||||
| 125 | { |
||||||
| 126 | $image = $this->getInstance( $imageIdOrImage ); |
||||||
| 127 | |||||||
| 128 | if ( !is_array( $image ) && !$image->processed ) |
||||||
|
0 ignored issues
–
show
|
|||||||
| 129 | { |
||||||
| 130 | $sizes = @$options['sizes']; |
||||||
| 131 | $sizes = is_array( $sizes ) ? $sizes : []; |
||||||
| 132 | |||||||
| 133 | $finalThumbs = []; |
||||||
| 134 | $destinationPath = dirname( $image->path ); |
||||||
| 135 | foreach( $sizes as $sizeKey => $size ) |
||||||
| 136 | { |
||||||
| 137 | $thumbs = $this->imageProcessingService->resizeOrCropImageToSizes( $image->path, $destinationPath, [ $size ], $options ); |
||||||
| 138 | |||||||
| 139 | if ( $thumbs && !empty( $thumbs ) ) |
||||||
| 140 | { |
||||||
| 141 | $thumb = array_first( $thumbs ); |
||||||
| 142 | $finalThumbs[ $sizeKey ] = $thumb; |
||||||
| 143 | } |
||||||
| 144 | } |
||||||
| 145 | |||||||
| 146 | // Update the file. |
||||||
| 147 | $update = [ |
||||||
| 148 | 'thumbnails' => json_encode( $finalThumbs ), |
||||||
| 149 | 'processed' => TRUE |
||||||
| 150 | ]; |
||||||
| 151 | |||||||
| 152 | $image->update( $update ); |
||||||
| 153 | |||||||
| 154 | foreach( $update as $key => $value ) $image->{$key} = $value; |
||||||
| 155 | |||||||
| 156 | event( new ImageWasProcessed( $image ) ); |
||||||
|
0 ignored issues
–
show
The function
event was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 157 | |||||||
| 158 | } |
||||||
| 159 | |||||||
| 160 | return $image; |
||||||
| 161 | |||||||
| 162 | } |
||||||
| 163 | |||||||
| 164 | |||||||
| 165 | /** |
||||||
| 166 | * @param $imageIdOrImage |
||||||
| 167 | * @return array|bool |
||||||
| 168 | */ |
||||||
| 169 | public function destroyImage( $imageIdOrImage ) |
||||||
| 170 | { |
||||||
| 171 | $isImage = $imageIdOrImage instanceof Image; |
||||||
| 172 | |||||||
| 173 | $data = [ |
||||||
| 174 | 'id' => $isImage ? $imageIdOrImage : $imageIdOrImage->id |
||||||
| 175 | ]; |
||||||
| 176 | |||||||
| 177 | if ( $isImage || $this->imageValidator->with( $data )->passes( ImageValidatorInterface::EXISTS_BY_ID_EVEN_DELETED ) ) |
||||||
| 178 | { |
||||||
| 179 | $image = $isImage ? $imageIdOrImage : Image::find( $imageIdOrImage )->withTrashed(); |
||||||
| 180 | |||||||
| 181 | // Remove from local disk. |
||||||
| 182 | $this->removeImageFromDisk( $image, config( 'images.local_disk_name', 'local' ) ); |
||||||
|
0 ignored issues
–
show
The function
config was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 183 | |||||||
| 184 | // If it's in the cloud then remove it from the cloud too. |
||||||
| 185 | $cloudDiskName = config( 'images.cloud_disk_name' ); |
||||||
| 186 | if ( $image->processed && $cloudDiskName ) |
||||||
| 187 | { |
||||||
| 188 | $this->removeImageFromDisk( $image, $cloudDiskName ); |
||||||
| 189 | } |
||||||
| 190 | |||||||
| 191 | // Destroy the entry. |
||||||
| 192 | $image->forceDelete(); |
||||||
| 193 | |||||||
| 194 | return TRUE; |
||||||
| 195 | } |
||||||
| 196 | |||||||
| 197 | return $this->imageValidator->errors(); |
||||||
| 198 | } |
||||||
| 199 | |||||||
| 200 | |||||||
| 201 | /******************************************************************************************************************* |
||||||
| 202 | ******************************************************************************************************************* |
||||||
| 203 | ******************************************************************************************************************/ |
||||||
| 204 | /** |
||||||
| 205 | * @param Image $image |
||||||
| 206 | * @param $diskName |
||||||
| 207 | */ |
||||||
| 208 | protected function removeImageFromDisk( Image $image, $diskName ) |
||||||
| 209 | { |
||||||
| 210 | if ( $diskName ) |
||||||
| 211 | { |
||||||
| 212 | $disk = Storage::disk( $diskName ); |
||||||
| 213 | |||||||
| 214 | // Delete all thumbnails locally. |
||||||
| 215 | foreach( $image->thumbnails( NULL, TRUE ) as $thumb ) |
||||||
| 216 | { |
||||||
| 217 | $folderName = dirname( $thumb ); |
||||||
| 218 | if ( $disk->exists( $folderName ) ) $disk->deleteDirectory( $folderName ); |
||||||
| 219 | } |
||||||
| 220 | |||||||
| 221 | // Also remove the main pic. |
||||||
| 222 | if ( $disk->exists( $image->path ) ) $disk->delete( $image->path ); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 223 | } |
||||||
| 224 | } |
||||||
| 225 | |||||||
| 226 | /** |
||||||
| 227 | * @param $imageOrImageId |
||||||
| 228 | * @return array |
||||||
| 229 | */ |
||||||
| 230 | protected function getInstance( $imageOrImageId ) |
||||||
| 231 | { |
||||||
| 232 | $isImage = $imageOrImageId instanceof Image; |
||||||
| 233 | |||||||
| 234 | return $isImage ? $imageOrImageId : $this->findImageById( $imageOrImageId->id ); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 235 | |||||||
| 236 | } |
||||||
| 237 | |||||||
| 238 | } |