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

244 lines 7.0 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 $mediaUrl = $this->library()->getSourceURL( $attachmentId, $size, $args );
39 // If asset is not imported yet
40 } else {
41 $size = is_array( $size ) ? 'large' : $size;
42 $mediaUrl = AssetsAPIService::getHotlink( $assetId, $size, ['forcePreview' => $forcePreview ] );
43 }
44
45 return $mediaUrl;
46 }
47
48 /**
49 * Shorthand method for calling resize and crop methods for an image.
50 *
51 * @param string|int $assetId
52 * @param int $resizeW Resize width
53 * @param int $resizeH Resize height
54 * @param int $cropW Crop with
55 * @param int $cropH Crop height
56 * @param array $args Resizing options
57 *
58 * @return mixed
59 */
60 public function resizeSourceUrl( $assetId, $resizeW = null, $resizeH = null, $cropW = null, $cropH = null, $args = [] ){
61
62 // if $assetId is an attachment ID
63 if( $attachmentId = $this->getAttachmentId( $assetId ) ) {
64 $mediaUrl = ImageEditor::process( $attachmentId, $resizeW, $resizeH, $cropW, $cropH, $args );
65 // If asset is not imported yet
66 } else {
67 $mediaUrl = AssetsAPIService::getHotlink( $assetId, 'large', [ 'forcePreview' => false ] );
68 }
69
70 return $mediaUrl;
71 }
72
73 /**
74 * Retrieves the MediaLibraryService
75 *
76 * @return MediaLibraryService
77 */
78 public function library(){
79 return \Depicter::resolve('depicter.media.library');
80 }
81
82 /**
83 * Retrieve asset hotlink
84 *
85 * @param string|int $assetId
86 * @param string $size
87 * @param false $forcePreview
88 *
89 * @return mixed|string
90 */
91 public function getHotLink( $assetId, $size = 'large', $forcePreview = false ) {
92 return AssetsAPIService::getHotlink( $assetId, $size, [ 'forcePreview' => $forcePreview ]);
93 }
94
95 /**
96 * Retrieves source set of an asset by ID
97 *
98 * @param string|int $assetId
99 *
100 * @param string $size
101 * @param array $args
102 *
103 * @return bool|string
104 */
105 public function getSrcSet( $assetId, $size = 'medium', $args = [] )
106 {
107 $assetId = is_numeric( $assetId ) ? $assetId : $this->library()->getAttachmentForImportedAsset( $assetId );
108 $source = '';
109 if ( is_numeric( $assetId ) ) {
110 if ( is_array( $size ) && empty( $args['isSvg'] ) ) {
111
112 // set device to get srcset for specific device
113 if ( $args['device'] == 'mobile' && !empty( $args['cropData']->mobile ) ) {
114 $device = 'mobile';
115 } elseif( $args['device'] == 'tablet' && !empty( $args['cropData']->tablet ) ) {
116 $device = 'tablet';
117 } else {
118 $device = 'default';
119 }
120
121 if ( !empty( $args['cropData']->{$device} ) && !empty( $args['cropData']->{$device}->focalPoint ) && !empty( $size[0] ) && !empty( $size[1] ) && empty( $args['fullSizeImageLoaded'] ) ) {
122
123 $source = ImageEditor::process( $assetId,
124 $args['cropData']->{$device}->mediaSize->width,
125 $args['cropData']->{$device}->mediaSize->height,
126 $size[0], $size[1],
127 [
128 'focalX' => $args['cropData']->{$device}->focalPoint->x,
129 'focalY' => $args['cropData']->{$device}->focalPoint->y,
130 'upscale' => true,
131 'dry' => true
132 ]
133 );
134 }
135 if ( empty( $source ) ) {
136 $source = wp_get_attachment_image_src( $assetId, $size )[0];
137 }
138 $sources = [
139 $source
140 ];
141
142 if ( !empty( $args['cropData']->{$device} ) && !empty( $args['cropData']->{$device}->focalPoint ) && empty( $args['fullSizeImageLoaded'] ) ) {
143 // load 2x size
144 if ( !empty( $size[1] ) ) {
145 $highResWidth = $size[0] * 2;
146 $highResHeight = $size[1] * 2;
147 } else {
148 $highResWidth = $size[0] * 2;
149 $highResHeight = $size[1];
150 }
151
152 $highResUrl = ImageEditor::process( $assetId,
153 $args['cropData']->{$device}->mediaSize->width,
154 $args['cropData']->{$device}->mediaSize->height,
155 $highResWidth, $highResHeight,
156 [
157 'focalX' => $args['cropData']->{$device}->focalPoint->x,
158 'focalY' => $args['cropData']->{$device}->focalPoint->y,
159 'upscale' => true,
160 'dry' => ! empty( $args['fullSizeImageLoaded'] ),
161 ]
162 );
163
164 if ( $highResUrl ) {
165 $sources[] = $highResUrl;
166 }
167 }
168 return implode( ', ', $sources );
169
170 } else {
171 return wp_get_attachment_image_srcset( $assetId, $size );
172 }
173 }
174 return '';
175 }
176
177 /**
178 * Retrieves alternative text of an asset by ID
179 *
180 * @param string|int $assetId
181 *
182 * @return mixed|string
183 */
184 public function getAltText( $assetId )
185 {
186 if ( is_numeric( $assetId ) ) {
187 return get_post_meta( $assetId, '_wp_attachment_image_alt', true );
188 }
189 return '';
190 }
191
192 /**
193 * Get API link to hotlink to a media source
194 *
195 * @param string|int $assetId
196 * @param string $size
197 * @param string $forcePreview
198 *
199 * @return string
200 */
201 public function getAjaxHotlink( $assetId, $size = 'large', $forcePreview = 'false' )
202 {
203 return add_query_arg(
204 [
205 'id' => $assetId,
206 'size' => $size,
207 'forcePreview' => $forcePreview
208 ],
209 \Depicter::routeUrl('getMedia')
210 );
211 }
212
213 /**
214 * Process document content and download third party assets used in document.
215 *
216 * @param $content
217 *
218 * @throws GuzzleException
219 */
220 public function importDocumentAssets( $content ){
221 $assetIDs = Helper::extractAssetIds( $content );
222 $this->library()->importAssets( $assetIDs );
223 }
224
225 /**
226 * Get attachment Id by asset id
227 *
228 * @param string|int $assetId
229 *
230 * @return int|null
231 */
232 public function getAttachmentId( $assetId ){
233 // if $assetId is an attachment ID
234 if( is_numeric( $assetId ) ) {
235 return $assetId;
236 // If asset with $assetId was imported and registered before
237 } elseif ( $attachmentId = $this->library()->getAttachmentForImportedAsset( $assetId ) ){
238 return $attachmentId;
239 }
240
241 return null;
242 }
243 }
244