111 $metadata['sizes'] = $editor->multi_resize( $sizes ); |
120 $metadata['sizes'] = $editor->multi_resize( $sizes ); |
112 } else { |
121 } else { |
113 $metadata['sizes'] = array(); |
122 $metadata['sizes'] = array(); |
114 } |
123 } |
115 |
124 |
116 // fetch additional metadata from exif/iptc |
125 // Fetch additional metadata from EXIF/IPTC. |
117 $image_meta = wp_read_image_metadata( $file ); |
126 $image_meta = wp_read_image_metadata( $file ); |
118 if ( $image_meta ) |
127 if ( $image_meta ) |
119 $metadata['image_meta'] = $image_meta; |
128 $metadata['image_meta'] = $image_meta; |
120 |
129 |
121 } elseif ( preg_match( '#^video/#', get_post_mime_type( $attachment ) ) ) { |
130 } elseif ( wp_attachment_is( 'video', $attachment ) ) { |
122 $metadata = wp_read_video_metadata( $file ); |
131 $metadata = wp_read_video_metadata( $file ); |
123 $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) && post_type_supports( 'attachment:video', 'thumbnail' ); |
132 $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' ); |
124 } elseif ( preg_match( '#^audio/#', get_post_mime_type( $attachment ) ) ) { |
133 } elseif ( wp_attachment_is( 'audio', $attachment ) ) { |
125 $metadata = wp_read_audio_metadata( $file ); |
134 $metadata = wp_read_audio_metadata( $file ); |
126 $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) && post_type_supports( 'attachment:audio', 'thumbnail' ); |
135 $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' ); |
127 } |
136 } |
128 |
137 |
129 if ( $support && ! empty( $metadata['image']['data'] ) ) { |
138 if ( $support && ! empty( $metadata['image']['data'] ) ) { |
130 $ext = '.jpg'; |
139 // Check for existing cover. |
131 switch ( $metadata['image']['mime'] ) { |
140 $hash = md5( $metadata['image']['data'] ); |
132 case 'image/gif': |
141 $posts = get_posts( array( |
133 $ext = '.gif'; |
142 'fields' => 'ids', |
134 break; |
143 'post_type' => 'attachment', |
135 case 'image/png': |
144 'post_mime_type' => $metadata['image']['mime'], |
136 $ext = '.png'; |
145 'post_status' => 'inherit', |
137 break; |
146 'posts_per_page' => 1, |
138 } |
147 'meta_key' => '_cover_hash', |
139 $basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext; |
148 'meta_value' => $hash |
140 $uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] ); |
149 ) ); |
141 if ( false === $uploaded['error'] ) { |
150 $exists = reset( $posts ); |
142 $attachment = array( |
151 |
143 'post_mime_type' => $metadata['image']['mime'], |
152 if ( ! empty( $exists ) ) { |
144 'post_type' => 'attachment', |
153 update_post_meta( $attachment_id, '_thumbnail_id', $exists ); |
145 'post_content' => '', |
154 } else { |
146 ); |
155 $ext = '.jpg'; |
147 $sub_attachment_id = wp_insert_attachment( $attachment, $uploaded['file'] ); |
156 switch ( $metadata['image']['mime'] ) { |
148 $attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] ); |
157 case 'image/gif': |
149 wp_update_attachment_metadata( $sub_attachment_id, $attach_data ); |
158 $ext = '.gif'; |
150 update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id ); |
159 break; |
151 } |
160 case 'image/png': |
152 } |
161 $ext = '.png'; |
153 // remove the blob of binary data from the array |
162 break; |
154 unset( $metadata['image']['data'] ); |
163 } |
155 |
164 $basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext; |
|
165 $uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] ); |
|
166 if ( false === $uploaded['error'] ) { |
|
167 $image_attachment = array( |
|
168 'post_mime_type' => $metadata['image']['mime'], |
|
169 'post_type' => 'attachment', |
|
170 'post_content' => '', |
|
171 ); |
|
172 /** |
|
173 * Filter the parameters for the attachment thumbnail creation. |
|
174 * |
|
175 * @since 3.9.0 |
|
176 * |
|
177 * @param array $image_attachment An array of parameters to create the thumbnail. |
|
178 * @param array $metadata Current attachment metadata. |
|
179 * @param array $uploaded An array containing the thumbnail path and url. |
|
180 */ |
|
181 $image_attachment = apply_filters( 'attachment_thumbnail_args', $image_attachment, $metadata, $uploaded ); |
|
182 |
|
183 $sub_attachment_id = wp_insert_attachment( $image_attachment, $uploaded['file'] ); |
|
184 add_post_meta( $sub_attachment_id, '_cover_hash', $hash ); |
|
185 $attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] ); |
|
186 wp_update_attachment_metadata( $sub_attachment_id, $attach_data ); |
|
187 update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id ); |
|
188 } |
|
189 } |
|
190 } |
|
191 |
|
192 // Remove the blob of binary data from the array. |
|
193 if ( isset( $metadata['image']['data'] ) ) |
|
194 unset( $metadata['image']['data'] ); |
|
195 |
|
196 /** |
|
197 * Filter the generated attachment meta data. |
|
198 * |
|
199 * @since 2.1.0 |
|
200 * |
|
201 * @param array $metadata An array of attachment meta data. |
|
202 * @param int $attachment_id Current attachment ID. |
|
203 */ |
156 return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ); |
204 return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ); |
157 } |
205 } |
158 |
206 |
159 /** |
207 /** |
160 * Convert a fraction string to a decimal. |
208 * Convert a fraction string to a decimal. |
221 'copyright' => '', |
271 'copyright' => '', |
222 'focal_length' => 0, |
272 'focal_length' => 0, |
223 'iso' => 0, |
273 'iso' => 0, |
224 'shutter_speed' => 0, |
274 'shutter_speed' => 0, |
225 'title' => '', |
275 'title' => '', |
|
276 'orientation' => 0, |
226 ); |
277 ); |
227 |
278 |
228 // read iptc first, since it might contain data not available in exif such |
279 /* |
229 // as caption, description etc |
280 * Read IPTC first, since it might contain data not available in exif such |
|
281 * as caption, description etc. |
|
282 */ |
230 if ( is_callable( 'iptcparse' ) ) { |
283 if ( is_callable( 'iptcparse' ) ) { |
231 getimagesize( $file, $info ); |
284 getimagesize( $file, $info ); |
232 |
285 |
233 if ( ! empty( $info['APP13'] ) ) { |
286 if ( ! empty( $info['APP13'] ) ) { |
234 $iptc = iptcparse( $info['APP13'] ); |
287 $iptc = iptcparse( $info['APP13'] ); |
235 |
288 |
236 // headline, "A brief synopsis of the caption." |
289 // Headline, "A brief synopsis of the caption." |
237 if ( ! empty( $iptc['2#105'][0] ) ) |
290 if ( ! empty( $iptc['2#105'][0] ) ) { |
238 $meta['title'] = trim( $iptc['2#105'][0] ); |
291 $meta['title'] = trim( $iptc['2#105'][0] ); |
239 // title, "Many use the Title field to store the filename of the image, though the field may be used in many ways." |
292 /* |
240 elseif ( ! empty( $iptc['2#005'][0] ) ) |
293 * Title, "Many use the Title field to store the filename of the image, |
|
294 * though the field may be used in many ways." |
|
295 */ |
|
296 } elseif ( ! empty( $iptc['2#005'][0] ) ) { |
241 $meta['title'] = trim( $iptc['2#005'][0] ); |
297 $meta['title'] = trim( $iptc['2#005'][0] ); |
|
298 } |
242 |
299 |
243 if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption |
300 if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption |
244 $caption = trim( $iptc['2#120'][0] ); |
301 $caption = trim( $iptc['2#120'][0] ); |
245 if ( empty( $meta['title'] ) ) { |
302 |
|
303 mbstring_binary_safe_encoding(); |
|
304 $caption_length = strlen( $caption ); |
|
305 reset_mbstring_encoding(); |
|
306 |
|
307 if ( empty( $meta['title'] ) && $caption_length < 80 ) { |
246 // Assume the title is stored in 2:120 if it's short. |
308 // Assume the title is stored in 2:120 if it's short. |
247 if ( strlen( $caption ) < 80 ) |
309 $meta['title'] = $caption; |
248 $meta['title'] = $caption; |
|
249 else |
|
250 $meta['caption'] = $caption; |
|
251 } elseif ( $caption != $meta['title'] ) { |
|
252 $meta['caption'] = $caption; |
|
253 } |
310 } |
|
311 |
|
312 $meta['caption'] = $caption; |
254 } |
313 } |
255 |
314 |
256 if ( ! empty( $iptc['2#110'][0] ) ) // credit |
315 if ( ! empty( $iptc['2#110'][0] ) ) // credit |
257 $meta['credit'] = trim( $iptc['2#110'][0] ); |
316 $meta['credit'] = trim( $iptc['2#110'][0] ); |
258 elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline |
317 elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline |
259 $meta['credit'] = trim( $iptc['2#080'][0] ); |
318 $meta['credit'] = trim( $iptc['2#080'][0] ); |
260 |
319 |
261 if ( ! empty( $iptc['2#055'][0] ) and ! empty( $iptc['2#060'][0] ) ) // created date and time |
320 if ( ! empty( $iptc['2#055'][0] ) && ! empty( $iptc['2#060'][0] ) ) // created date and time |
262 $meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] ); |
321 $meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] ); |
263 |
322 |
264 if ( ! empty( $iptc['2#116'][0] ) ) // copyright |
323 if ( ! empty( $iptc['2#116'][0] ) ) // copyright |
265 $meta['copyright'] = trim( $iptc['2#116'][0] ); |
324 $meta['copyright'] = trim( $iptc['2#116'][0] ); |
266 } |
325 } |
267 } |
326 } |
268 |
327 |
269 // fetch additional info from exif if available |
328 /** |
|
329 * Filter the image types to check for exif data. |
|
330 * |
|
331 * @since 2.5.0 |
|
332 * |
|
333 * @param array $image_types Image types to check for exif data. |
|
334 */ |
270 if ( is_callable( 'exif_read_data' ) && in_array( $sourceImageType, apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) ) ) ) { |
335 if ( is_callable( 'exif_read_data' ) && in_array( $sourceImageType, apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) ) ) ) { |
271 $exif = @exif_read_data( $file ); |
336 $exif = @exif_read_data( $file ); |
272 |
337 |
273 if ( !empty( $exif['Title'] ) ) |
|
274 $meta['title'] = trim( $exif['Title'] ); |
|
275 |
|
276 if ( ! empty( $exif['ImageDescription'] ) ) { |
338 if ( ! empty( $exif['ImageDescription'] ) ) { |
277 if ( empty( $meta['title'] ) && strlen( $exif['ImageDescription'] ) < 80 ) { |
339 mbstring_binary_safe_encoding(); |
|
340 $description_length = strlen( $exif['ImageDescription'] ); |
|
341 reset_mbstring_encoding(); |
|
342 |
|
343 if ( empty( $meta['title'] ) && $description_length < 80 ) { |
278 // Assume the title is stored in ImageDescription |
344 // Assume the title is stored in ImageDescription |
279 $meta['title'] = trim( $exif['ImageDescription'] ); |
345 $meta['title'] = trim( $exif['ImageDescription'] ); |
280 if ( ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] ) |
346 } |
281 $meta['caption'] = trim( $exif['COMPUTED']['UserComment'] ); |
347 |
282 } elseif ( trim( $exif['ImageDescription'] ) != $meta['title'] ) { |
348 if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) ) { |
|
349 $meta['caption'] = trim( $exif['COMPUTED']['UserComment'] ); |
|
350 } |
|
351 |
|
352 if ( empty( $meta['caption'] ) ) { |
283 $meta['caption'] = trim( $exif['ImageDescription'] ); |
353 $meta['caption'] = trim( $exif['ImageDescription'] ); |
284 } |
354 } |
285 } elseif ( ! empty( $exif['Comments'] ) && trim( $exif['Comments'] ) != $meta['title'] ) { |
355 } elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) ) { |
286 $meta['caption'] = trim( $exif['Comments'] ); |
356 $meta['caption'] = trim( $exif['Comments'] ); |
287 } |
357 } |
288 |
358 |
289 if ( ! empty( $exif['Artist'] ) ) |
359 if ( empty( $meta['credit'] ) ) { |
290 $meta['credit'] = trim( $exif['Artist'] ); |
360 if ( ! empty( $exif['Artist'] ) ) { |
291 elseif ( ! empty($exif['Author'] ) ) |
361 $meta['credit'] = trim( $exif['Artist'] ); |
292 $meta['credit'] = trim( $exif['Author'] ); |
362 } elseif ( ! empty($exif['Author'] ) ) { |
293 |
363 $meta['credit'] = trim( $exif['Author'] ); |
294 if ( ! empty( $exif['Copyright'] ) ) |
364 } |
|
365 } |
|
366 |
|
367 if ( empty( $meta['copyright'] ) && ! empty( $exif['Copyright'] ) ) { |
295 $meta['copyright'] = trim( $exif['Copyright'] ); |
368 $meta['copyright'] = trim( $exif['Copyright'] ); |
296 if ( ! empty($exif['FNumber'] ) ) |
369 } |
|
370 if ( ! empty( $exif['FNumber'] ) ) { |
297 $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 ); |
371 $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 ); |
298 if ( ! empty($exif['Model'] ) ) |
372 } |
|
373 if ( ! empty( $exif['Model'] ) ) { |
299 $meta['camera'] = trim( $exif['Model'] ); |
374 $meta['camera'] = trim( $exif['Model'] ); |
300 if ( ! empty($exif['DateTimeDigitized'] ) ) |
375 } |
301 $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized'] ); |
376 if ( empty( $meta['created_timestamp'] ) && ! empty( $exif['DateTimeDigitized'] ) ) { |
302 if ( ! empty($exif['FocalLength'] ) ) |
377 $meta['created_timestamp'] = wp_exif_date2ts( $exif['DateTimeDigitized'] ); |
|
378 } |
|
379 if ( ! empty( $exif['FocalLength'] ) ) { |
303 $meta['focal_length'] = (string) wp_exif_frac2dec( $exif['FocalLength'] ); |
380 $meta['focal_length'] = (string) wp_exif_frac2dec( $exif['FocalLength'] ); |
304 if ( ! empty($exif['ISOSpeedRatings'] ) ) { |
381 } |
|
382 if ( ! empty( $exif['ISOSpeedRatings'] ) ) { |
305 $meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings']; |
383 $meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings']; |
306 $meta['iso'] = trim( $meta['iso'] ); |
384 $meta['iso'] = trim( $meta['iso'] ); |
307 } |
385 } |
308 if ( ! empty($exif['ExposureTime'] ) ) |
386 if ( ! empty( $exif['ExposureTime'] ) ) { |
309 $meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] ); |
387 $meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] ); |
|
388 } |
|
389 if ( ! empty( $exif['Orientation'] ) ) { |
|
390 $meta['orientation'] = $exif['Orientation']; |
|
391 } |
310 } |
392 } |
311 |
393 |
312 foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) { |
394 foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) { |
313 if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) ) |
395 if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) ) { |
314 $meta[ $key ] = utf8_encode( $meta[ $key ] ); |
396 $meta[ $key ] = utf8_encode( $meta[ $key ] ); |
315 } |
397 } |
316 |
398 } |
|
399 |
|
400 foreach ( $meta as &$value ) { |
|
401 if ( is_string( $value ) ) { |
|
402 $value = wp_kses_post( $value ); |
|
403 } |
|
404 } |
|
405 |
|
406 /** |
|
407 * Filter the array of meta data read from an image's exif data. |
|
408 * |
|
409 * @since 2.5.0 |
|
410 * |
|
411 * @param array $meta Image meta data. |
|
412 * @param string $file Path to image file. |
|
413 * @param int $sourceImageType Type of image. |
|
414 */ |
317 return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType ); |
415 return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType ); |
318 |
416 |
319 } |
417 } |
320 |
418 |
321 /** |
419 /** |
407 function _load_image_to_edit_path( $attachment_id, $size = 'full' ) { |
524 function _load_image_to_edit_path( $attachment_id, $size = 'full' ) { |
408 $filepath = get_attached_file( $attachment_id ); |
525 $filepath = get_attached_file( $attachment_id ); |
409 |
526 |
410 if ( $filepath && file_exists( $filepath ) ) { |
527 if ( $filepath && file_exists( $filepath ) ) { |
411 if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) { |
528 if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) { |
|
529 /** |
|
530 * Filter the path to the current image. |
|
531 * |
|
532 * The filter is evaluated for all image sizes except 'full'. |
|
533 * |
|
534 * @since 3.1.0 |
|
535 * |
|
536 * @param string $path Path to the current image. |
|
537 * @param string $attachment_id Attachment ID. |
|
538 * @param string $size Size of the image. |
|
539 */ |
412 $filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size ); |
540 $filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size ); |
413 } |
541 } |
414 } elseif ( function_exists( 'fopen' ) && function_exists( 'ini_get' ) && true == ini_get( 'allow_url_fopen' ) ) { |
542 } elseif ( function_exists( 'fopen' ) && function_exists( 'ini_get' ) && true == ini_get( 'allow_url_fopen' ) ) { |
|
543 /** |
|
544 * Filter the image URL if not in the local filesystem. |
|
545 * |
|
546 * The filter is only evaluated if fopen is enabled on the server. |
|
547 * |
|
548 * @since 3.1.0 |
|
549 * |
|
550 * @param string $image_url Current image URL. |
|
551 * @param string $attachment_id Attachment ID. |
|
552 * @param string $size Size of the image. |
|
553 */ |
415 $filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size ); |
554 $filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size ); |
416 } |
555 } |
417 |
556 |
|
557 /** |
|
558 * Filter the returned path or URL of the current image. |
|
559 * |
|
560 * @since 2.9.0 |
|
561 * |
|
562 * @param string|bool $filepath File path or URL to current image, or false. |
|
563 * @param string $attachment_id Attachment ID. |
|
564 * @param string $size Size of the image. |
|
565 */ |
418 return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size ); |
566 return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size ); |
419 } |
567 } |
420 |
568 |
421 /** |
569 /** |
422 * Copy an existing image file. |
570 * Copy an existing image file. |