t('Imagecrop'),
'path' => 'admin/settings/imagecrop',
'callback' => 'drupal_get_form',
'callback arguments' => 'imagecrop_settings',
'access' => $access_settings,
);
$items[] = array(
'path' => 'imagecrop/showcrop',
'callback' => 'imagecrop_showcrop',
'type' => MENU_CALLBACK,
'access' => $access_crop,
);
$items[] = array(
'path' => 'imagecrop/docrop',
'callback' => 'imagecrop_docrop',
'type' => MENU_CALLBACK,
'access' => $access_crop,
);
}
return $items;
}
/**
* Imagecrop settings page
*/
function imagecrop_settings() {
// hook into image module
if (module_exists('image')) {
$options_modules['image'] = t('Hook into image module');
}
// hook into node_images module
if (module_exists('node_images')) {
$result = db_query("SELECT name,value FROM {variable} WHERE name LIKE 'node_images_position_%'");
if (db_num_rows($result) > 0) {
drupal_set_message(t('When you want to enable support for the node_images module, please read the README that comes with the imagecrop module.'));
while ($row = db_fetch_object($result)) {
if (variable_get($row->name, 'hide') != 'hide') {
$explode = explode('_', $row->name);
$options_modules[$row->name] = t('Hook into node_images module for conten type @type', array('@type' => $explode[3]));
}
}
}
}
// hook into imagefield module
if (module_exists('imagefield')) {
$result = db_query("SELECT field_name,label FROM {node_field_instance} WHERE widget_type = 'image'");
while ($row = db_fetch_object($result)) {
$options_fields[$row->field_name] = t('Hook into imagefield %s', array('%s' => $row->label));
}
}
// show checkboxes if options are not empty
if (!empty($options_modules) || !empty($options_fields)) {
if (!empty($options_modules)) {
$form['imagecrop_modules'] = array(
'#type' => 'checkboxes',
'#title' => t('Hook into modules'),
'#default_value' => variable_get('imagecrop_modules', array()),
'#options' => $options_modules,
);
}
if (!empty($options_fields)) {
$form['imagecrop_fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Hook into cck fields'),
'#default_value' => variable_get('imagecrop_fields', array()),
'#options' => $options_fields,
);
}
$form['array_filter'] = array('#type' => 'hidden');
}
else {
$form['no_modules_fields'] = array(
'#type' => 'item',
'#value' => t('No modules or fields are found to hook into.'),
);
}
// drupal message if no action is found with javascript_crop
if (imagecrop_action_exists() == false) {
drupal_set_message(t('No preset is found with the javascript_crop action so far. If you want to take advantage of this module, you will need to create at least one preset with that action.'));
}
return system_settings_form($form);
}
/**
* Implementation of hook_cron().
* Delete all references in imagecrop table when
* a) file doesn't exist anymore.
* b) when preset has been deleted.
* c) when javascrip_crop action is removed from a preset.
*/
function imagecrop_cron() {
// get all files which do not exist anymore from the files table
$result = db_query("SELECT ic.fid,ic.presetid FROM {imagecrop} ic WHERE NOT EXISTS (SELECT fid FROM {files} f WHERE ic.fid = f.fid) AND ic.reference = 'files'");
while ($row = db_fetch_object($result)) {
$records[] = array('fid' => $row->fid, 'presetid' => $row->presetid, 'reference' => 'files');
}
// get all files which do not exist anymore from the node_images table
if (module_exists('node_images')) {
$result = db_query("SELECT ic.fid,presetid FROM {imagecrop} ic WHERE NOT EXISTS (SELECT id FROM {node_images} ni WHERE ic.fid = ni.id) AND ic.reference = 'node_images'");
while ($row = db_fetch_object($result)) {
$records[] = array('fid' => $row->fid, 'presetid' => $row->presetid, 'reference' => 'node_images');
}
}
/*
* Get all records
* a) from presets which do not exist anymore.
* b) and/or from presets with no imagecrop_javascript action anymore.
*/
// files table
$result = db_query("SELECT ic.fid,ic.presetid FROM {imagecrop} ic WHERE NOT EXISTS (SELECT presetid FROM {imagecache_action} ia where ic.presetid = ia.presetid AND ia.action = 'imagecrop_javascript') AND ic.reference = 'files'");
while ($row = db_fetch_object($result)) {
$records[] = array('fid' => $row->fid, 'presetid' => $row->presetid, 'reference' => 'files');
}
// node_images table
if (module_exists('node_images')) {
$result = db_query("SELECT ic.fid,ic.presetid FROM {imagecrop} ic WHERE NOT EXISTS (SELECT presetid FROM {imagecache_action} ia where ic.presetid = ia.presetid AND ia.action = 'imagecrop_javascript') AND ic.reference = 'node_images'");
while ($row = db_fetch_object($result)) {
$records[] = array('fid' => $row->fid, 'presetid' => $row->presetid, 'reference' => 'node_images');
}
}
if (!empty($records)) {
while (list($key, $val) = each($records)) {
db_query("DELETE FROM {imagecrop} WHERE fid=%d AND presetid=%d AND reference = '%s'", $val['fid'], $val['presetid'], $val['reference']);
}
}
}
/**
* Implementation of hook_imagecache_actions().
*/
function imagecrop_imagecache_actions() {
$actions = array(
'imagecrop_javascript' => array(
'name' => 'Javascript crop',
'description' => 'Create a crop with a javascript toolbox.',
'file' => 'imagecrop_actions.inc',
),
);
return $actions;
}
/**
* Helper function to check if a preset exists with the imagecrop_javascript action.
* Needed to determine if we have to display our javascript crop link.
*
* @return true or false
*/
function imagecrop_action_exists() {
$result = db_result(db_query("SELECT actionid FROM {imagecache_action} WHERE action = 'imagecrop_javascript'"));
return $result;
}
/**
* Implementation of hook_form_alter().
* Hook into several existing image modules/fields.
*/
function imagecrop_form_alter($form_id, &$form) {
// do we have presets with javascript_crop ?
$exists = imagecrop_action_exists();
// user access
$access = user_access('crop images with toolbox');
// build array with available modules/fields
$modules = variable_get('imagecrop_modules', array());
$fields = variable_get('imagecrop_fields', array());
$hooks = array_merge($modules, $fields);
// hook into imagefield module
// @todo handle multiple values per field (only first one is found now)
if (module_exists('imagefield') && $exists != false && $access) {
$formfield = imagecrop_find_imagefields($form, $fields);
if ($formfield != false) {
$count = count($formfield);
for ($i=0;$i<$count;$i++) {
if (!empty($form[$formfield[$i]][0]['fid']['#value']))
$form[$formfield[$i]]['croptab'] = imagecrop_formitem($form[$formfield[$i]][0]['fid']['#value'], 0);
}
}
}
// hook into image module
if (module_exists('image') && $exists != false && $access && in_array('image', $hooks) && isset($form['images']['thumbnail'])) {
// it's anonying we have to make a database call to get the right fid.
$fid = db_result(db_query("SELECT fid FROM {files} WHERE nid=%d AND filename = '_original' AND filepath='%s'", $form['nid']['#value'], $form['images']['_original']['#default_value']));
$form['croptab'] = imagecrop_formitem($fid, -10);
}
// hook into node_images module
if (module_exists('node_images') && $form_id == '_node_images_list' && $exists != false && $access) {
$type = $form['#parameters'][1]->type;
if (variable_get('node_images_position_'. $type, 'hide') != 'hide' && in_array('node_images_position_'. $type, $hooks)) {
$form['imagecrop'] = array('#type' => 'hidden', '#value' => '1');
}
}
}
/**
* Helper function to add form item
*
* @return $form form markup
*/
function imagecrop_formitem($fid, $weight = 0) {
$form = array(
'#type' => 'item',
'#value' => ''. t('Javascript crop') .'',
'#weight' => $weight,
);
return $form;
}
/**
* Helper function to search for a cck field image.
* Temporary images not supported at this point.
*
* @param $form complete form object
* @param $fields all available fields from settings
* @return ckk imagefields array or false
*/
function imagecrop_find_imagefields($form, $fields) {
$temp_path = file_directory_temp();
$count = count($fields);
$return = false;
for ($i=0;$i<$count;$i++) {
$temppath = strpos($form[$fields[$i]][0]['filepath']['#value'], $temp_path);
$deleteflag = $form[$fields[$i]][0]['flags']['delete']['#value'];
if (isset($form[$fields[$i]]) && $temppath === false && $deleteflag != 1) {
$return[] = $fields[$i];
}
}
return $return;
}
/**
* Callback with javascript crop.
*
* @param $fid id of file
* @param $presetid id of preset
*/
function imagecrop_docrop($fid, $presetid, $module = '') {
imagecrop_markup(true, true);
if (imagecrop_action_exists() == true) {
$presets = return_presets($presetid);
$file = create_image_object($fid, $presetid, $module);
if ($file != false) {
$size_warning = false;
// get size of temporary image
$size = getimagesize($file->dst);
$width = $size[0];
$height = $size[1];
// return warning message if crop toolbox is too big and not resizable.
if (($width < $file->crop_width || $height < $file->crop_height) && $file->resizable == 0) {
$size_warning = true;
}
// add jquery interface
jquery_interface_add();
// output
if ($size_warning == false) {
$url = url($file->dst);
$output .= '
'. t('Even if you think this crop is ok, press the submit button!') .'
';
$output .= theme('imagecrop', $url, $width, $height, $file->resizable);
$output .= drupal_get_form('imageoffsets', $file->xoffset, $file->yoffset, $file->crop_width, $file->crop_height, $presetid, $fid, $module);
}
else {
$output .= '';
}
return $output;
}
else {
return ''. t('Image to crop was not found.') .'
';
}
}
else {
return ''. t('No preset is found with the javascript_crop action so far. If you want to take advantage of this module, you will need to create at least one preset with that action.') .'
';
}
}
/**
* Callback to return offsets, height & width
*
* @param $xoffset x value of javascript box
* @param $yoffset y value of javascript box
* @param $crop_width width of javascript box
* @param $crop_height height of javascript box
* @param $presetid id of preset
* @param $fid id of file
* @param $module specific module
* @return array $form
*/
function imageoffsets($xoffset, $yoffset, $crop_width, $crop_height, $presetid, $fid, $module) {
$form['submit'] = array('#type' => 'submit', '#value' => t('Create crop'));
$form['image-crop-x'] = array('#type' => 'hidden', '#default_value' => $xoffset, '#attributes' => array('class' => 'edit-image-crop-x'));
$form['image-crop-y'] = array('#type' => 'hidden', '#default_value' => $yoffset, '#attributes' => array('class' => 'edit-image-crop-y'));
$form['image-crop-width'] = array('#type' => 'hidden', '#default_value' => $crop_width, '#attributes' => array('class' => 'edit-image-crop-width'));
$form['image-crop-height'] = array('#type' => 'hidden', '#default_value' => $crop_height, '#attributes' => array('class' => 'edit-image-crop-height'));
$form['fid'] = array('#type' => 'hidden', '#value' => $fid);
$form['module'] = array('#type' => 'hidden', '#value' => $module);
$form['presetid'] = array('#type' => 'hidden', '#value' => $presetid);
return $form;
}
/**
* Save the offset & size values
*
* @param $form_id id of the form
* @param $form_values submitted values of the imageoffsets form
*/
function imageoffsets_submit($form_id, $form_values) {
$module = (!empty($form_values['module'])) ? '/'. $form_values['module'] : '';
$reference = (!empty($form_values['module'])) ? $form_values['module'] : 'files';
db_query("DELETE FROM {imagecrop} WHERE fid=%d AND presetid=%d AND reference = '%s'", $form_values['fid'], $form_values['presetid'], $reference);
db_query("INSERT INTO {imagecrop} VALUES (%d,%d,'%s',%d,%d,%d,%d)", $form_values['fid'], $form_values['presetid'], $reference, $form_values['image-crop-x'], $form_values['image-crop-y'], $form_values['image-crop-width'], $form_values['image-crop-height']);
drupal_goto('imagecrop/showcrop/'. $form_values['fid'] .'/'. $form_values['presetid'] . $module);
}
/**
* Show the cropped image.
*
* @param $fid file id
* @param $presetid id of preset
* @return cropped version of chosen image
*/
function imagecrop_showcrop($fid, $presetid = 0, $module = '') {
imagecrop_markup(false, true);
if (imagecrop_action_exists() == true) {
$presets = return_presets($presetid);
$presetid = $presets['presetid'];
$file = create_image_object($fid, $presetid, $module, TRUE);
if ($file != false) {
$module = (!empty($module)) ? '/'. $module : '';
$output = theme('presettabs', $presets, $fid, $presetid, $module);
$output .= ''. l(t('Click here to choose another crop area for this picture'), 'imagecrop/docrop/'. $fid .'/'. $presetid . $module) .'
';
$output .= theme('imagecache', $file->presetname, $file->filepath);
return $output;
}
else {
return ''. t('Image to crop was not found.') .'
';
}
}
else {
return ''. t('No preset is found with the javascript_crop action so far. If you want to take advantage of this module, you will need to create at least one preset with that action.') .'
';
}
}
/**
* Helper function to determine which preset exists and which to load
*
* @param $presetid id of preset
* @return $presets array with presetid to load and list of all other possible presets
*/
function return_presets($presetid) {
$result = db_query("SELECT ip.presetid, ip.presetname FROM {imagecache_preset} ip INNER JOIN {imagecache_action} ia on ia.presetid = ip.presetid WHERE action = 'imagecrop_javascript' ORDER by ip.presetname ASC");
while ($row = db_fetch_object($result)) {
$presets['tabs'][] = array('id' => $row->presetid, 'name' => $row->presetname);
}
if (!empty($presetid)) $presets['presetid'] = $presetid;
else $presets['presetid'] = $presets['tabs'][0]['id'];
return $presets;
}
/**
* Helper function to create image
*
* @param $fid file id in files table
* @param $presetid id of the preset
* @param $cutoff delete the javascript crop action when user wants to define the offsets
* @return $file with file, javascript crop and preset properties
*/
function create_image_object($fid, $presetid, $module = '', $cutoff = false) {
$file = _imagecrop_file_load($fid, $module);
if ($file != false) {
$preset = imagecache_preset($presetid);
imagecache_image_flush($file->filename);
if ($cutoff == false) {
// get the actions from the preset and and throw out the javascript_crop action
// and every other action which comes after it.
$actions_count = count($preset['actions']);
while (list($key, $val) = each($preset['actions'])) {
if ($val['action'] == 'imagecrop_javascript') {
$crop_width = $preset['actions'][$key]['data']['width'];
$crop_height = $preset['actions'][$key]['data']['height'];
$resizable = $preset['actions'][$key]['data']['resizable'];
unset($preset['actions'][$key]);
// stop the loop, we don't need anything after this
break;
}
}
// see if we have stored values allready for this file
$file->xoffset = 0;
$file->yoffset = 0;
$file->crop_width = $crop_width;
$file->crop_height = $crop_height;
$reference = (!empty($module)) ? $module : 'files';
$row = db_fetch_object(db_query("SELECT xoffset,yoffset,width,height FROM {imagecrop} ic where ic.fid = %d AND ic.presetid = %d AND ic.reference = '%s'", $fid, $presetid, $reference));
if (!empty($row)) {
$file->xoffset = $row->xoffset;
$file->yoffset = $row->yoffset;
$file->crop_width = $row->width;
$file->crop_height = $row->height;
}
// resizable or not
$file->resizable = $resizable;
}
$src = $file->filepath;
$file->presetname = $preset['presetname'];
$dst = imagecache_create_path($preset['presetname'], $file->filepath);
$file->dst = $dst;
// create the file to either display for the crop or the result,
// we also set a global presetid variable, so I can use this in
// the javascript_crop action
$GLOBALS['imagecrop_presetid'] = $presetid;
imagecache_build_derivative($preset['actions'], $src, $dst);
return $file;
}
else return false;
}
/**
* Helper function to load a file into an object
*
* @param $fid file id
* @param $module specific module which does not use the files table
* @return $file with properties of the file or false
*/
function _imagecrop_file_load($fid, $module) {
// standard files table
if (empty($module))
$result = db_query('SELECT * FROM {files} WHERE fid = %d', $fid);
// node_images table
else if ($module == 'node_images') {
$result = db_query('SELECT * FROM {node_images} WHERE id = %d', $fid);
}
$file = db_fetch_object($result);
if ($file) {
// make sure it's an image. Any other mime extensions possible?
// return false if it's not the right mime type
$filemime = array('image/jpeg', 'image/gif', 'image/png', 'image/pjpeg');
if (!in_array($file->filemime, $filemime)) return false;
// access denied if current user hasn't enough permissions
$node = node_load($file->nid);
if (!user_access('administer nodes') && !user_access('edit '. $node->type .' content') && !user_access('edit own '. $node->type .' content')) {
drupal_access_denied();
exit();
}
// all seems ok, return file
return $file;
}
// return false if no file was found.
return false;
}
/**
* Theme image crop.
*
* @param $url url of image
* @param $width width of image
* @param $height height of image
* @param $resize wether the cropping box is resizeble or not
* @return $output html of the javascript crop area
*/
function theme_imagecrop($url, $width, $height, $resize = 0) {
$output = '
';
if ($resize == 1) {
$output .= '
';
}
$output .= '
';
return $output;
}
/**
* Theme preset tabs
*
* @param $tabs array of available presets
* @param fid file id
* @param $presetid preset to highlight
* @return $output html of the tabs
*/
function theme_presettabs($presets, $fid, $presetid, $module = '') {
$tab_output = '';
foreach ($presets['tabs'] as $key => $value) {
$class = ($value['id'] == $presetid) ? 'imagecrop_tab imagecrop_highlight' : 'imagecrop_tab ';
$url = 'imagecrop/showcrop/'. $fid .'/'. $value['id'] . $module;
$tab_output .= ''. l($value['name'], $url) .'';
}
$output = ''. t('Imagecache presets »') .' '. $tab_output .'
';
return $output;
}
/**
* Add imagecrop css & javascript
*/
function imagecrop_markup($js, $css) {
$path = drupal_get_path('module', 'imagecrop');
if ($js == true) drupal_add_js($path .'/imagecrop.js');
if ($css == true) drupal_add_css($path .'/imagecrop.css');
}