wp_generate_attachment_metadata

Generate post thumbnail attachment meta data.

Description

wp_generate_attachment_metadata( (int) $attachment_id, (string) $file ); 

Parameters (2)

0. $attachment_id (int)
Attachment Id to process.
1. $file (string)
Filepath of the Attached image.

Usage

  1. if ( !function_exists( 'wp_generate_attachment_metadata' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/image.php'; 
  3.  
  4. // Attachment Id to process. 
  5. $attachment_id = -1; 
  6.  
  7. // Filepath of the Attached image. 
  8. $file = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_generate_attachment_metadata($attachment_id, $file); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/image.php  
  1. function wp_generate_attachment_metadata( $attachment_id, $file ) { 
  2. $attachment = get_post( $attachment_id ); 
  3.  
  4. $metadata = array(); 
  5. $support = false; 
  6. $mime_type = get_post_mime_type( $attachment ); 
  7.  
  8. if ( preg_match( '!^image/!', $mime_type ) && file_is_displayable_image( $file ) ) { 
  9. $imagesize = getimagesize( $file ); 
  10. $metadata['width'] = $imagesize[0]; 
  11. $metadata['height'] = $imagesize[1]; 
  12.  
  13. // Make the file path relative to the upload dir. 
  14. $metadata['file'] = _wp_relative_upload_path($file); 
  15.  
  16. // Make thumbnails and other intermediate sizes. 
  17. $_wp_additional_image_sizes = wp_get_additional_image_sizes(); 
  18.  
  19. $sizes = array(); 
  20. foreach ( get_intermediate_image_sizes() as $s ) { 
  21. $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false ); 
  22. if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) { 
  23. // For theme-added sizes 
  24. $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); 
  25. } else { 
  26. // For default sizes set in options 
  27. $sizes[$s]['width'] = get_option( "{$s}_size_w" ); 
  28.  
  29. if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) { 
  30. // For theme-added sizes 
  31. $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); 
  32. } else { 
  33. // For default sizes set in options 
  34. $sizes[$s]['height'] = get_option( "{$s}_size_h" ); 
  35.  
  36. if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) { 
  37. // For theme-added sizes 
  38. $sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop']; 
  39. } else { 
  40. // For default sizes set in options 
  41. $sizes[$s]['crop'] = get_option( "{$s}_crop" ); 
  42.  
  43. /** 
  44. * Filters the image sizes automatically generated when uploading an image. 
  45. * @since 2.9.0 
  46. * @since 4.4.0 Added the `$metadata` argument. 
  47. * @param array $sizes An associative array of image sizes. 
  48. * @param array $metadata An associative array of image metadata: width, height, file. 
  49. */ 
  50. $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes, $metadata ); 
  51.  
  52. if ( $sizes ) { 
  53. $editor = wp_get_image_editor( $file ); 
  54.  
  55. if ( ! is_wp_error( $editor ) ) 
  56. $metadata['sizes'] = $editor->multi_resize( $sizes ); 
  57. } else { 
  58. $metadata['sizes'] = array(); 
  59.  
  60. // Fetch additional metadata from EXIF/IPTC. 
  61. $image_meta = wp_read_image_metadata( $file ); 
  62. if ( $image_meta ) 
  63. $metadata['image_meta'] = $image_meta; 
  64.  
  65. } elseif ( wp_attachment_is( 'video', $attachment ) ) { 
  66. $metadata = wp_read_video_metadata( $file ); 
  67. $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' ); 
  68. } elseif ( wp_attachment_is( 'audio', $attachment ) ) { 
  69. $metadata = wp_read_audio_metadata( $file ); 
  70. $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' ); 
  71.  
  72. if ( $support && ! empty( $metadata['image']['data'] ) ) { 
  73. // Check for existing cover. 
  74. $hash = md5( $metadata['image']['data'] ); 
  75. $posts = get_posts( array( 
  76. 'fields' => 'ids',  
  77. 'post_type' => 'attachment',  
  78. 'post_mime_type' => $metadata['image']['mime'],  
  79. 'post_status' => 'inherit',  
  80. 'posts_per_page' => 1,  
  81. 'meta_key' => '_cover_hash',  
  82. 'meta_value' => $hash 
  83. ) ); 
  84. $exists = reset( $posts ); 
  85.  
  86. if ( ! empty( $exists ) ) { 
  87. update_post_meta( $attachment_id, '_thumbnail_id', $exists ); 
  88. } else { 
  89. $ext = '.jpg'; 
  90. switch ( $metadata['image']['mime'] ) { 
  91. case 'image/gif': 
  92. $ext = '.gif'; 
  93. break; 
  94. case 'image/png': 
  95. $ext = '.png'; 
  96. break; 
  97. $basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext; 
  98. $uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] ); 
  99. if ( false === $uploaded[error] ) { 
  100. $image_attachment = array( 
  101. 'post_mime_type' => $metadata['image']['mime'],  
  102. 'post_type' => 'attachment',  
  103. 'post_content' => '',  
  104. ); 
  105. /** 
  106. * Filters the parameters for the attachment thumbnail creation. 
  107. * @since 3.9.0 
  108. * @param array $image_attachment An array of parameters to create the thumbnail. 
  109. * @param array $metadata Current attachment metadata. 
  110. * @param array $uploaded An array containing the thumbnail path and url. 
  111. */ 
  112. $image_attachment = apply_filters( 'attachment_thumbnail_args', $image_attachment, $metadata, $uploaded ); 
  113.  
  114. $sub_attachment_id = wp_insert_attachment( $image_attachment, $uploaded['file'] ); 
  115. add_post_meta( $sub_attachment_id, '_cover_hash', $hash ); 
  116. $attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] ); 
  117. wp_update_attachment_metadata( $sub_attachment_id, $attach_data ); 
  118. update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id ); 
  119. // Try to create image thumbnails for PDFs 
  120. else if ( 'application/pdf' === $mime_type ) { 
  121. $fallback_sizes = array( 
  122. 'thumbnail',  
  123. 'medium',  
  124. 'large',  
  125. ); 
  126.  
  127. /** 
  128. * Filters the image sizes generated for non-image mime types. 
  129. * @since 4.7.0 
  130. * @param array $fallback_sizes An array of image size names. 
  131. */ 
  132. $fallback_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata ); 
  133.  
  134. $sizes = array(); 
  135. $_wp_additional_image_sizes = wp_get_additional_image_sizes(); 
  136.  
  137. foreach ( $fallback_sizes as $s ) { 
  138. if ( isset( $_wp_additional_image_sizes[ $s ]['width'] ) ) { 
  139. $sizes[ $s ]['width'] = intval( $_wp_additional_image_sizes[ $s ]['width'] ); 
  140. } else { 
  141. $sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); 
  142.  
  143. if ( isset( $_wp_additional_image_sizes[ $s ]['height'] ) ) { 
  144. $sizes[ $s ]['height'] = intval( $_wp_additional_image_sizes[ $s ]['height'] ); 
  145. } else { 
  146. $sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); 
  147.  
  148. if ( isset( $_wp_additional_image_sizes[ $s ]['crop'] ) ) { 
  149. $sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop']; 
  150. } else { 
  151. // Force thumbnails to be soft crops. 
  152. if ( ! 'thumbnail' === $s ) { 
  153. $sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); 
  154.  
  155. // Only load PDFs in an image editor if we're processing sizes. 
  156. if ( ! empty( $sizes ) ) { 
  157. $editor = wp_get_image_editor( $file ); 
  158.  
  159. if ( ! is_wp_error( $editor ) ) { // No support for this type of file 
  160. /** 
  161. * PDFs may have the same file filename as JPEGs. 
  162. * Ensure the PDF preview image does not overwrite any JPEG images that already exist. 
  163. */ 
  164. $dirname = dirname( $file ) . '/'; 
  165. $ext = '.' . pathinfo( $file, PATHINFO_EXTENSION ); 
  166. $preview_file = $dirname . wp_unique_filename( $dirname, wp_basename( $file, $ext ) . '-pdf.jpg' ); 
  167.  
  168. $uploaded = $editor->save( $preview_file, 'image/jpeg' ); 
  169. unset( $editor ); 
  170.  
  171. // Resize based on the full size image, rather than the source. 
  172. if ( ! is_wp_error( $uploaded ) ) { 
  173. $editor = wp_get_image_editor( $uploaded['path'] ); 
  174. unset( $uploaded['path'] ); 
  175.  
  176. if ( ! is_wp_error( $editor ) ) { 
  177. $metadata['sizes'] = $editor->multi_resize( $sizes ); 
  178. $metadata['sizes']['full'] = $uploaded; 
  179.  
  180. // Remove the blob of binary data from the array. 
  181. if ( $metadata ) { 
  182. unset( $metadata['image']['data'] ); 
  183.  
  184. /** 
  185. * Filters the generated attachment meta data. 
  186. * @since 2.1.0 
  187. * @param array $metadata An array of attachment meta data. 
  188. * @param int $attachment_id Current attachment ID. 
  189. */ 
  190. return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );