Skip to content

Instantly share code, notes, and snippets.

@rahulsprajapati
Last active January 22, 2020 20:11
Show Gist options
  • Save rahulsprajapati/b2178f7c399480fd21fd5c4fe896039f to your computer and use it in GitHub Desktop.
Save rahulsprajapati/b2178f7c399480fd21fd5c4fe896039f to your computer and use it in GitHub Desktop.
Tachyon image service helper class to extend the resized image data. https://github.com/humanmade/tachyon-plugin
<?php
/**
* Tachyon image service helper class to extend the resized image data.
*
* @package tachyon-helper
*/
namespace Tachyon_Helper;
use Tachyon;
/**
* Class Tachyon_Helper
*/
class Tachyon_Helper extends Tachyon {
/**
* Class variables
*
* @var object
*/
private static $__instance = null;
/**
* Singleton implementation
*
* @return object
*/
public static function instance() {
if ( ! is_a( self::$__instance, __CLASS__ ) ) {
$class = get_called_class();
self::$__instance = new $class;
self::$__instance->init();
}
return self::$__instance;
}
/**
* Silence is golden.
*/
private function __construct() {}
/**
* Init helper.
*/
public function init() {
// Disable automatic creation of intermediate image sizes.
add_filter( 'intermediate_image_sizes', '__return_empty_array' );
add_filter( 'intermediate_image_sizes_advanced', '__return_empty_array' );
add_filter( 'fallback_intermediate_image_sizes', '__return_empty_array' );
add_filter( 'wp_get_attachment_metadata', [ $this, 'maybe_inject_image_sizes' ], 20, 2 );
}
/**
* Inject image sizes to attachment metadata.
*
* @param array $data Attachment metadata.
* @param int $attachment_id Attachment's post ID.
*
* @return array Attachment metadata.
*/
public function maybe_inject_image_sizes( $data, $attachment_id ) {
// Can't do much if data is empty.
if ( empty( $data ) ) {
return $data;
}
// Missing some critical data we need to determine sizes, so bail.
if (
! isset( $data['file'] )
|| ! isset( $data['width'] )
|| ! isset( $data['height'] )
) {
return $data;
}
static $cached_sizes = [];
// Don't process image sizes that we already processed.
if ( isset( $cached_sizes[ $attachment_id ] ) ) {
$data['sizes'] = $cached_sizes[ $attachment_id ];
return $data;
}
// Skip non-image attachments.
$mime_type = get_post_mime_type( $attachment_id );
$attachment_is_image = preg_match( '!^image/!', $mime_type );
if ( 1 !== $attachment_is_image ) {
return $data;
}
if ( ! isset( $data['sizes'] ) || ! is_array( $data['sizes'] ) ) {
$data['sizes'] = [];
}
$sizes_already_exist = false === empty( $data['sizes'] );
$registered_image_sizes = self::image_sizes();
if ( is_array( $registered_image_sizes ) ) {
$available_sizes = array_keys( $registered_image_sizes );
$known_sizes = array_keys( $data['sizes'] );
$missing_sizes = array_diff( $available_sizes, $known_sizes );
// Do not need to process if there is no missing image size.
if ( $sizes_already_exist && empty( $missing_sizes ) ) {
return $data;
}
$new_sizes = [];
foreach ( $missing_sizes as $size ) {
$new_width = (int) $registered_image_sizes[ $size ]['width'];
$new_height = (int) $registered_image_sizes[ $size ]['height'];
$new_sizes[ $size ] = [
'file' => basename( $data['file'] ),
'width' => $new_width,
'height' => $new_height,
'mime_type' => $mime_type,
];
}
if ( ! empty( $new_sizes ) ) {
$data['sizes'] = array_merge( $data['sizes'], $new_sizes );
}
}
$cached_sizes[ $attachment_id ] = $data['sizes'];
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment