jetpack_photon_url

Generates a Photon URL.

Description

(string) jetpack_photon_url( (string) $image_url, (array) $args = array(), (null) $scheme = null ); 

Returns (string)

The raw final URL. You should run this through esc_url() before displaying it.

Parameters (3)

0. $image_url (string)
URL to the publicly accessible image you want to manipulate
1. $args — Optional. (array) => array()
An array of arguments, i.e. array( w => 300,, resize => array( 123, 456 ) ), or in string form (w=123&h=456)
2. $scheme — Optional. (null) => null
The scheme.

Usage

  1. if ( !function_exists( 'jetpack_photon_url' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'jetpack-by-wordpress-com/functions.photon.php'; 
  3.  
  4. // URL to the publicly accessible image you want to manipulate 
  5. $image_url = ''; 
  6.  
  7. // An array of arguments, i.e. array( 'w' => '300', 'resize' => array( 123, 456 ) ), or in string form (w=123&h=456) 
  8. $args = array(); 
  9.  
  10. // The scheme. 
  11. $scheme = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = jetpack_photon_url($image_url, $args, $scheme); 
  15.  

Defined (1)

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

/functions.photon.php  
  1. function jetpack_photon_url( $image_url, $args = array(), $scheme = null ) { 
  2. $image_url = trim( $image_url ); 
  3.  
  4. /** 
  5. * Allow specific image URls to avoid going through Photon. 
  6. * @module photon 
  7. * @since 3.2.0 
  8. * @param bool false Should the image be returned as is, without going through Photon. Default to false. 
  9. * @param string $image_url Image URL. 
  10. * @param array|string $args Array of Photon arguments. 
  11. * @param string|null $scheme Image scheme. Default to null. 
  12. */ 
  13. if ( false !== apply_filters( 'jetpack_photon_skip_for_url', false, $image_url, $args, $scheme ) ) { 
  14. return $image_url; 
  15.  
  16. /** 
  17. * Filter the original image URL before it goes through Photon. 
  18. * @module photon 
  19. * @since 1.9.0 
  20. * @param string $image_url Image URL. 
  21. * @param array|string $args Array of Photon arguments. 
  22. * @param string|null $scheme Image scheme. Default to null. 
  23. */ 
  24. $image_url = apply_filters( 'jetpack_photon_pre_image_url', $image_url, $args, $scheme ); 
  25. /** 
  26. * Filter the original Photon image parameters before Photon is applied to an image. 
  27. * @module photon 
  28. * @since 1.9.0 
  29. * @param array|string $args Array of Photon arguments. 
  30. * @param string $image_url Image URL. 
  31. * @param string|null $scheme Image scheme. Default to null. 
  32. */ 
  33. $args = apply_filters( 'jetpack_photon_pre_args', $args, $image_url, $scheme ); 
  34.  
  35. if ( empty( $image_url ) ) 
  36. return $image_url; 
  37.  
  38. $image_url_parts = @parse_url( $image_url ); 
  39.  
  40. // Unable to parse 
  41. if ( ! is_array( $image_url_parts ) || empty( $image_url_parts['host'] ) || empty( $image_url_parts['path'] ) ) 
  42. return $image_url; 
  43.  
  44. if ( is_array( $args ) ) { 
  45. // Convert values that are arrays into strings 
  46. foreach ( $args as $arg => $value ) { 
  47. if ( is_array( $value ) ) { 
  48. $args[$arg] = implode( ', ', $value ); 
  49.  
  50. // Encode values 
  51. // See http://core.trac.wordpress.org/ticket/17923 
  52. $args = rawurlencode_deep( $args ); 
  53.  
  54. /** This filter is documented below. */ 
  55. $custom_photon_url = apply_filters( 'jetpack_photon_domain', '', $image_url ); 
  56. $custom_photon_url = esc_url( $custom_photon_url ); 
  57.  
  58. // You can't run a Photon URL through Photon again because query strings are stripped. 
  59. // So if the image is already a Photon URL, append the new arguments to the existing URL. 
  60. if ( 
  61. in_array( $image_url_parts['host'], array( 'i0.wp.com', 'i1.wp.com', 'i2.wp.com' ) ) 
  62. || $image_url_parts['host'] === parse_url( $custom_photon_url, PHP_URL_HOST ) 
  63. ) { 
  64. $photon_url = add_query_arg( $args, $image_url ); 
  65. return jetpack_photon_url_scheme( $photon_url, $scheme ); 
  66.  
  67. /** 
  68. * Allow Photon to use query strings as well. 
  69. * By default, Photon doesn't support query strings so we ignore them and look only at the path. 
  70. * This setting is Photon Server dependent. 
  71. * @module photon 
  72. * @since 1.9.0 
  73. * @param bool false Should images using query strings go through Photon. Default is false. 
  74. * @param string $image_url_parts['host'] Image URL's host. 
  75. */ 
  76. if ( ! apply_filters( 'jetpack_photon_any_extension_for_domain', false, $image_url_parts['host'] ) ) { 
  77. // Photon doesn't support query strings so we ignore them and look only at the path. 
  78. // However some source images are served via PHP so check the no-query-string extension. 
  79. // For future proofing, this is a blacklist of common issues rather than a whitelist. 
  80. $extension = pathinfo( $image_url_parts['path'], PATHINFO_EXTENSION ); 
  81. if ( empty( $extension ) || in_array( $extension, array( 'php' ) ) ) 
  82. return $image_url; 
  83.  
  84. $image_host_path = $image_url_parts['host'] . $image_url_parts['path']; 
  85.  
  86. // Figure out which CDN subdomain to use 
  87. srand( crc32( $image_host_path ) ); 
  88. $subdomain = rand( 0, 2 ); 
  89. srand(); 
  90.  
  91. /** 
  92. * Filters the domain used by the Photon module. 
  93. * @module photon 
  94. * @since 3.4.2 
  95. * @param string http://i{$subdomain}.wp.com Domain used by Photon. $subdomain is a random number between 0 and 2. 
  96. * @param string $image_url URL of the image to be photonized. 
  97. */ 
  98. $photon_domain = apply_filters( 'jetpack_photon_domain', "http://i{$subdomain}.wp.com", $image_url ); 
  99. $photon_domain = trailingslashit( esc_url( $photon_domain ) ); 
  100. $photon_url = $photon_domain . $image_host_path; 
  101.  
  102. /** 
  103. * Add query strings to Photon URL. 
  104. * By default, Photon doesn't support query strings so we ignore them. 
  105. * This setting is Photon Server dependent. 
  106. * @module photon 
  107. * @since 1.9.0 
  108. * @param bool false Should query strings be added to the image URL. Default is false. 
  109. * @param string $image_url_parts['host'] Image URL's host. 
  110. */ 
  111. if ( isset( $image_url_parts['query'] ) && apply_filters( 'jetpack_photon_add_query_string_to_domain', false, $image_url_parts['host'] ) ) { 
  112. $photon_url .= '?q=' . rawurlencode( $image_url_parts['query'] ); 
  113.  
  114. if ( $args ) { 
  115. if ( is_array( $args ) ) { 
  116. $photon_url = add_query_arg( $args, $photon_url ); 
  117. } else { 
  118. // You can pass a query string for complicatedrequestsbut where you still want CDN subdomain help, etc. 
  119. $photon_url .= '?' . $args; 
  120.  
  121. return jetpack_photon_url_scheme( $photon_url, $scheme );