Last active
August 29, 2015 14:08
-
-
Save joelstein/29f94ddc69a9a11fa0b0 to your computer and use it in GitHub Desktop.
Drupal Picture/Breakpoints Image Styles Generator
This file contains hidden or 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 | |
/** | |
* Configure ratios, columns, breakpoints, and other stuff. | |
*/ | |
function custom_config() { | |
return array( | |
'ratios' => array( | |
'wide' => array('width' => 1, 'height' => .5), | |
'normal' => array('width' => 1, 'height' => .75), | |
'full' => array('width' => 1), | |
), | |
'columns' => array(3, 4, 6, 12), | |
'total_columns' => 12, | |
'container' => .9, | |
'breakpoints' => breakpoints_breakpoint_load_all_theme(), | |
'theme' => variable_get('theme_default', 'bartik'), | |
); | |
} | |
/** | |
* Implements hook_ctools_plugin_api(). | |
*/ | |
function custom_ctools_plugin_api($module = NULL, $api = NULL) { | |
if ($module == "picture" && $api == "default_picture_mapping") { | |
return array("version" => "2"); | |
} | |
} | |
/** | |
* Implements hook_image_default_styles(). | |
*/ | |
function custom_image_default_styles() { | |
$config = custom_config(); | |
// Create image style for each combination. | |
$styles = array(); | |
foreach ($config['ratios'] as $ratio_name => $ratio) { | |
foreach ($config['columns'] as $column) { | |
foreach ($config['breakpoints'] as $breakpoint) { | |
$name = "image_{$ratio_name}_{$column}col_{$breakpoint->name}px"; | |
$styles[$name]['label'] = $name; | |
// If height is defined, use Focal Point Scale and Crop. | |
if (isset($ratio['height'])) { | |
$styles[$name]['effects'][] = array( | |
'name' => 'focal_point_scale_and_crop', | |
'weight' => 0, | |
'data' => array( | |
'width' => ceil($breakpoint->name * 2 * $column / $config['total_columns'] * $config['container'] * $ratio['width']), | |
'height' => ceil($breakpoint->name * 2 * $column / $config['total_columns'] * $config['container'] * $ratio['height']), | |
), | |
); | |
} | |
// Otherwise, use Scale on width only. | |
else { | |
$styles[$name]['effects'][] = array( | |
'name' => 'image_scale', | |
'weight' => 0, | |
'data' => array( | |
'width' => ceil($breakpoint->name * 2 * $column / $config['total_columns'] * $config['container']), | |
'height' => 0, | |
'upscale' => 0, | |
), | |
); | |
} | |
// Add Hi-Res and 30% quality. | |
$styles[$name]['effects'][] = array( | |
'name' => 'image_hires', | |
'weight' => 1, | |
); | |
$styles[$name]['effects'][] = array( | |
'name' => 'image_style_quality', | |
'weight' => 2, | |
'data' => array( | |
'quality' => 30, | |
), | |
); | |
} | |
} | |
} | |
return $styles; | |
} | |
/** | |
* Implements hook_default_picture_mapping(). | |
*/ | |
function custom_default_picture_mapping() { | |
$config = custom_config(); | |
$export = array(); | |
// Create a picture mapping for each combination. | |
foreach ($config['ratios'] as $type => $ratio) { | |
foreach ($config['columns'] as $column) { | |
$name = "image_{$type}_{$column}col"; | |
$picture_mapping = new PictureMapping(); | |
$picture_mapping->disabled = FALSE; | |
$picture_mapping->api_version = 2; | |
$picture_mapping->label = $name; | |
$picture_mapping->machine_name = $name; | |
$picture_mapping->breakpoint_group = variable_get('theme_default', 'bartik'); | |
$mappings = array(); | |
foreach ($config['breakpoints'] as $breakpoint) { | |
$mappings[$breakpoint->machine_name]['1x'] = array( | |
'mapping_type' => 'image_style', | |
'image_style' => "{$name}_{$breakpoint->name}px", | |
); | |
} | |
$picture_mapping->setMappings($mappings); | |
$export[$name] = $picture_mapping; | |
} | |
} | |
return $export; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment