Jetpack
Jetpack Photon Snippets
Last updated 2 years ago
Determine if Jetpack is installed and can generate photon URLs:
PHP
function_exists( ‘jetpack_photon_url' );
Determine whether photon is an active Jetpack module:
PHP
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:
PHP
echo apply_filters( 'jetpack_photon_url', get_template_directory_uri() . '/img/logo.png' );
Example with argument:
PHP
echo apply_filters( 'jetpack_photon_url', get_template_directory_uri() . '/img/logo.png', 'filter=grayscale', 'https' );
Set custom photon arguments for all photon urls:
PHP
/**
* 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:
PHP
/**
* 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:
Parameter | Description | Usage |
---|---|---|
w | width (px) | w=50 |
h | height (px) | h=50 |
crop | crop x,y,w,h (defaults to %) | crop=5,10,20,40 |
resize | resize and crop to given dimensions (px) | resize=250,300 |
fit | keep aspect ratio, but fit to within given box (px) | fit=100,100 |
lb | add letterboxing, color code is optional (it’s black by default) | lb=300,250,1A4E9F |
ulb | remove letterboxing (auto detects the letterbox color) | ulb=true |
filter | apply various imagefilter() filters | filter=grayscale |
brightness | adjusts brightness | brightness=100 |
contrast | adjusts contrast | contrast=50 |
colorize | applies a color hue | colorize=255,0,0 |
smooth | smoothes out the image | smooth=2 |
zoom | size images for high pixel ratio devices | zoom=2 |
quality | sets the quality for JPEG images | quality=50 |
strip | removes metadata from JPEG images | strip=all |
All code snippets are licensed GPLv2 or later unless otherwise stated.