PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Services / MediaBridge.php

MediaBridge.php in Depicter — Popup & Slider Builder trunk, at app/src/Services/MediaBridge.php

283 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Services;
3
4
5 use Averta\Core\Utility\Arr;
6 use Depicter\Document\Helper\Helper;
7 use Depicter\GuzzleHttp\Exception\GuzzleException;
8 use Depicter\Media\Image\ImageEditor;
9 use Exception;
10 use Psr\Http\Message\StreamInterface;
11
12 /**
13 * A bridge for MediaLibrary Service and AssetsAPIService to retrieve medias from different services
14 *
15 * Class MediaBridge
16 *
17 * @package Depicter\Services
18 */
19 class MediaBridge
20 {
21 /**
22 * Empty image placeholder
23 */
24 const IMAGE_PLACEHOLDER_SRC = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
25
26
27 /**
28 * Retrieves source of an asset by ID
29 *
30 * @param string|int $assetId
31 * @param string $size
32 * @param bool $forcePreview
33 *
34 * @return array|false|string
35 * @throws Exception|GuzzleException
36 */
37 public function getSourceUrl( $assetId, $size = 'large', $forcePreview = false, $args = [] )
38 {
39 // if $assetId is an attachment ID
40 if( $attachmentId = $this->getAttachmentId( $assetId ) ) {
41 try {
42 $mediaUrl = $this->library()->getSourceURL( $attachmentId, $size, $args );
43 } catch( Exception $e ) {
44 // if asset is imported but deleted manually after a while
45 if ( ! is_numeric( $assetId ) ) {
46 $this->library()->importAsset( $assetId, true );
47 $attachmentId = $this->getAttachmentId( $assetId );
48 $mediaUrl = $this->library()->getSourceURL( $attachmentId, $size, $args );
49 }
50 }
51
52 // If asset is not imported yet
53 } else {
54 $size = is_array( $size ) ? 'large' : $size;
55
56 $mediaUrl = AssetsAPIService::getHotlink( $assetId, $size, Arr::merge( ['forcePreview' => $forcePreview ], $args ) );
57 }
58
59 return $mediaUrl;
60 }
61
62 /**
63 * Shorthand method for calling resize and crop methods for an image.
64 *
65 * @param string|int $assetId
66 * @param int $resizeW Resize width
67 * @param int $resizeH Resize height
68 * @param int $cropW Crop with
69 * @param int $cropH Crop height
70 * @param array $args Resizing options
71 *
72 * @return mixed
73 * @throws GuzzleException
74 */
75 public function resizeSourceUrl( $assetId, $resizeW = null, $resizeH = null, $cropW = null, $cropH = null, $args = [] ){
76
77 // if $assetId is an attachment ID
78 if( $attachmentId = $this->getAttachmentId( $assetId ) ) {
79 try {
80 $mediaUrl = ImageEditor::process( $attachmentId, $resizeW, $resizeH, $cropW, $cropH, $args );
81 } catch( Exception $e ) {
82 // if asset is imported but deleted manually after a while
83 if ( ! is_numeric( $assetId ) ) {
84 $this->library()->importAsset( $assetId, true );
85 $attachmentId = $this->getAttachmentId( $assetId );
86 $mediaUrl = ImageEditor::process( $attachmentId, $resizeW, $resizeH, $cropW, $cropH, $args );
87 }
88 }
89 // If asset is not imported yet
90 } else {
91 $mediaUrl = AssetsAPIService::getHotlink( $assetId, 'large', [ 'forcePreview' => false ] );
92 }
93
94 return $mediaUrl;
95 }
96
97 /**
98 * Retrieves the MediaLibraryService
99 *
100 * @return MediaLibraryService
101 */
102 public function library(){
103 return \Depicter::resolve('depicter.media.library');
104 }
105
106 /**
107 * Retrieve asset hotlink
108 *
109 * @param string|int $assetId
110 * @param string $size
111 * @param false $forcePreview
112 *
113 * @return mixed|string
114 */
115 public function getHotLink( $assetId, $size = 'large', $forcePreview = false ) {
116 return AssetsAPIService::getHotlink( $assetId, $size, [ 'forcePreview' => $forcePreview ]);
117 }
118
119 /**
120 * Retrieves source set of an asset by ID
121 *
122 * @param string|int $assetId
123 *
124 * @param string $size
125 * @param array $args
126 *
127 * @return bool|string
128 */
129 public function getSrcSet( $assetId, $size = 'medium', $args = [] )
130 {
131 $assetId = is_numeric( $assetId ) ? $assetId : $this->library()->getAttachmentForImportedAsset( $assetId );
132 $source = '';
133 if ( is_numeric( $assetId ) ) {
134 if ( is_array( $size ) && empty( $args['isSvg'] ) ) {
135
136 // set device to get srcset for specific device
137 if ( $args['device'] == 'mobile' && !empty( $args['cropData']->mobile ) ) {
138 $device = 'mobile';
139 } elseif( $args['device'] == 'tablet' && !empty( $args['cropData']->tablet ) ) {
140 $device = 'tablet';
141 } else {
142 $device = 'default';
143 }
144
145 if ( !empty( $args['cropData']->{$device} ) && !empty( $args['cropData']->{$device}->focalPoint ) && !empty( $size[0] ) && !empty( $size[1] ) && empty( $args['fullSizeImageLoaded'] ) ) {
146
147 $source = ImageEditor::process( $assetId,
148 $args['cropData']->{$device}->mediaSize->width,
149 $args['cropData']->{$device}->mediaSize->height,
150 $size[0], $size[1],
151 [
152 'focalX' => $args['cropData']->{$device}->focalPoint->x,
153 'focalY' => $args['cropData']->{$device}->focalPoint->y,
154 'upscale' => true,
155 'dry' => true
156 ]
157 );
158 }
159 if ( empty( $source ) ) {
160 $source = wp_get_attachment_image_src( $assetId, $size )[0];
161 }
162 $sources = [
163 $source
164 ];
165
166 if ( !empty( $args['cropData']->{$device} ) && !empty( $args['cropData']->{$device}->focalPoint ) && empty( $args['fullSizeImageLoaded'] ) ) {
167 // load 2x size
168 if ( !empty( $size[1] ) ) {
169 $highResWidth = $size[0] * 2;
170 $highResHeight = $size[1] * 2;
171 } else {
172 $highResWidth = $size[0] * 2;
173 $highResHeight = $size[1];
174 }
175
176 $highResUrl = ImageEditor::process( $assetId,
177 $args['cropData']->{$device}->mediaSize->width,
178 $args['cropData']->{$device}->mediaSize->height,
179 $highResWidth, $highResHeight,
180 [
181 'focalX' => $args['cropData']->{$device}->focalPoint->x,
182 'focalY' => $args['cropData']->{$device}->focalPoint->y,
183 'upscale' => true,
184 'dry' => ! empty( $args['fullSizeImageLoaded'] ),
185 ]
186 );
187
188 if ( $highResUrl ) {
189 $sources[] = $highResUrl;
190 }
191 }
192 return implode( ', ', $sources );
193
194 } else {
195 return wp_get_attachment_image_srcset( $assetId, $size );
196 }
197 }
198 return '';
199 }
200
201 /**
202 * Retrieves alternative text of an asset by ID
203 *
204 * @param string|int $assetId
205 *
206 * @return mixed|string
207 */
208 public function getAltText( $assetId )
209 {
210 if ( is_numeric( $assetId ) ) {
211 return get_post_meta( $assetId, '_wp_attachment_image_alt', true );
212 }
213 return '';
214 }
215
216 /**
217 * Get API link to hotlink to a media source
218 *
219 * @param string|int $assetId
220 * @param string $size
221 * @param string $forcePreview
222 *
223 * @return string
224 */
225 public function getAjaxHotlink( $assetId, $size = 'large', $forcePreview = 'false' )
226 {
227 return add_query_arg(
228 [
229 'id' => $assetId,
230 'size' => $size,
231 'forcePreview' => $forcePreview
232 ],
233 \Depicter::routeUrl('getMedia')
234 );
235 }
236
237 /**
238 * Process document content and download third party assets used in document.
239 *
240 * @param $content
241 *
242 * @throws GuzzleException
243 */
244 public function importDocumentAssets( $content ){
245 $assetIDs = Helper::extractAssetIds( $content );
246 $this->library()->importAssets( $assetIDs );
247 }
248
249 /**
250 * Get attachment Id by asset id
251 *
252 * @param string|int $assetId
253 *
254 * @return int|null
255 */
256 public function getAttachmentId( $assetId ){
257 // if $assetId is an attachment ID
258 if( is_numeric( $assetId ) ) {
259 return $assetId;
260 // If asset with $assetId was imported and registered before
261 } elseif ( $attachmentId = $this->library()->getAttachmentForImportedAsset( $assetId ) ){
262 return $attachmentId;
263 }
264
265 return null;
266 }
267
268
269 /**
270 * Remove attachment from imported media dictionary
271 *
272 * @param int $attachmentId
273 * @return void
274 */
275 public function maybeRemoveAttachmentFromDictionary( $attachmentId ) {
276 $imported_media_dictionary = \Depicter::options()->get( 'imported_assets', [] );
277 if ( false !== $array_key = array_search( $attachmentId, $imported_media_dictionary ) ) {
278 unset( $imported_media_dictionary[ $array_key ] );
279 \Depicter::options()->set( 'imported_assets', $imported_media_dictionary );
280 }
281 }
282 }
283