image_style_quality.module in Image Style Quality 7
This module is intended to allow users to set custom quality settings on individual image styles with no core hacks.
File
image_style_quality.moduleView source
<?php
/**
* @file
* This module is intended to allow users to set custom quality settings
* on individual image styles with no core hacks.
*/
/**
* Implements hook_image_effect_info().
*/
function image_style_quality_image_effect_info() {
$effects = array();
// Define all the paramters of our new style.
$effects['image_style_quality'] = array(
'label' => t('Quality'),
'help' => t('Define the image quality for JPEG manipulations. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'),
'effect callback' => '_image_style_quality_apply',
'form callback' => 'image_style_quality_style_form',
'dimensions passthrough' => TRUE,
'summary theme' => 'image_style_quality_summary',
);
return $effects;
}
/**
* Implements hook_form_alter().
*/
function image_style_quality_help($path, $arg) {
// Allow users to have some extra help on certain pages or args.
switch ($path) {
// If we are on this modules help page.
case 'admin/help#image_style_quality':
return '<h3>' . t('About') . '</h3>
<p>' . t('This module is allows you to create new <em>Image
style</em> effects to modify the quality of images. To use this
module all you have to do is navigate to your desired <em>Image
style</em> by clicking "Configuration" > "Image Styles" and then
"Edit" or "Add Style". You will have the option to "Change
Quality" in the "Add new effect" dropdown menu. You may use
this quality setting in any order, in combination with any
other set of effects.') . '</p>';
break;
}
}
/**
* The configuration form used to ask the user what quality their image
* should be.
*/
function image_style_quality_style_form($data) {
$form = array();
// Collect the quality of our image style, keep the default 75.
$form['quality'] = array(
'#title' => t('Image Quality'),
'#type' => 'textfield',
'#default_value' => isset($data['quality']) ? $data['quality'] : 75,
'#field_prefix' => '%',
'#size' => 4,
'#element_validate' => array(
'_image_style_quality_form_quality_validate',
),
'#required' => TRUE,
);
// Return our form to hook_image_effect_info.
return $form;
}
/**
* This function validates the percentage input by a user to two conditions,
* it must be numeric and it must fall within 1 and 100.
*/
function _image_style_quality_form_quality_validate($element, &$form_state, $form) {
// Get the value entered by the user.
$value = $element['#value'];
// Make sure we are dealing with a number.
if (!is_numeric($value)) {
form_set_error('quality', t('Please enter a percentage as a single digit, eg "70"'));
}
// Make sure that number falls within a valid range.
if ($value <= 0 || $value > 100) {
form_set_error('quality', t('Please enter a percentage between 0 and 100.'));
}
}
/**
* This function is called when the image is ready for proccessing. It
* sets the sites quality setting to the percentage defined in our
* image style.
*/
function _image_style_quality_apply(&$image, $data) {
// Grab the quality from our image style.
$image_style_quality = $data['quality'];
// Set the sites internal quality to be the one defined by our image style.
$GLOBALS['conf']['image_jpeg_quality'] = $image_style_quality;
$GLOBALS['conf']['imagemagick_quality'] = $image_style_quality;
}
/**
* Implements hook_theme().
*/
function image_style_quality_theme() {
return array(
'image_style_quality_summary' => array(
'variables' => array(
'data' => NULL,
),
),
);
}
/**
* Theme function for formatting a percentage quality.
*/
function theme_image_style_quality_summary($variables) {
$data = $variables['data'];
return $data['quality'] . '%';
}
Functions
Name | Description |
---|---|
image_style_quality_help | Implements hook_form_alter(). |
image_style_quality_image_effect_info | Implements hook_image_effect_info(). |
image_style_quality_style_form | The configuration form used to ask the user what quality their image should be. |
image_style_quality_theme | Implements hook_theme(). |
theme_image_style_quality_summary | Theme function for formatting a percentage quality. |
_image_style_quality_apply | This function is called when the image is ready for proccessing. It sets the sites quality setting to the percentage defined in our image style. |
_image_style_quality_form_quality_validate | This function validates the percentage input by a user to two conditions, it must be numeric and it must fall within 1 and 100. |