PluginProbe
Depicter — Popup & Slider Builder / 1.9.2
Depicter — Popup & Slider Builder v1.9.2
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 1.9.2, at app/src/Services/MediaBridge.php

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