Skip to content

Instantly share code, notes, and snippets.

@sirkitree
Last active December 16, 2015 08:09
Show Gist options
  • Select an option

  • Save sirkitree/5404089 to your computer and use it in GitHub Desktop.

Select an option

Save sirkitree/5404089 to your computer and use it in GitHub Desktop.
Image Style Scale and Crop from the top
; $Id$
name = Image Style Scale and Crop from the top
description = Image style to scale an image and crop it from the top.
core = 7.x
package = ImageStyles
<?php
/**
* Implementation of hook_theme().
*/
function sct_theme() {
$theme = array(
'sct_scale_and_crop_top' => array(
'file' => 'sct_actions.inc',
'variables' => array('element' => NULL),
),
);
return $theme;
}
/**
* Image effect "Scale and Crop from top".
*/
function sct_image_effect_info() {
$effects['image_scale_and_crop_top'] = array(
'label' => 'Scale and Crop from Top',
'description' => 'Resize an image while maintaining aspect ratio, then crop it to the specified dimensions starting conserving the top part of the photo.',
'effect callback' => 'sct_scale_and_crop_top_effect',
'dimensions callback' => 'image_resize_dimensions',
'form callback' => 'image_resize_form',
'summary theme' => 'image_resize_summary',
);
return $effects;
}
/**
* Image effect callback; Scale and crop an image from top.
*/
function sct_scale_and_crop_top_effect(&$image, $data) {
$scale = max($data['width'] / $image->info['width'], $data['height'] / $image->info['height']);
$x = ($image->info['width'] * $scale - $data['width']) / 2;
$y = ($image->info['height'] * $scale - $data['height']) / 2;
if (image_resize($image, $image->info['width'] * $scale, $image->info['height'] * $scale)) {
return image_crop($image, $x, 0, $data['width'], $data['height']);
}
return FALSE;
}
<?php
/**
* Image Style Scale and Crop from the top
*/
function sct_scale_and_crop_top($element) {
return theme_imagecache_resize($element);
}
function sct_scale_and_crop_top_image(&$image, $data) {
if (!sct_scale_and_crop_top_process($image, $data['width'], $data['height'])) {
watchdog('imagecache', 'imagecache_scale_and_crop failed. image: %image, data: %data.', array('%image' => $image->source, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR);
return FALSE;
}
return TRUE;
}
function theme_sct_scale_and_crop_top($element) {
$data = $element['#value'];
if ($data['width'] && $data['height']) {
return check_plain($data['width']) . 'x' . check_plain($data['height']);
}
return ($data['width']) ? t('width @width', array('@width' => $data['width'])) : t('height @height', array('@height' => $data['height']));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment