Jetpack Photon Snippets

Last updated 1 year ago

Determine if Jetpack is installed and can generate photon URLs:

function_exists( ‘jetpack_photon_url' );

Determine whether photon is an active Jetpack module:

class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'get_active_modules' ) && in_array( 'photon', Jetpack::get_active_modules() )

Example how to apply photon to any image you wish to load via custom theme:

echo apply_filters( 'jetpack_photon_url', get_template_directory_uri() . '/img/logo.png' );

Example with argument:

echo apply_filters( 'jetpack_photon_url', get_template_directory_uri() . '/img/logo.png', 'filter=grayscale', 'https' );

Set custom photon arguments for all photon urls:

* Hubbard Labs Custom Photon Arguments.
* @access public
* @param mixed $args Photon Arguments.
function hlabs_custom_photon( $args ) {
  $args['quality'] = 80;
  $args['strip']   = 'all';
  $args['filter']  = 'grayscale';
  return $args;
add_filter( 'jetpack_photon_pre_args', 'hlabs_custom_photon' );

Set header Image to use photon:

* Hubbard Labs Photonize Header Image.
* @access public
* @param mixed $image Image.
function hlabs_photonize_header_image( $image ) {
  if ( $image && function_exists( 'jetpack_photon_url' ) ) {
    $image = jetpack_photon_url( $image );
  return $image;
add_filter( 'theme_mod_header_image', 'hlabs_photonize_header_image' );

Supported query arguments:

wwidth (px)w=50
hheight (px)h=50
cropcrop x,y,w,h (defaults to %)crop=5,10,20,40
resizeresize and crop to given dimensions (px)resize=250,300
fitkeep aspect ratio, but fit to within given box (px)fit=100,100
lbadd letterboxing, color code is optional (it’s black by default)lb=300,250,1A4E9F
ulbremove letterboxing (auto detects the letterbox color)ulb=true
filterapply various imagefilter() filtersfilter=grayscale
brightnessadjusts brightnessbrightness=100
contrastadjusts contrastcontrast=50
colorizeapplies a color huecolorize=255,0,0
smoothsmoothes out the imagesmooth=2
zoomsize images for high pixel ratio deviceszoom=2
qualitysets the quality for JPEG imagesquality=50
stripremoves metadata from JPEG imagesstrip=all
Supported query arguments.

Tags: #Jetpack, #Photon

All code snippets are licensed GPLv2 or later unless otherwise stated.