Skip to content

Instantly share code, notes, and snippets.

@a-barbieri
Last active November 28, 2018 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-barbieri/8a2dfd4092d318120491b161f08f84f9 to your computer and use it in GitHub Desktop.
Save a-barbieri/8a2dfd4092d318120491b161f08f84f9 to your computer and use it in GitHub Desktop.
WordPress Plugin to apply a filter and clone every image uploaded.
<?php
///- - - - - - - - - - - - - - - - - - - -
/// Image processing references
///
/// https://codeable.io/community/how-to-watermark-wordpress-images-with-imagemagick/
/// http://ottopress.com/2011/customizing-wordpress-images/
///- - - - - - - - - - - - - - - - - - - -
function register_precompiled_size() {
add_image_size( 'precompiled', 1000, 1000 );
add_image_size( 'precompiled_medium', 750, 750 );
add_image_size( 'precompiled_small', 500, 500 );
add_image_size( 'precompiled_tiny', 250, 250 );
add_image_size( 'precompiled_tinytiny', 50, 50 );
}
add_action( 'after_setup_theme', 'register_precompiled_size' );
function generate_precompiled_image( $meta ) {
$time = substr( $meta['file'], 0, 7); // Extract the date in form "2015/04"
$upload_dir = wp_upload_dir( $time );
precompiled_image_log( $meta );
if ( $meta['width'] >= 1000 && $meta['height'] >= 1000 ) {
$label = 'precompiled';
}
else if ( $meta['width'] >= 750 && $meta['height'] >= 750 ) {
$label = 'precompiled_medium';
}
else if ( $meta['width'] >= 500 && $meta['height'] >= 500 ) {
// add_action( 'admin_notices', array( $this, 'precompiled_error') );
$label = 'precompiled_small';
}
else if ( $meta['width'] >= 250 && $meta['height'] >= 250 ) {
// add_action( 'admin_notices', array( $this, 'precompiled_error') );
$label = 'precompiled_tiny';
}
else {
$label = 'precompiled_tinytiny';
}
$filename = $meta['sizes'][$label]['file'];
$meta['sizes'][$label]['file'] = precompiled_image( $filename, $upload_dir, $label );
return $meta;
}
add_filter( 'wp_generate_attachment_metadata', 'generate_precompiled_image' );
function precompiled_image( $filename, $upload_dir, $label ) {
$original_image_path = trailingslashit( $upload_dir['path'] ) . $filename;
error_log( $original_image_path."\n", 3, "../debug.log");
$image_resource = new Imagick( $original_image_path );
$image_resource->transformImageColorspace( imagick::COLORSPACE_GRAY );
$clut = new Imagick( get_template_directory().'/assets/images/clut.jpg' );
$image_resource->clutImage( $clut );
return save_precompiled_image( $image_resource, $original_image_path, $label );
}
function save_precompiled_image( $image_resource, $original_image_path, $label ) {
$image_data = pathinfo( $original_image_path );
// Drop sizes from name
// For example 'my-image-1400x1000' becomes 'my-image'
$new_image_data_filename_array = explode( '-', $image_data['filename'] );
array_pop( $new_image_data_filename_array );
$new_image_data_filename = implode('-', $new_image_data_filename_array);
$new_filename = $new_image_data_filename . '-' . $label . '.' . $image_data['extension'];
$precompiled_image_path = str_replace($image_data['basename'], $new_filename, $original_image_path);
if ( ! $image_resource->writeImage( $precompiled_image_path ) ) {
return $image_data['basename'];
}
unlink( $original_image_path );
return $new_filename;
}
function precompiled_error() {
$class = 'notice notice-error';
$message = "Sorry, this image cannot be uploaded. \nEach side must be longer than 1000px.";
printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message );
}
function precompiled_image_log( $meta ) {
error_log( "\n\nGet \$meta: \n", 3, "../debug.log");
error_log( print_r( $meta, true ), 3, "../debug.log");
error_log( "\n ---- \n\n", 3, "../debug.log");
}
<?php
/*
Plugin Name: Precompiled Image Generator
Description: Plugin designed for INBAF website
Version: 0.0.1
Author: Alessandro Barbieri
Author URI: http://lacolonia.studio
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
// https://codex.wordpress.org/Writing_a_Plugin#Plugin_Files
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
include_once( plugin_dir_path( __FILE__ ) . 'precompilation_process.php');
include_once( plugin_dir_path( __FILE__ ) . 'precompiled_image_methods.php');
<?php
///- - - - - - - - - - - - - - - - - - - -
/// Get the precompile version of the image
///
/// @param obj $image The image
///
/// @return array ( description_of_the_return_value )
///- - - - - - - - - - - - - - - - - - - -
function precompile( $image ) {
// Get relative path from WP root
// For example: 'path/to/wp-content/uploads/2016/10'
$rel_path_array = explode('/', get_attached_file( $image['ID'] ));
array_pop( $rel_path_array );
$rel_path = implode('/', $rel_path_array ).'/';
// Get absolute path
// For example: 'http://domain.com/wp-content/uploads/2016/10'
$abs_path_array = explode('/', wp_get_attachment_url( $image['ID'] ));
array_pop( $abs_path_array );
$abs_path = implode('/', $abs_path_array ).'/';
// Get image name and extension
$img_elems = explode('.', $image['filename']);
$img_extension = '.'.array_pop( $img_elems );
$img_name = implode('.', $img_elems );
$suffix = get_precompiled_suffix( $rel_path, $img_name, $img_extension );
if ( $suffix ) { $image_exists = true; }
else { $image_exists = false; }
$result = array(
'exists?' => $image_exists,
'original' => $abs_path.$img_name.$img_extension,
'precomplied' => $abs_path.$img_name.$suffix.$img_extension
);
return $result;
}
function get_precompiled_suffix( $rel_path, $img_name, $img_extension ) {
// Default value
$suffix = '-precompiled';
// Check default value
// If doesn't exist change to medium
if ( file_exists( $rel_path.$img_name.$suffix.$img_extension ) ) {
return $suffix;
}
else {
$suffix = '-precompiled_medium';
}
// Check medium value
// If doesn't exist change to small
if ( file_exists( $rel_path.$img_name.$suffix.$img_extension ) ) {
return $suffix;
}
else {
$suffix = '-precompiled_small';
}
// Check small value
// If doesn't exist change to tiny
if ( file_exists( $rel_path.$img_name.$suffix.$img_extension ) ) {
return $suffix;
}
else {
$suffix = '-precompiled_tiny';
}
// Check small value
// If doesn't exist change to tiny
if ( file_exists( $rel_path.$img_name.$suffix.$img_extension ) ) {
return $suffix;
}
else {
$suffix = '-precompiled_tinytiny';
}
// Check tiny value
// If doesn't exist return to false
if ( file_exists( $rel_path.$img_name.$suffix.$img_extension ) ) {
return $suffix;
}
else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment