Last active
August 29, 2015 14:09
-
-
Save n7studios/d7d73a1e763c9698ecac to your computer and use it in GitHub Desktop.
Soliloquy - Preserve and Fill Width or Height
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Soliloquy - Preserve and Fill Width or Height | |
* Plugin URI: http://soliloquywp.com | |
* Version: 1.0 | |
* Author: Tim Carr | |
* Author URI: http://www.n7studios.co.uk | |
* Description: Where an image is either smaller than the slide width but larger than the slide height, or smaller than the slide height but larger than the slide width, scale the image down to meet the slide height or width, without cropping. | |
*/ | |
/** | |
* Change how the image is resized if it meets our conditions | |
* | |
* @param array $args Image cropping/resizing args | |
* @param array $data Slider Data | |
* @return array Image cropping/resizing args | |
*/ | |
function soliloquy_preserve_full_image_width_height( $args, $data ) { | |
// Check if the image width is smaller than the maximum required, and the image height is larger than the maximum required | |
// If so, we need to scale down the image so it meets the dest_height property | |
if ( $args['orig_width'] <= $args['dest_width'] && $args['orig_height'] > $args['dest_height'] ) { | |
// Calculate dest_width | |
$args['dest_width'] = round( ( $args['dest_height'] / $args['orig_height'] ) * $args['orig_width'] ); | |
// Change suffix | |
$args['suffix'] = $args['dest_width'] . 'x' . $args['dest_height'] . '_c'; | |
$args['dest_file_name'] = $args['dir'] . '/' . $args['name'] . '-' . $args['suffix'] . '.' . $args['ext']; | |
return $args; | |
} | |
// Check if the image height is smaller than the maximum required, and the image width is larger than the maximum required | |
// If so, we need to scale down the image so it meets the dest_width property | |
if ( $args['orig_width'] > $args['dest_width'] && $args['orig_height'] <= $args['dest_height'] ) { | |
// Calculate dest_height | |
$args['dest_height'] = (int) round( ( $args['dest_width'] / $args['orig_width'] ) * $args['orig_height'] ); | |
// Change suffix | |
$args['suffix'] = $args['dest_width'] . 'x' . $args['dest_height'] . '_c'; | |
$args['dest_file_name'] = $args['dir'] . '/' . $args['name'] . '-' . $args['suffix'] . '.' . $args['ext']; | |
return $args; | |
} | |
return $args; | |
} | |
add_filter( 'soliloquy_get_image_info', 'soliloquy_preserve_full_image_width_height', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment