PageRenderTime 48ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/wp-includes/media.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 1393 lines | 542 code | 158 blank | 693 comment | 152 complexity | c0bf64cf8fa10540a6659e41bb68f501 MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress API for media display.
  4. *
  5. * @package WordPress
  6. * @subpackage Media
  7. */
  8. /**
  9. * Retrieve additional image sizes.
  10. *
  11. * @since 4.7.0
  12. *
  13. * @global array $_wp_additional_image_sizes
  14. *
  15. * @return array Additional images size data.
  16. */
  17. function wp_get_additional_image_sizes() {
  18. global $_wp_additional_image_sizes;
  19. if ( ! $_wp_additional_image_sizes ) {
  20. $_wp_additional_image_sizes = array();
  21. }
  22. return $_wp_additional_image_sizes;
  23. }
  24. /**
  25. * Scale down the default size of an image.
  26. *
  27. * This is so that the image is a better fit for the editor and theme.
  28. *
  29. * The `$size` parameter accepts either an array or a string. The supported string
  30. * values are 'thumb' or 'thumbnail' for the given thumbnail size or defaults at
  31. * 128 width and 96 height in pixels. Also supported for the string value is
  32. * 'medium', 'medium_large' and 'full'. The 'full' isn't actually supported, but any value other
  33. * than the supported will result in the content_width size or 500 if that is
  34. * not set.
  35. *
  36. * Finally, there is a filter named {@see 'editor_max_image_size'}, that will be
  37. * called on the calculated array for width and height, respectively.
  38. *
  39. * @since 2.5.0
  40. *
  41. * @global int $content_width
  42. *
  43. * @param int $width Width of the image in pixels.
  44. * @param int $height Height of the image in pixels.
  45. * @param string|array $size Optional. Image size. Accepts any valid image size, or an array
  46. * of width and height values in pixels (in that order).
  47. * Default 'medium'.
  48. * @param string $context Optional. Could be 'display' (like in a theme) or 'edit'
  49. * (like inserting into an editor). Default null.
  50. * @return int[] {
  51. * An array of width and height values.
  52. *
  53. * @type int $0 The maximum width in pixels.
  54. * @type int $1 The maximum height in pixels.
  55. * }
  56. */
  57. function image_constrain_size_for_editor( $width, $height, $size = 'medium', $context = null ) {
  58. global $content_width;
  59. $_wp_additional_image_sizes = wp_get_additional_image_sizes();
  60. if ( ! $context ) {
  61. $context = is_admin() ? 'edit' : 'display';
  62. }
  63. if ( is_array( $size ) ) {
  64. $max_width = $size[0];
  65. $max_height = $size[1];
  66. } elseif ( 'thumb' === $size || 'thumbnail' === $size ) {
  67. $max_width = intval( get_option( 'thumbnail_size_w' ) );
  68. $max_height = intval( get_option( 'thumbnail_size_h' ) );
  69. // Last chance thumbnail size defaults.
  70. if ( ! $max_width && ! $max_height ) {
  71. $max_width = 128;
  72. $max_height = 96;
  73. }
  74. } elseif ( 'medium' === $size ) {
  75. $max_width = intval( get_option( 'medium_size_w' ) );
  76. $max_height = intval( get_option( 'medium_size_h' ) );
  77. } elseif ( 'medium_large' === $size ) {
  78. $max_width = intval( get_option( 'medium_large_size_w' ) );
  79. $max_height = intval( get_option( 'medium_large_size_h' ) );
  80. if ( intval( $content_width ) > 0 ) {
  81. $max_width = min( intval( $content_width ), $max_width );
  82. }
  83. } elseif ( 'large' === $size ) {
  84. /*
  85. * We're inserting a large size image into the editor. If it's a really
  86. * big image we'll scale it down to fit reasonably within the editor
  87. * itself, and within the theme's content width if it's known. The user
  88. * can resize it in the editor if they wish.
  89. */
  90. $max_width = intval( get_option( 'large_size_w' ) );
  91. $max_height = intval( get_option( 'large_size_h' ) );
  92. if ( intval( $content_width ) > 0 ) {
  93. $max_width = min( intval( $content_width ), $max_width );
  94. }
  95. } elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ), true ) ) {
  96. $max_width = intval( $_wp_additional_image_sizes[ $size ]['width'] );
  97. $max_height = intval( $_wp_additional_image_sizes[ $size ]['height'] );
  98. // Only in admin. Assume that theme authors know what they're doing.
  99. if ( intval( $content_width ) > 0 && 'edit' === $context ) {
  100. $max_width = min( intval( $content_width ), $max_width );
  101. }
  102. } else { // $size === 'full' has no constraint.
  103. $max_width = $width;
  104. $max_height = $height;
  105. }
  106. /**
  107. * Filters the maximum image size dimensions for the editor.
  108. *
  109. * @since 2.5.0
  110. *
  111. * @param int[] $max_image_size {
  112. * An array of width and height values.
  113. *
  114. * @type int $0 The maximum width in pixels.
  115. * @type int $1 The maximum height in pixels.
  116. * }
  117. * @param string|array $size Size of what the result image should be.
  118. * @param string $context The context the image is being resized for.
  119. * Possible values are 'display' (like in a theme)
  120. * or 'edit' (like inserting into an editor).
  121. */
  122. list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size, $context );
  123. return wp_constrain_dimensions( $width, $height, $max_width, $max_height );
  124. }
  125. /**
  126. * Retrieve width and height attributes using given width and height values.
  127. *
  128. * Both attributes are required in the sense that both parameters must have a
  129. * value, but are optional in that if you set them to false or null, then they
  130. * will not be added to the returned string.
  131. *
  132. * You can set the value using a string, but it will only take numeric values.
  133. * If you wish to put 'px' after the numbers, then it will be stripped out of
  134. * the return.
  135. *
  136. * @since 2.5.0
  137. *
  138. * @param int|string $width Image width in pixels.
  139. * @param int|string $height Image height in pixels.
  140. * @return string HTML attributes for width and, or height.
  141. */
  142. function image_hwstring( $width, $height ) {
  143. $out = '';
  144. if ( $width ) {
  145. $out .= 'width="' . intval( $width ) . '" ';
  146. }
  147. if ( $height ) {
  148. $out .= 'height="' . intval( $height ) . '" ';
  149. }
  150. return $out;
  151. }
  152. /**
  153. * Scale an image to fit a particular size (such as 'thumb' or 'medium').
  154. *
  155. * The URL might be the original image, or it might be a resized version. This
  156. * function won't create a new resized copy, it will just return an already
  157. * resized one if it exists.
  158. *
  159. * A plugin may use the {@see 'image_downsize'} filter to hook into and offer image
  160. * resizing services for images. The hook must return an array with the same
  161. * elements that are normally returned from the function.
  162. *
  163. * @since 2.5.0
  164. *
  165. * @param int $id Attachment ID for image.
  166. * @param string|int[] $size Optional. Image size to scale to. Accepts any valid image size name,
  167. * or an array of width and height values in pixels (in that order).
  168. * Default 'medium'.
  169. * @return array|false {
  170. * Array of image data, or boolean false if no image is available.
  171. *
  172. * @type string $0 Image source URL.
  173. * @type int $1 Image width in pixels.
  174. * @type int $2 Image height in pixels.
  175. * @type bool $3 Whether the image is a resized image.
  176. * }
  177. */
  178. function image_downsize( $id, $size = 'medium' ) {
  179. $is_image = wp_attachment_is_image( $id );
  180. /**
  181. * Filters whether to preempt the output of image_downsize().
  182. *
  183. * Returning a truthy value from the filter will effectively short-circuit
  184. * down-sizing the image, returning that value instead.
  185. *
  186. * @since 2.5.0
  187. *
  188. * @param bool|array $downsize Whether to short-circuit the image downsize.
  189. * @param int $id Attachment ID for image.
  190. * @param array|string $size Requested size of image. Image size name, or array of width
  191. * and height values (in that order).
  192. */
  193. $out = apply_filters( 'image_downsize', false, $id, $size );
  194. if ( $out ) {
  195. return $out;
  196. }
  197. $img_url = wp_get_attachment_url( $id );
  198. $meta = wp_get_attachment_metadata( $id );
  199. $width = 0;
  200. $height = 0;
  201. $is_intermediate = false;
  202. $img_url_basename = wp_basename( $img_url );
  203. // If the file isn't an image, attempt to replace its URL with a rendered image from its meta.
  204. // Otherwise, a non-image type could be returned.
  205. if ( ! $is_image ) {
  206. if ( ! empty( $meta['sizes']['full'] ) ) {
  207. $img_url = str_replace( $img_url_basename, $meta['sizes']['full']['file'], $img_url );
  208. $img_url_basename = $meta['sizes']['full']['file'];
  209. $width = $meta['sizes']['full']['width'];
  210. $height = $meta['sizes']['full']['height'];
  211. } else {
  212. return false;
  213. }
  214. }
  215. // Try for a new style intermediate size.
  216. $intermediate = image_get_intermediate_size( $id, $size );
  217. if ( $intermediate ) {
  218. $img_url = str_replace( $img_url_basename, $intermediate['file'], $img_url );
  219. $width = $intermediate['width'];
  220. $height = $intermediate['height'];
  221. $is_intermediate = true;
  222. } elseif ( 'thumbnail' === $size ) {
  223. // Fall back to the old thumbnail.
  224. $thumb_file = wp_get_attachment_thumb_file( $id );
  225. $info = null;
  226. if ( $thumb_file ) {
  227. $info = @getimagesize( $thumb_file );
  228. }
  229. if ( $thumb_file && $info ) {
  230. $img_url = str_replace( $img_url_basename, wp_basename( $thumb_file ), $img_url );
  231. $width = $info[0];
  232. $height = $info[1];
  233. $is_intermediate = true;
  234. }
  235. }
  236. if ( ! $width && ! $height && isset( $meta['width'], $meta['height'] ) ) {
  237. // Any other type: use the real image.
  238. $width = $meta['width'];
  239. $height = $meta['height'];
  240. }
  241. if ( $img_url ) {
  242. // We have the actual image size, but might need to further constrain it if content_width is narrower.
  243. list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size );
  244. return array( $img_url, $width, $height, $is_intermediate );
  245. }
  246. return false;
  247. }
  248. /**
  249. * Register a new image size.
  250. *
  251. * @since 2.9.0
  252. *
  253. * @global array $_wp_additional_image_sizes Associative array of additional image sizes.
  254. *
  255. * @param string $name Image size identifier.
  256. * @param int $width Optional. Image width in pixels. Default 0.
  257. * @param int $height Optional. Image height in pixels. Default 0.
  258. * @param bool|array $crop Optional. Image cropping behavior. If false, the image will be scaled (default),
  259. * If true, image will be cropped to the specified dimensions using center positions.
  260. * If an array, the image will be cropped using the array to specify the crop location.
  261. * Array values must be in the format: array( x_crop_position, y_crop_position ) where:
  262. * - x_crop_position accepts: 'left', 'center', or 'right'.
  263. * - y_crop_position accepts: 'top', 'center', or 'bottom'.
  264. */
  265. function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
  266. global $_wp_additional_image_sizes;
  267. $_wp_additional_image_sizes[ $name ] = array(
  268. 'width' => absint( $width ),
  269. 'height' => absint( $height ),
  270. 'crop' => $crop,
  271. );
  272. }
  273. /**
  274. * Check if an image size exists.
  275. *
  276. * @since 3.9.0
  277. *
  278. * @param string $name The image size to check.
  279. * @return bool True if the image size exists, false if not.
  280. */
  281. function has_image_size( $name ) {
  282. $sizes = wp_get_additional_image_sizes();
  283. return isset( $sizes[ $name ] );
  284. }
  285. /**
  286. * Remove a new image size.
  287. *
  288. * @since 3.9.0
  289. *
  290. * @global array $_wp_additional_image_sizes
  291. *
  292. * @param string $name The image size to remove.
  293. * @return bool True if the image size was successfully removed, false on failure.
  294. */
  295. function remove_image_size( $name ) {
  296. global $_wp_additional_image_sizes;
  297. if ( isset( $_wp_additional_image_sizes[ $name ] ) ) {
  298. unset( $_wp_additional_image_sizes[ $name ] );
  299. return true;
  300. }
  301. return false;
  302. }
  303. /**
  304. * Registers an image size for the post thumbnail.
  305. *
  306. * @since 2.9.0
  307. *
  308. * @see add_image_size() for details on cropping behavior.
  309. *
  310. * @param int $width Image width in pixels.
  311. * @param int $height Image height in pixels.
  312. * @param bool|array $crop Optional. Whether to crop images to specified width and height or resize.
  313. * An array can specify positioning of the crop area. Default false.
  314. */
  315. function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) {
  316. add_image_size( 'post-thumbnail', $width, $height, $crop );
  317. }
  318. /**
  319. * Gets an img tag for an image attachment, scaling it down if requested.
  320. *
  321. * The {@see 'get_image_tag_class'} filter allows for changing the class name for the
  322. * image without having to use regular expressions on the HTML content. The
  323. * parameters are: what WordPress will use for the class, the Attachment ID,
  324. * image align value, and the size the image should be.
  325. *
  326. * The second filter, {@see 'get_image_tag'}, has the HTML content, which can then be
  327. * further manipulated by a plugin to change all attribute values and even HTML
  328. * content.
  329. *
  330. * @since 2.5.0
  331. *
  332. * @param int $id Attachment ID.
  333. * @param string $alt Image description for the alt attribute.
  334. * @param string $title Image description for the title attribute.
  335. * @param string $align Part of the class name for aligning the image.
  336. * @param string|array $size Optional. Registered image size to retrieve a tag for. Accepts any
  337. * valid image size, or an array of width and height values in pixels
  338. * (in that order). Default 'medium'.
  339. * @return string HTML IMG element for given image attachment
  340. */
  341. function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) {
  342. list( $img_src, $width, $height ) = image_downsize( $id, $size );
  343. $hwstring = image_hwstring( $width, $height );
  344. $title = $title ? 'title="' . esc_attr( $title ) . '" ' : '';
  345. $class = 'align' . esc_attr( $align ) . ' size-' . esc_attr( $size ) . ' wp-image-' . $id;
  346. /**
  347. * Filters the value of the attachment's image tag class attribute.
  348. *
  349. * @since 2.6.0
  350. *
  351. * @param string $class CSS class name or space-separated list of classes.
  352. * @param int $id Attachment ID.
  353. * @param string $align Part of the class name for aligning the image.
  354. * @param string|array $size Size of image. Image size or array of width and height values (in that order).
  355. * Default 'medium'.
  356. */
  357. $class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size );
  358. $html = '<img src="' . esc_attr( $img_src ) . '" alt="' . esc_attr( $alt ) . '" ' . $title . $hwstring . 'class="' . $class . '" />';
  359. /**
  360. * Filters the HTML content for the image tag.
  361. *
  362. * @since 2.6.0
  363. *
  364. * @param string $html HTML content for the image.
  365. * @param int $id Attachment ID.
  366. * @param string $alt Image description for the alt attribute.
  367. * @param string $title Image description for the title attribute.
  368. * @param string $align Part of the class name for aligning the image.
  369. * @param string|array $size Size of image. Image size or array of width and height values (in that order).
  370. * Default 'medium'.
  371. */
  372. return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );
  373. }
  374. /**
  375. * Calculates the new dimensions for a down-sampled image.
  376. *
  377. * If either width or height are empty, no constraint is applied on
  378. * that dimension.
  379. *
  380. * @since 2.5.0
  381. *
  382. * @param int $current_width Current width of the image.
  383. * @param int $current_height Current height of the image.
  384. * @param int $max_width Optional. Max width in pixels to constrain to. Default 0.
  385. * @param int $max_height Optional. Max height in pixels to constrain to. Default 0.
  386. * @return int[] {
  387. * An array of width and height values.
  388. *
  389. * @type int $0 The width in pixels.
  390. * @type int $1 The height in pixels.
  391. * }
  392. */
  393. function wp_constrain_dimensions( $current_width, $current_height, $max_width = 0, $max_height = 0 ) {
  394. if ( ! $max_width && ! $max_height ) {
  395. return array( $current_width, $current_height );
  396. }
  397. $width_ratio = 1.0;
  398. $height_ratio = 1.0;
  399. $did_width = false;
  400. $did_height = false;
  401. if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) {
  402. $width_ratio = $max_width / $current_width;
  403. $did_width = true;
  404. }
  405. if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height ) {
  406. $height_ratio = $max_height / $current_height;
  407. $did_height = true;
  408. }
  409. // Calculate the larger/smaller ratios.
  410. $smaller_ratio = min( $width_ratio, $height_ratio );
  411. $larger_ratio = max( $width_ratio, $height_ratio );
  412. if ( (int) round( $current_width * $larger_ratio ) > $max_width || (int) round( $current_height * $larger_ratio ) > $max_height ) {
  413. // The larger ratio is too big. It would result in an overflow.
  414. $ratio = $smaller_ratio;
  415. } else {
  416. // The larger ratio fits, and is likely to be a more "snug" fit.
  417. $ratio = $larger_ratio;
  418. }
  419. // Very small dimensions may result in 0, 1 should be the minimum.
  420. $w = max( 1, (int) round( $current_width * $ratio ) );
  421. $h = max( 1, (int) round( $current_height * $ratio ) );
  422. /*
  423. * Sometimes, due to rounding, we'll end up with a result like this:
  424. * 465x700 in a 177x177 box is 117x176... a pixel short.
  425. * We also have issues with recursive calls resulting in an ever-changing result.
  426. * Constraining to the result of a constraint should yield the original result.
  427. * Thus we look for dimensions that are one pixel shy of the max value and bump them up.
  428. */
  429. // Note: $did_width means it is possible $smaller_ratio == $width_ratio.
  430. if ( $did_width && $w === $max_width - 1 ) {
  431. $w = $max_width; // Round it up.
  432. }
  433. // Note: $did_height means it is possible $smaller_ratio == $height_ratio.
  434. if ( $did_height && $h === $max_height - 1 ) {
  435. $h = $max_height; // Round it up.
  436. }
  437. /**
  438. * Filters dimensions to constrain down-sampled images to.
  439. *
  440. * @since 4.1.0
  441. *
  442. * @param int[] $dimensions {
  443. * An array of width and height values.
  444. *
  445. * @type int $0 The width in pixels.
  446. * @type int $1 The height in pixels.
  447. * }
  448. * @param int $current_width The current width of the image.
  449. * @param int $current_height The current height of the image.
  450. * @param int $max_width The maximum width permitted.
  451. * @param int $max_height The maximum height permitted.
  452. */
  453. return apply_filters( 'wp_constrain_dimensions', array( $w, $h ), $current_width, $current_height, $max_width, $max_height );
  454. }
  455. /**
  456. * Retrieves calculated resize dimensions for use in WP_Image_Editor.
  457. *
  458. * Calculates dimensions and coordinates for a resized image that fits
  459. * within a specified width and height.
  460. *
  461. * Cropping behavior is dependent on the value of $crop:
  462. * 1. If false (default), images will not be cropped.
  463. * 2. If an array in the form of array( x_crop_position, y_crop_position ):
  464. * - x_crop_position accepts 'left' 'center', or 'right'.
  465. * - y_crop_position accepts 'top', 'center', or 'bottom'.
  466. * Images will be cropped to the specified dimensions within the defined crop area.
  467. * 3. If true, images will be cropped to the specified dimensions using center positions.
  468. *
  469. * @since 2.5.0
  470. *
  471. * @param int $orig_w Original width in pixels.
  472. * @param int $orig_h Original height in pixels.
  473. * @param int $dest_w New width in pixels.
  474. * @param int $dest_h New height in pixels.
  475. * @param bool|array $crop Optional. Whether to crop image to specified width and height or resize.
  476. * An array can specify positioning of the crop area. Default false.
  477. * @return array|false Returned array matches parameters for `imagecopyresampled()`. False on failure.
  478. */
  479. function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = false ) {
  480. if ( $orig_w <= 0 || $orig_h <= 0 ) {
  481. return false;
  482. }
  483. // At least one of $dest_w or $dest_h must be specific.
  484. if ( $dest_w <= 0 && $dest_h <= 0 ) {
  485. return false;
  486. }
  487. /**
  488. * Filters whether to preempt calculating the image resize dimensions.
  489. *
  490. * Passing a non-null value to the filter will effectively short-circuit
  491. * image_resize_dimensions(), returning that value instead.
  492. *
  493. * @since 3.4.0
  494. *
  495. * @param null|mixed $null Whether to preempt output of the resize dimensions.
  496. * @param int $orig_w Original width in pixels.
  497. * @param int $orig_h Original height in pixels.
  498. * @param int $dest_w New width in pixels.
  499. * @param int $dest_h New height in pixels.
  500. * @param bool|array $crop Whether to crop image to specified width and height or resize.
  501. * An array can specify positioning of the crop area. Default false.
  502. */
  503. $output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop );
  504. if ( null !== $output ) {
  505. return $output;
  506. }
  507. // Stop if the destination size is larger than the original image dimensions.
  508. if ( empty( $dest_h ) ) {
  509. if ( $orig_w < $dest_w ) {
  510. return false;
  511. }
  512. } elseif ( empty( $dest_w ) ) {
  513. if ( $orig_h < $dest_h ) {
  514. return false;
  515. }
  516. } else {
  517. if ( $orig_w < $dest_w && $orig_h < $dest_h ) {
  518. return false;
  519. }
  520. }
  521. if ( $crop ) {
  522. /*
  523. * Crop the largest possible portion of the original image that we can size to $dest_w x $dest_h.
  524. * Note that the requested crop dimensions are used as a maximum bounding box for the original image.
  525. * If the original image's width or height is less than the requested width or height
  526. * only the greater one will be cropped.
  527. * For example when the original image is 600x300, and the requested crop dimensions are 400x400,
  528. * the resulting image will be 400x300.
  529. */
  530. $aspect_ratio = $orig_w / $orig_h;
  531. $new_w = min( $dest_w, $orig_w );
  532. $new_h = min( $dest_h, $orig_h );
  533. if ( ! $new_w ) {
  534. $new_w = (int) round( $new_h * $aspect_ratio );
  535. }
  536. if ( ! $new_h ) {
  537. $new_h = (int) round( $new_w / $aspect_ratio );
  538. }
  539. $size_ratio = max( $new_w / $orig_w, $new_h / $orig_h );
  540. $crop_w = round( $new_w / $size_ratio );
  541. $crop_h = round( $new_h / $size_ratio );
  542. if ( ! is_array( $crop ) || count( $crop ) !== 2 ) {
  543. $crop = array( 'center', 'center' );
  544. }
  545. list( $x, $y ) = $crop;
  546. if ( 'left' === $x ) {
  547. $s_x = 0;
  548. } elseif ( 'right' === $x ) {
  549. $s_x = $orig_w - $crop_w;
  550. } else {
  551. $s_x = floor( ( $orig_w - $crop_w ) / 2 );
  552. }
  553. if ( 'top' === $y ) {
  554. $s_y = 0;
  555. } elseif ( 'bottom' === $y ) {
  556. $s_y = $orig_h - $crop_h;
  557. } else {
  558. $s_y = floor( ( $orig_h - $crop_h ) / 2 );
  559. }
  560. } else {
  561. // Resize using $dest_w x $dest_h as a maximum bounding box.
  562. $crop_w = $orig_w;
  563. $crop_h = $orig_h;
  564. $s_x = 0;
  565. $s_y = 0;
  566. list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
  567. }
  568. if ( wp_fuzzy_number_match( $new_w, $orig_w ) && wp_fuzzy_number_match( $new_h, $orig_h ) ) {
  569. // The new size has virtually the same dimensions as the original image.
  570. /**
  571. * Filters whether to proceed with making an image sub-size with identical dimensions
  572. * with the original/source image. Differences of 1px may be due to rounding and are ignored.
  573. *
  574. * @since 5.3.0
  575. *
  576. * @param bool $proceed The filtered value.
  577. * @param int $orig_w Original image width.
  578. * @param int $orig_h Original image height.
  579. */
  580. $proceed = (bool) apply_filters( 'wp_image_resize_identical_dimensions', false, $orig_w, $orig_h );
  581. if ( ! $proceed ) {
  582. return false;
  583. }
  584. }
  585. // The return array matches the parameters to imagecopyresampled().
  586. // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
  587. return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
  588. }
  589. /**
  590. * Resizes an image to make a thumbnail or intermediate size.
  591. *
  592. * The returned array has the file size, the image width, and image height. The
  593. * {@see 'image_make_intermediate_size'} filter can be used to hook in and change the
  594. * values of the returned array. The only parameter is the resized file path.
  595. *
  596. * @since 2.5.0
  597. *
  598. * @param string $file File path.
  599. * @param int $width Image width.
  600. * @param int $height Image height.
  601. * @param bool $crop Optional. Whether to crop image to specified width and height or resize.
  602. * Default false.
  603. * @return array|false Metadata array on success. False if no image was created.
  604. */
  605. function image_make_intermediate_size( $file, $width, $height, $crop = false ) {
  606. if ( $width || $height ) {
  607. $editor = wp_get_image_editor( $file );
  608. if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) ) {
  609. return false;
  610. }
  611. $resized_file = $editor->save();
  612. if ( ! is_wp_error( $resized_file ) && $resized_file ) {
  613. unset( $resized_file['path'] );
  614. return $resized_file;
  615. }
  616. }
  617. return false;
  618. }
  619. /**
  620. * Helper function to test if aspect ratios for two images match.
  621. *
  622. * @since 4.6.0
  623. *
  624. * @param int $source_width Width of the first image in pixels.
  625. * @param int $source_height Height of the first image in pixels.
  626. * @param int $target_width Width of the second image in pixels.
  627. * @param int $target_height Height of the second image in pixels.
  628. * @return bool True if aspect ratios match within 1px. False if not.
  629. */
  630. function wp_image_matches_ratio( $source_width, $source_height, $target_width, $target_height ) {
  631. /*
  632. * To test for varying crops, we constrain the dimensions of the larger image
  633. * to the dimensions of the smaller image and see if they match.
  634. */
  635. if ( $source_width > $target_width ) {
  636. $constrained_size = wp_constrain_dimensions( $source_width, $source_height, $target_width );
  637. $expected_size = array( $target_width, $target_height );
  638. } else {
  639. $constrained_size = wp_constrain_dimensions( $target_width, $target_height, $source_width );
  640. $expected_size = array( $source_width, $source_height );
  641. }
  642. // If the image dimensions are within 1px of the expected size, we consider it a match.
  643. $matched = ( wp_fuzzy_number_match( $constrained_size[0], $expected_size[0] ) && wp_fuzzy_number_match( $constrained_size[1], $expected_size[1] ) );
  644. return $matched;
  645. }
  646. /**
  647. * Retrieves the image's intermediate size (resized) path, width, and height.
  648. *
  649. * The $size parameter can be an array with the width and height respectively.
  650. * If the size matches the 'sizes' metadata array for width and height, then it
  651. * will be used. If there is no direct match, then the nearest image size larger
  652. * than the specified size will be used. If nothing is found, then the function
  653. * will break out and return false.
  654. *
  655. * The metadata 'sizes' is used for compatible sizes that can be used for the
  656. * parameter $size value.
  657. *
  658. * The url path will be given, when the $size parameter is a string.
  659. *
  660. * If you are passing an array for the $size, you should consider using
  661. * add_image_size() so that a cropped version is generated. It's much more
  662. * efficient than having to find the closest-sized image and then having the
  663. * browser scale down the image.
  664. *
  665. * @since 2.5.0
  666. *
  667. * @param int $post_id Attachment ID.
  668. * @param array|string $size Optional. Image size. Accepts any valid image size, or an array
  669. * of width and height values in pixels (in that order).
  670. * Default 'thumbnail'.
  671. * @return array|false $data {
  672. * Array of file relative path, width, and height on success. Additionally includes absolute
  673. * path and URL if registered size is passed to $size parameter. False on failure.
  674. *
  675. * @type string $file Image's path relative to uploads directory
  676. * @type int $width Width of image
  677. * @type int $height Height of image
  678. * @type string $path Image's absolute filesystem path.
  679. * @type string $url Image's URL.
  680. * }
  681. */
  682. function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) {
  683. $imagedata = wp_get_attachment_metadata( $post_id );
  684. if ( ! $size || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) {
  685. return false;
  686. }
  687. $data = array();
  688. // Find the best match when '$size' is an array.
  689. if ( is_array( $size ) ) {
  690. $candidates = array();
  691. if ( ! isset( $imagedata['file'] ) && isset( $imagedata['sizes']['full'] ) ) {
  692. $imagedata['height'] = $imagedata['sizes']['full']['height'];
  693. $imagedata['width'] = $imagedata['sizes']['full']['width'];
  694. }
  695. foreach ( $imagedata['sizes'] as $_size => $data ) {
  696. // If there's an exact match to an existing image size, short circuit.
  697. if ( intval( $data['width'] ) === intval( $size[0] ) && intval( $data['height'] ) === intval( $size[1] ) ) {
  698. $candidates[ $data['width'] * $data['height'] ] = $data;
  699. break;
  700. }
  701. // If it's not an exact match, consider larger sizes with the same aspect ratio.
  702. if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) {
  703. // If '0' is passed to either size, we test ratios against the original file.
  704. if ( 0 === $size[0] || 0 === $size[1] ) {
  705. $same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $imagedata['width'], $imagedata['height'] );
  706. } else {
  707. $same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $size[0], $size[1] );
  708. }
  709. if ( $same_ratio ) {
  710. $candidates[ $data['width'] * $data['height'] ] = $data;
  711. }
  712. }
  713. }
  714. if ( ! empty( $candidates ) ) {
  715. // Sort the array by size if we have more than one candidate.
  716. if ( 1 < count( $candidates ) ) {
  717. ksort( $candidates );
  718. }
  719. $data = array_shift( $candidates );
  720. /*
  721. * When the size requested is smaller than the thumbnail dimensions, we
  722. * fall back to the thumbnail size to maintain backward compatibility with
  723. * pre 4.6 versions of WordPress.
  724. */
  725. } elseif ( ! empty( $imagedata['sizes']['thumbnail'] ) && $imagedata['sizes']['thumbnail']['width'] >= $size[0] && $imagedata['sizes']['thumbnail']['width'] >= $size[1] ) {
  726. $data = $imagedata['sizes']['thumbnail'];
  727. } else {
  728. return false;
  729. }
  730. // Constrain the width and height attributes to the requested values.
  731. list( $data['width'], $data['height'] ) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
  732. } elseif ( ! empty( $imagedata['sizes'][ $size ] ) ) {
  733. $data = $imagedata['sizes'][ $size ];
  734. }
  735. // If we still don't have a match at this point, return false.
  736. if ( empty( $data ) ) {
  737. return false;
  738. }
  739. // Include the full filesystem path of the intermediate file.
  740. if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) {
  741. $file_url = wp_get_attachment_url( $post_id );
  742. $data['path'] = path_join( dirname( $imagedata['file'] ), $data['file'] );
  743. $data['url'] = path_join( dirname( $file_url ), $data['file'] );
  744. }
  745. /**
  746. * Filters the output of image_get_intermediate_size()
  747. *
  748. * @since 4.4.0
  749. *
  750. * @see image_get_intermediate_size()
  751. *
  752. * @param array $data Array of file relative path, width, and height on success. May also include
  753. * file absolute path and URL.
  754. * @param int $post_id The post_id of the image attachment
  755. * @param string|array $size Registered image size or flat array of initially-requested height and width
  756. * dimensions (in that order).
  757. */
  758. return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size );
  759. }
  760. /**
  761. * Gets the available intermediate image size names.
  762. *
  763. * @since 3.0.0
  764. *
  765. * @return string[] An array of image size names.
  766. */
  767. function get_intermediate_image_sizes() {
  768. $default_sizes = array( 'thumbnail', 'medium', 'medium_large', 'large' );
  769. $additional_sizes = wp_get_additional_image_sizes();
  770. if ( ! empty( $additional_sizes ) ) {
  771. $default_sizes = array_merge( $default_sizes, array_keys( $additional_sizes ) );
  772. }
  773. /**
  774. * Filters the list of intermediate image sizes.
  775. *
  776. * @since 2.5.0
  777. *
  778. * @param string[] $default_sizes An array of intermediate image size names. Defaults
  779. * are 'thumbnail', 'medium', 'medium_large', 'large'.
  780. */
  781. return apply_filters( 'intermediate_image_sizes', $default_sizes );
  782. }
  783. /**
  784. * Returns a normalized list of all currently registered image sub-sizes.
  785. *
  786. * @since 5.3.0
  787. * @uses wp_get_additional_image_sizes()
  788. * @uses get_intermediate_image_sizes()
  789. *
  790. * @return array Associative array of the registered image sub-sizes.
  791. */
  792. function wp_get_registered_image_subsizes() {
  793. $additional_sizes = wp_get_additional_image_sizes();
  794. $all_sizes = array();
  795. foreach ( get_intermediate_image_sizes() as $size_name ) {
  796. $size_data = array(
  797. 'width' => 0,
  798. 'height' => 0,
  799. 'crop' => false,
  800. );
  801. if ( isset( $additional_sizes[ $size_name ]['width'] ) ) {
  802. // For sizes added by plugins and themes.
  803. $size_data['width'] = intval( $additional_sizes[ $size_name ]['width'] );
  804. } else {
  805. // For default sizes set in options.
  806. $size_data['width'] = intval( get_option( "{$size_name}_size_w" ) );
  807. }
  808. if ( isset( $additional_sizes[ $size_name ]['height'] ) ) {
  809. $size_data['height'] = intval( $additional_sizes[ $size_name ]['height'] );
  810. } else {
  811. $size_data['height'] = intval( get_option( "{$size_name}_size_h" ) );
  812. }
  813. if ( empty( $size_data['width'] ) && empty( $size_data['height'] ) ) {
  814. // This size isn't set.
  815. continue;
  816. }
  817. if ( isset( $additional_sizes[ $size_name ]['crop'] ) ) {
  818. $size_data['crop'] = $additional_sizes[ $size_name ]['crop'];
  819. } else {
  820. $size_data['crop'] = get_option( "{$size_name}_crop" );
  821. }
  822. if ( ! is_array( $size_data['crop'] ) || empty( $size_data['crop'] ) ) {
  823. $size_data['crop'] = (bool) $size_data['crop'];
  824. }
  825. $all_sizes[ $size_name ] = $size_data;
  826. }
  827. return $all_sizes;
  828. }
  829. /**
  830. * Retrieves an image to represent an attachment.
  831. *
  832. * @since 2.5.0
  833. *
  834. * @param int $attachment_id Image attachment ID.
  835. * @param string|int[] $size Optional. Image size. Accepts any valid image size name, or an array of width
  836. * and height values in pixels (in that order). Default 'thumbnail'.
  837. * @param bool $icon Optional. Whether the image should fall back to a mime type icon. Default false.
  838. * @return array|false {
  839. * Array of image data, or boolean false if no image is available.
  840. *
  841. * @type string $0 Image source URL.
  842. * @type int $1 Image width in pixels.
  843. * @type int $2 Image height in pixels.
  844. * }
  845. */
  846. function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) {
  847. // Get a thumbnail or intermediate image if there is one.
  848. $image = image_downsize( $attachment_id, $size );
  849. if ( ! $image ) {
  850. $src = false;
  851. if ( $icon ) {
  852. $src = wp_mime_type_icon( $attachment_id );
  853. if ( $src ) {
  854. /** This filter is documented in wp-includes/post.php */
  855. $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
  856. $src_file = $icon_dir . '/' . wp_basename( $src );
  857. list( $width, $height ) = @getimagesize( $src_file );
  858. }
  859. }
  860. if ( $src && $width && $height ) {
  861. $image = array( $src, $width, $height );
  862. }
  863. }
  864. /**
  865. * Filters the attachment image source result.
  866. *
  867. * @since 4.3.0
  868. *
  869. * @param array|false $image {
  870. * Array of image data, or boolean false if no image is available.
  871. *
  872. * @type string $0 Image source URL.
  873. * @type int $1 Image width in pixels.
  874. * @type int $2 Image height in pixels.
  875. * }
  876. * @param int $attachment_id Image attachment ID.
  877. * @param string|int[] $size Requested size of image. Image size name, or array of width
  878. * and height values (in that order).
  879. * @param bool $icon Whether the image should be treated as an icon.
  880. */
  881. return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
  882. }
  883. /**
  884. * Get an HTML img element representing an image attachment
  885. *
  886. * While `$size` will accept an array, it is better to register a size with
  887. * add_image_size() so that a cropped version is generated. It's much more
  888. * efficient than having to find the closest-sized image and then having the
  889. * browser scale down the image.
  890. *
  891. * @since 2.5.0
  892. *
  893. * @param int $attachment_id Image attachment ID.
  894. * @param string|array $size Optional. Image size. Accepts any valid image size, or an array of width
  895. * and height values in pixels (in that order). Default 'thumbnail'.
  896. * @param bool $icon Optional. Whether the image should be treated as an icon. Default false.
  897. * @param string|array $attr {
  898. * Optional. Attributes for the image markup.
  899. *
  900. * @type string $src Image attachment URL.
  901. * @type string $class CSS class name or space-separated list of classes.
  902. * Default `attachment-$size_class size-$size_class`,
  903. * where `$size_class` is the image size being requested.
  904. * @type string $alt Image description for the alt attribute.
  905. * @type string $srcset The 'srcset' attribute value.
  906. * @type string $sizes The 'sizes' attribute value.
  907. * }
  908. * @return string HTML img element or empty string on failure.
  909. */
  910. function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {
  911. $html = '';
  912. $image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
  913. if ( $image ) {
  914. list( $src, $width, $height ) = $image;
  915. $hwstring = image_hwstring( $width, $height );
  916. $size_class = $size;
  917. if ( is_array( $size_class ) ) {
  918. $size_class = join( 'x', $size_class );
  919. }
  920. $attachment = get_post( $attachment_id );
  921. $default_attr = array(
  922. 'src' => $src,
  923. 'class' => "attachment-$size_class size-$size_class",
  924. 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
  925. );
  926. $attr = wp_parse_args( $attr, $default_attr );
  927. // Generate 'srcset' and 'sizes' if not already present.
  928. if ( empty( $attr['srcset'] ) ) {
  929. $image_meta = wp_get_attachment_metadata( $attachment_id );
  930. if ( is_array( $image_meta ) ) {
  931. $size_array = array( absint( $width ), absint( $height ) );
  932. $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id );
  933. $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id );
  934. if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
  935. $attr['srcset'] = $srcset;
  936. if ( empty( $attr['sizes'] ) ) {
  937. $attr['sizes'] = $sizes;
  938. }
  939. }
  940. }
  941. }
  942. /**
  943. * Filters the list of attachment image attributes.
  944. *
  945. * @since 2.8.0
  946. *
  947. * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name.
  948. * See wp_get_attachment_image().
  949. * @param WP_Post $attachment Image attachment post.
  950. * @param string|array $size Requested size. Image size or array of width and height values
  951. * (in that order). Default 'thumbnail'.
  952. */
  953. $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
  954. $attr = array_map( 'esc_attr', $attr );
  955. $html = rtrim( "<img $hwstring" );
  956. foreach ( $attr as $name => $value ) {
  957. $html .= " $name=" . '"' . $value . '"';
  958. }
  959. $html .= ' />';
  960. }
  961. return $html;
  962. }
  963. /**
  964. * Get the URL of an image attachment.
  965. *
  966. * @since 4.4.0
  967. *
  968. * @param int $attachment_id Image attachment ID.
  969. * @param string|array $size Optional. Image size to retrieve. Accepts any valid image size, or an array
  970. * of width and height values in pixels (in that order). Default 'thumbnail'.
  971. * @param bool $icon Optional. Whether the image should be treated as an icon. Default false.
  972. * @return string|false Attachment URL or false if no image is available.
  973. */
  974. function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) {
  975. $image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
  976. return isset( $image['0'] ) ? $image['0'] : false;
  977. }
  978. /**
  979. * Get the attachment path relative to the upload directory.
  980. *
  981. * @since 4.4.1
  982. * @access private
  983. *
  984. * @param string $file Attachment file name.
  985. * @return string Attachment path relative to the upload directory.
  986. */
  987. function _wp_get_attachment_relative_path( $file ) {
  988. $dirname = dirname( $file );
  989. if ( '.' === $dirname ) {
  990. return '';
  991. }
  992. if ( false !== strpos( $dirname, 'wp-content/uploads' ) ) {
  993. // Get the directory name relative to the upload directory (back compat for pre-2.7 uploads).
  994. $dirname = substr( $dirname, strpos( $dirname, 'wp-content/uploads' ) + 18 );
  995. $dirname = ltrim( $dirname, '/' );
  996. }
  997. return $dirname;
  998. }
  999. /**
  1000. * Get the image size as array from its meta data.
  1001. *
  1002. * Used for responsive images.
  1003. *
  1004. * @since 4.4.0
  1005. * @access private
  1006. *
  1007. * @param string $size_name Image size. Accepts any valid image size name ('thumbnail', 'medium', etc.).
  1008. * @param array $image_meta The image meta data.
  1009. * @return array|bool The image meta data as returned by `wp_get_attachment_metadata()`.
  1010. */
  1011. function _wp_get_image_size_from_meta( $size_name, $image_meta ) {
  1012. if ( 'full' === $size_name ) {
  1013. return array(
  1014. absint( $image_meta['width'] ),
  1015. absint( $image_meta['height'] ),
  1016. );
  1017. } elseif ( ! empty( $image_meta['sizes'][ $size_name ] ) ) {
  1018. return array(
  1019. absint( $image_meta['sizes'][ $size_name ]['width'] ),
  1020. absint( $image_meta['sizes'][ $size_name ]['height'] ),
  1021. );
  1022. }
  1023. return false;
  1024. }
  1025. /**
  1026. * Retrieves the value for an image attachment's 'srcset' attribute.
  1027. *
  1028. * @since 4.4.0
  1029. *
  1030. * @see wp_calculate_image_srcset()
  1031. *
  1032. * @param int $attachment_id Image attachment ID.
  1033. * @param array|string $size Optional. Image size. Accepts any valid image size, or an array of
  1034. * width and height values in pixels (in that order). Default 'medium'.
  1035. * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
  1036. * Default null.
  1037. * @return string|bool A 'srcset' value string or false.
  1038. */
  1039. function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) {
  1040. $image = wp_get_attachment_image_src( $attachment_id, $size );
  1041. if ( ! $image ) {
  1042. return false;
  1043. }
  1044. if ( ! is_array( $image_meta ) ) {
  1045. $image_meta = wp_get_attachment_metadata( $attachment_id );
  1046. }
  1047. $image_src = $image[0];
  1048. $size_array = array(
  1049. absint( $image[1] ),
  1050. absint( $image[2] ),
  1051. );
  1052. return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
  1053. }
  1054. /**
  1055. * A helper function to calculate the image sources to include in a 'srcset' attribute.
  1056. *
  1057. * @since 4.4.0
  1058. *
  1059. * @param int[] $size_array {
  1060. * An array of width and height values.
  1061. *
  1062. * @type int $0 The width in pixels.
  1063. * @type int $1 The height in pixels.
  1064. * }
  1065. * @param string $image_src The 'src' of the image.
  1066. * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
  1067. * @param int $attachment_id Optional. The image attachment ID. Default 0.
  1068. * @return string|bool The 'srcset' attribute value. False on error or when only one source exists.
  1069. */
  1070. function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {
  1071. /**
  1072. * Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data.
  1073. *
  1074. * @since 4.5.0
  1075. *
  1076. * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
  1077. * @param int[] $size_array {
  1078. * An array of requested width and height values.
  1079. *
  1080. * @type int $0 The width in pixels.
  1081. * @type int $1 The height in pixels.
  1082. * }
  1083. * @param string $image_src The 'src' of the image.
  1084. * @param int $attachment_id The image attachment ID or 0 if not supplied.
  1085. */
  1086. $image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id );
  1087. if ( empty( $image_meta['sizes'] ) || ! isset( $image_meta['file'] ) || strlen( $image_meta['file'] ) < 4 ) {
  1088. return false;
  1089. }
  1090. $image_sizes = $image_meta['sizes'];
  1091. // Get the width and height of the image.
  1092. $image_width = (int) $size_array[0];
  1093. $image_height = (int) $size_array[1];
  1094. // Bail early if error/no width.
  1095. if ( $image_width < 1 ) {
  1096. return false;
  1097. }
  1098. $image_basename = wp_basename( $image_meta['file'] );
  1099. /*
  1100. * WordPress flattens animated GIFs into one frame when generating intermediate sizes.
  1101. * To avoid hiding animation in user content, if src is a full size GIF, a srcset attribute is not generated.
  1102. * If src is an intermediate size GIF, the full size is excluded from srcset to keep a flattened GIF from becoming animated.
  1103. */
  1104. if ( ! isset( $image_sizes['thumbnail']['mime-type'] ) || 'image/gif' !== $image_sizes['thumbnail']['mime-type'] ) {
  1105. $image_sizes[] = array(
  1106. 'width' => $image_meta['width'],
  1107. 'height' => $image_meta['height'],
  1108. 'file' => $image_basename,
  1109. );
  1110. } elseif ( strpos( $image_src, $image_meta['file'] ) ) {
  1111. return false;
  1112. }
  1113. // Retrieve the uploads sub-directory from the full size image.
  1114. $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
  1115. if ( $dirname ) {
  1116. $dirname = trailingslashit( $dirname );
  1117. }
  1118. $upload_dir = wp_get_upload_dir();
  1119. $image_baseurl = trailingslashit( $upload_dir['baseurl'] ) . $dirname;
  1120. /*
  1121. * If currently on HTTPS, prefer HTTPS URLs when we know they're supported by the domain
  1122. * (which is to say, when they share the domain name of the current request).
  1123. */
  1124. if ( is_ssl() && 'https' !== substr( $image_baseurl, 0, 5 ) && parse_url( $image_baseurl, PHP_URL_HOST ) === $_SERVER['HTTP_HOST'] ) {
  1125. $image_baseurl = set_url_scheme( $image_baseurl, 'https' );
  1126. }
  1127. /*
  1128. * Images that have been edited in WordPress after being uploaded will
  1129. * contain a unique hash. Look for that hash and use it later to filter
  1130. * out images that are leftovers from previous versions.
  1131. */
  1132. $image_edited = preg_match( '/-e[0-9]{13}/', wp_basename( $image_src ), $image_edit_hash );
  1133. /**
  1134. * Filters the maximum image width to be included in a 'srcset' attribute.
  1135. *
  1136. * @since 4.4.0
  1137. *
  1138. * @param int $max_width The maximum image width to be included in the 'srcset'. Default '2048'.
  1139. * @param int[] $size_array {
  1140. * An array of requested width and height values.
  1141. *
  1142. * @type int $0 The width in pixels.
  1143. * @type int $1 The height in pixels.
  1144. * }
  1145. */
  1146. $max_srcset_image_width = apply_filters( 'max_srcset_image_width', 2048, $size_array );
  1147. // Array to hold URL candidates.
  1148. $sources = array();
  1149. /**
  1150. * To make sure the ID matches our image src, we will check to see if any sizes in our attachment
  1151. * meta match our $image_src. If no matches are found we don't return a srcset to avoid serving
  1152. * an incorrect image. See #35045.
  1153. */
  1154. $src_matched = false;
  1155. /*
  1156. * Loop through available images. Only use images that are resized
  1157. * versions of the same edit.
  1158. */
  1159. foreach ( $image_sizes as $image ) {
  1160. $is_src = false;
  1161. // Check if image meta isn't corrupted.
  1162. if ( ! is_array( $image ) ) {
  1163. continue;
  1164. }
  1165. // If the file name is part of the `src`, we've confirmed a match.
  1166. if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) {
  1167. $src_matched = true;
  1168. $is_src = true;
  1169. }
  1170. // Filter out images that are from previous edits.
  1171. if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) {
  1172. continue;
  1173. }
  1174. /*
  1175. * Filters out images that are wider than '$max_srcset_image_width' unless
  1176. * that file is in the 'src' attribute.
  1177. */
  1178. if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width && ! $is_src ) {
  1179. continue;
  1180. }
  1181. // If the image dimensions are within 1px of the expected size, use it.
  1182. if ( wp_image_matches_ratio( $image_width, $image_height, $image['width'], $image['height'] ) ) {
  1183. // Add the URL, descriptor, and value to the sources array to be returned.
  1184. $source = array(
  1185. 'url' => $image_baseurl . $image['file'],
  1186. 'descriptor' => 'w',
  1187. 'value' => $image['width'],
  1188. );
  1189. // The 'src' image has to be the first in the 'srcset', because of a bug in iOS8. See #35030.
  1190. if ( $is_src ) {
  1191. $sources = array( $image['width'] => $source ) + $sources;
  1192. } else {
  1193. $sources[ $image['width'] ] = $source;
  1194. }
  1195. }
  1196. }
  1197. /**
  1198. * Filters an image's 'srcset' sources.
  1199. *
  1200. * @since 4.4.0
  1201. *
  1202. * @param array $sources {
  1203. * One or more arrays of source data to include in the 'srcset'.
  1204. *
  1205. * @type array $width {
  1206. * @type string $url The URL of an image source.
  1207. * @type string $descriptor The descriptor type used in the image candidate string,
  1208. * either 'w' or 'x'.
  1209. * @type int $value The source width if paired with a 'w' descriptor, or a
  1210. * pixel density value if paired with an 'x' descriptor.
  1211. * }
  1212. * }
  1213. * @param array $size_array {
  1214. * An array of requested width and height values.
  1215. *
  1216. * @type int $0 The width in pixels.
  1217. * @type int $1 The height in pixels.
  1218. * }
  1219. * @param string $image_src The 'src' of the image.
  1220. * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
  1221. * @param int $attachment_id Image attachment ID or 0.
  1222. */
  1223. $sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );
  1224. // Only return a 'srcset' value if there is more than one source.
  1225. if ( ! $src_matched || ! is_array( $sources ) || count( $sources ) < 2 ) {
  1226. return false;
  1227. }
  1228. $srcset = '';
  1229. foreach ( $sources as $source ) {
  1230. $srcset .= str_replace( ' ', '%20', $source['url'] ) . ' ' . $source['value'] . $source['descriptor'] . ', ';
  1231. }
  1232. return rtrim( $srcset, ', ' );
  1233. }
  1234. /**
  1235. * Retrieves the value for