image.admin.inc

  1. 7.x drupal/modules/image/image.admin.inc
  2. 8.x drupal/core/modules/image/image.admin.inc

Administration pages for image settings.

Functions

Namesort descending Description
image_crop_form Form structure for the image crop form.
image_effect_color_validate Element validate handler to ensure a hexadecimal color value.
image_effect_delete_form Form builder; Form for deleting an image effect.
image_effect_delete_form_submit Submit handler to delete an image effect.
image_effect_form Form builder; Form for adding and editing image effects.
image_effect_form_submit Submit handler for updating an image effect.
image_effect_integer_validate Element validate handler to ensure an integer pixel value.
image_effect_scale_validate Element validate handler to ensure that either a height or a width is specified.
image_resize_form Form structure for the image resize form.
image_rotate_form Form structure for the image rotate form.
image_scale_form Form structure for the image scale form.
image_style_add_form Form builder; Form for adding a new image style.
image_style_add_form_submit Submit handler for adding a new image style.
image_style_delete_form Form builder; Form for deleting an image style.
image_style_delete_form_submit Submit handler to delete an image style.
image_style_form Form builder; Edit an image style name and effects order.
image_style_form_add_submit Submit handler for adding a new image effect to an image style.
image_style_form_add_validate Validate handler for adding a new image effect to an image style.
image_style_form_override_submit Submit handler for overriding a module-defined style.
image_style_form_submit Submit handler for saving an image style.
image_style_list Menu callback; Listing of all current image styles.
image_style_name_validate Element validate function to ensure unique, URL safe style names.
image_style_revert_form Confirmation form to revert a database style to its default.
image_style_revert_form_submit Submit handler to convert an overridden style to its default.
theme_image_anchor Returns HTML for a 3x3 grid of checkboxes for image anchors.
theme_image_crop_summary Returns HTML for a summary of an image crop effect.
theme_image_resize_summary Returns HTML for a summary of an image resize effect.
theme_image_rotate_summary Returns HTML for a summary of an image rotate effect.
theme_image_scale_summary Returns HTML for a summary of an image scale effect.
theme_image_style_effects Returns HTML for a listing of the effects within a specific image style.
theme_image_style_list Returns HTML for the page containing the list of image styles.
theme_image_style_preview Returns HTML for a preview of an image style.

File

drupal/modules/image/image.admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Administration pages for image settings.
  5. */
  6. /**
  7. * Menu callback; Listing of all current image styles.
  8. */
  9. function image_style_list() {
  10. $page = array();
  11. $styles = image_styles();
  12. $page['image_style_list'] = array(
  13. '#markup' => theme('image_style_list', array('styles' => $styles)),
  14. '#attached' => array(
  15. 'css' => array(drupal_get_path('module', 'image') . '/image.admin.css' => array()),
  16. ),
  17. );
  18. return $page;
  19. }
  20. /**
  21. * Form builder; Edit an image style name and effects order.
  22. *
  23. * @param $form_state
  24. * An associative array containing the current state of the form.
  25. * @param $style
  26. * An image style array.
  27. * @ingroup forms
  28. * @see image_style_form_submit()
  29. */
  30. function image_style_form($form, &$form_state, $style) {
  31. $title = t('Edit %name style', array('%name' => $style['label']));
  32. drupal_set_title($title, PASS_THROUGH);
  33. // Adjust this form for styles that must be overridden to edit.
  34. $editable = (bool) ($style['storage'] & IMAGE_STORAGE_EDITABLE);
  35. if (!$editable && empty($form_state['input'])) {
  36. drupal_set_message(t('This image style is currently being provided by a module. Click the "Override defaults" button to change its settings.'), 'warning');
  37. }
  38. $form_state['image_style'] = $style;
  39. $form['#tree'] = TRUE;
  40. $form['#attached']['css'][drupal_get_path('module', 'image') . '/image.admin.css'] = array();
  41. // Show the thumbnail preview.
  42. $form['preview'] = array(
  43. '#type' => 'item',
  44. '#title' => t('Preview'),
  45. '#markup' => theme('image_style_preview', array('style' => $style)),
  46. );
  47. // Show the Image Style label.
  48. $form['label'] = array(
  49. '#type' => 'textfield',
  50. '#title' => t('Image style name'),
  51. '#default_value' => $style['label'],
  52. '#disabled' => !$editable,
  53. '#required' => TRUE,
  54. );
  55. // Allow the name of the style to be changed, unless this style is
  56. // provided by a module's hook_default_image_styles().
  57. $form['name'] = array(
  58. '#type' => 'machine_name',
  59. '#size' => '64',
  60. '#default_value' => $style['name'],
  61. '#disabled' => !$editable,
  62. '#description' => t('The name is used in URLs for generated images. Use only lowercase alphanumeric characters, underscores (_), and hyphens (-).'),
  63. '#required' => TRUE,
  64. '#machine_name' => array(
  65. 'exists' => 'image_style_load',
  66. 'source' => array('label'),
  67. 'replace_pattern' => '[^0-9a-z_\-]',
  68. 'error' => t('Please only use lowercase alphanumeric characters, underscores (_), and hyphens (-) for style names.'),
  69. ),
  70. );
  71. // Build the list of existing image effects for this image style.
  72. $form['effects'] = array(
  73. '#theme' => 'image_style_effects',
  74. );
  75. foreach ($style['effects'] as $key => $effect) {
  76. $form['effects'][$key]['#weight'] = isset($form_state['input']['effects']) ? $form_state['input']['effects'][$key]['weight'] : NULL;
  77. $form['effects'][$key]['label'] = array(
  78. '#markup' => $effect['label'],
  79. );
  80. $form['effects'][$key]['summary'] = array(
  81. '#markup' => isset($effect['summary theme']) ? theme($effect['summary theme'], array('data' => $effect['data'])) : '',
  82. );
  83. $form['effects'][$key]['weight'] = array(
  84. '#type' => 'weight',
  85. '#title' => t('Weight for @title', array('@title' => $effect['label'])),
  86. '#title_display' => 'invisible',
  87. '#default_value' => $effect['weight'],
  88. '#access' => $editable,
  89. );
  90. // Only attempt to display these fields for editable styles as the 'ieid'
  91. // key is not set for styles defined in code.
  92. if ($editable) {
  93. $form['effects'][$key]['configure'] = array(
  94. '#type' => 'link',
  95. '#title' => t('edit'),
  96. '#href' => 'admin/config/media/image-styles/edit/' . $style['name'] . '/effects/' . $effect['ieid'],
  97. '#access' => $editable && isset($effect['form callback']),
  98. );
  99. $form['effects'][$key]['remove'] = array(
  100. '#type' => 'link',
  101. '#title' => t('delete'),
  102. '#href' => 'admin/config/media/image-styles/edit/' . $style['name'] . '/effects/' . $effect['ieid'] . '/delete',
  103. '#access' => $editable,
  104. );
  105. }
  106. }
  107. // Build the new image effect addition form and add it to the effect list.
  108. $new_effect_options = array();
  109. foreach (image_effect_definitions() as $effect => $definition) {
  110. $new_effect_options[$effect] = check_plain($definition['label']);
  111. }
  112. $form['effects']['new'] = array(
  113. '#tree' => FALSE,
  114. '#weight' => isset($form_state['input']['weight']) ? $form_state['input']['weight'] : NULL,
  115. '#access' => $editable,
  116. );
  117. $form['effects']['new']['new'] = array(
  118. '#type' => 'select',
  119. '#title' => t('Effect'),
  120. '#title_display' => 'invisible',
  121. '#options' => $new_effect_options,
  122. '#empty_option' => t('Select a new effect'),
  123. );
  124. $form['effects']['new']['weight'] = array(
  125. '#type' => 'weight',
  126. '#title' => t('Weight for new effect'),
  127. '#title_display' => 'invisible',
  128. '#default_value' => count($form['effects']) - 1,
  129. );
  130. $form['effects']['new']['add'] = array(
  131. '#type' => 'submit',
  132. '#value' => t('Add'),
  133. '#validate' => array('image_style_form_add_validate'),
  134. '#submit' => array('image_style_form_submit', 'image_style_form_add_submit'),
  135. );
  136. // Show the Override or Submit button for this style.
  137. $form['actions'] = array('#type' => 'actions');
  138. $form['actions']['override'] = array(
  139. '#type' => 'submit',
  140. '#value' => t('Override defaults'),
  141. '#validate' => array(),
  142. '#submit' => array('image_style_form_override_submit'),
  143. '#access' => !$editable,
  144. );
  145. $form['actions']['submit'] = array(
  146. '#type' => 'submit',
  147. '#value' => t('Update style'),
  148. '#access' => $editable,
  149. );
  150. return $form;
  151. }
  152. /**
  153. * Validate handler for adding a new image effect to an image style.
  154. */
  155. function image_style_form_add_validate($form, &$form_state) {
  156. if (!$form_state['values']['new']) {
  157. form_error($form['effects']['new']['new'], t('Select an effect to add.'));
  158. }
  159. }
  160. /**
  161. * Submit handler for adding a new image effect to an image style.
  162. */
  163. function image_style_form_add_submit($form, &$form_state) {
  164. $style = $form_state['image_style'];
  165. // Check if this field has any configuration options.
  166. $effect = image_effect_definition_load($form_state['values']['new']);
  167. // Load the configuration form for this option.
  168. if (isset($effect['form callback'])) {
  169. $path = 'admin/config/media/image-styles/edit/' . $form_state['image_style']['name'] . '/add/' . $form_state['values']['new'];
  170. $form_state['redirect'] = array($path, array('query' => array('weight' => $form_state['values']['weight'])));
  171. }
  172. // If there's no form, immediately add the image effect.
  173. else {
  174. $effect['isid'] = $style['isid'];
  175. $effect['weight'] = $form_state['values']['weight'];
  176. image_effect_save($effect);
  177. drupal_set_message(t('The image effect was successfully applied.'));
  178. }
  179. }
  180. /**
  181. * Submit handler for overriding a module-defined style.
  182. */
  183. function image_style_form_override_submit($form, &$form_state) {
  184. drupal_set_message(t('The %style style has been overridden, allowing you to change its settings.', array('%style' => $form_state['image_style']['label'])));
  185. image_default_style_save($form_state['image_style']);
  186. }
  187. /**
  188. * Submit handler for saving an image style.
  189. */
  190. function image_style_form_submit($form, &$form_state) {
  191. // Update the image style.
  192. $style = $form_state['image_style'];
  193. $style['name'] = $form_state['values']['name'];
  194. $style['label'] = $form_state['values']['label'];
  195. // Update image effect weights.
  196. if (!empty($form_state['values']['effects'])) {
  197. foreach ($form_state['values']['effects'] as $ieid => $effect_data) {
  198. if (isset($style['effects'][$ieid])) {
  199. $effect = $style['effects'][$ieid];
  200. $effect['weight'] = $effect_data['weight'];
  201. image_effect_save($effect);
  202. }
  203. }
  204. }
  205. image_style_save($style);
  206. if ($form_state['values']['op'] == t('Update style')) {
  207. drupal_set_message(t('Changes to the style have been saved.'));
  208. }
  209. $form_state['redirect'] = 'admin/config/media/image-styles/edit/' . $style['name'];
  210. }
  211. /**
  212. * Form builder; Form for adding a new image style.
  213. *
  214. * @ingroup forms
  215. * @see image_style_add_form_submit()
  216. */
  217. function image_style_add_form($form, &$form_state) {
  218. $form['label'] = array(
  219. '#type' => 'textfield',
  220. '#title' => t('Style name'),
  221. '#default_value' => '',
  222. '#required' => TRUE,
  223. );
  224. $form['name'] = array(
  225. '#type' => 'machine_name',
  226. '#description' => t('The name is used in URLs for generated images. Use only lowercase alphanumeric characters, underscores (_), and hyphens (-).'),
  227. '#size' => '64',
  228. '#required' => TRUE,
  229. '#machine_name' => array(
  230. 'exists' => 'image_style_load',
  231. 'source' => array('label'),
  232. 'replace_pattern' => '[^0-9a-z_\-]',
  233. 'error' => t('Please only use lowercase alphanumeric characters, underscores (_), and hyphens (-) for style names.'),
  234. ),
  235. );
  236. $form['submit'] = array(
  237. '#type' => 'submit',
  238. '#value' => t('Create new style'),
  239. );
  240. return $form;
  241. }
  242. /**
  243. * Submit handler for adding a new image style.
  244. */
  245. function image_style_add_form_submit($form, &$form_state) {
  246. $style = array(
  247. 'name' => $form_state['values']['name'],
  248. 'label' => $form_state['values']['label'],
  249. );
  250. $style = image_style_save($style);
  251. drupal_set_message(t('Style %name was created.', array('%name' => $style['label'])));
  252. $form_state['redirect'] = 'admin/config/media/image-styles/edit/' . $style['name'];
  253. }
  254. /**
  255. * Element validate function to ensure unique, URL safe style names.
  256. *
  257. * This function is no longer used in Drupal core since image style names are
  258. * now validated using #machine_name functionality. It is kept for backwards
  259. * compatibility (since non-core modules may be using it) and will be removed
  260. * in Drupal 8.
  261. */
  262. function image_style_name_validate($element, $form_state) {
  263. // Check for duplicates.
  264. $styles = image_styles();
  265. if (isset($styles[$element['#value']]) && (!isset($form_state['image_style']['isid']) || $styles[$element['#value']]['isid'] != $form_state['image_style']['isid'])) {
  266. form_set_error($element['#name'], t('The image style name %name is already in use.', array('%name' => $element['#value'])));
  267. }
  268. // Check for illegal characters in image style names.
  269. if (preg_match('/[^0-9a-z_\-]/', $element['#value'])) {
  270. form_set_error($element['#name'], t('Please only use lowercase alphanumeric characters, underscores (_), and hyphens (-) for style names.'));
  271. }
  272. }
  273. /**
  274. * Form builder; Form for deleting an image style.
  275. *
  276. * @param $style
  277. * An image style array.
  278. *
  279. * @ingroup forms
  280. * @see image_style_delete_form_submit()
  281. */
  282. function image_style_delete_form($form, &$form_state, $style) {
  283. $form_state['image_style'] = $style;
  284. $replacement_styles = array_diff_key(image_style_options(TRUE, PASS_THROUGH), array($style['name'] => ''));
  285. $form['replacement'] = array(
  286. '#title' => t('Replacement style'),
  287. '#type' => 'select',
  288. '#options' => $replacement_styles,
  289. '#empty_option' => t('No replacement, just delete'),
  290. );
  291. return confirm_form(
  292. $form,
  293. t('Optionally select a style before deleting %style', array('%style' => $style['label'])),
  294. 'admin/config/media/image-styles',
  295. t('If this style is in use on the site, you may select another style to replace it. All images that have been generated for this style will be permanently deleted.'),
  296. t('Delete'), t('Cancel')
  297. );
  298. }
  299. /**
  300. * Submit handler to delete an image style.
  301. */
  302. function image_style_delete_form_submit($form, &$form_state) {
  303. $style = $form_state['image_style'];
  304. image_style_delete($style, $form_state['values']['replacement']);
  305. drupal_set_message(t('Style %name was deleted.', array('%name' => $style['label'])));
  306. $form_state['redirect'] = 'admin/config/media/image-styles';
  307. }
  308. /**
  309. * Confirmation form to revert a database style to its default.
  310. */
  311. function image_style_revert_form($form, &$form_state, $style) {
  312. $form_state['image_style'] = $style;
  313. return confirm_form(
  314. $form,
  315. t('Revert the %style style?', array('%style' => $style['label'])),
  316. 'admin/config/media/image-styles',
  317. t('Reverting this style will delete the customized settings and restore the defaults provided by the @module module.', array('@module' => $style['module'])),
  318. t('Revert'), t('Cancel')
  319. );
  320. }
  321. /**
  322. * Submit handler to convert an overridden style to its default.
  323. */
  324. function image_style_revert_form_submit($form, &$form_state) {
  325. drupal_set_message(t('The %style style has been reverted to its defaults.', array('%style' => $form_state['image_style']['label'])));
  326. image_default_style_revert($form_state['image_style']);
  327. $form_state['redirect'] = 'admin/config/media/image-styles';
  328. }
  329. /**
  330. * Form builder; Form for adding and editing image effects.
  331. *
  332. * This form is used universally for editing all image effects. Each effect adds
  333. * its own custom section to the form by calling the form function specified in
  334. * hook_image_effects().
  335. *
  336. * @param $form_state
  337. * An associative array containing the current state of the form.
  338. * @param $style
  339. * An image style array.
  340. * @param $effect
  341. * An image effect array.
  342. *
  343. * @ingroup forms
  344. * @see hook_image_effects()
  345. * @see image_effects()
  346. * @see image_resize_form()
  347. * @see image_scale_form()
  348. * @see image_rotate_form()
  349. * @see image_crop_form()
  350. * @see image_effect_form_submit()
  351. */
  352. function image_effect_form($form, &$form_state, $style, $effect) {
  353. if (!empty($effect['data'])) {
  354. $title = t('Edit %label effect', array('%label' => $effect['label']));
  355. }
  356. else{
  357. $title = t('Add %label effect', array('%label' => $effect['label']));
  358. }
  359. drupal_set_title($title, PASS_THROUGH);
  360. $form_state['image_style'] = $style;
  361. $form_state['image_effect'] = $effect;
  362. // If no configuration for this image effect, return to the image style page.
  363. if (!isset($effect['form callback'])) {
  364. drupal_goto('admin/config/media/image-styles/edit/' . $style['name']);
  365. }
  366. $form['#tree'] = TRUE;
  367. $form['#attached']['css'][drupal_get_path('module', 'image') . '/image.admin.css'] = array();
  368. if (function_exists($effect['form callback'])) {
  369. $form['data'] = call_user_func($effect['form callback'], $effect['data']);
  370. }
  371. // Check the URL for a weight, then the image effect, otherwise use default.
  372. $form['weight'] = array(
  373. '#type' => 'hidden',
  374. '#value' => isset($_GET['weight']) ? intval($_GET['weight']) : (isset($effect['weight']) ? $effect['weight'] : count($style['effects'])),
  375. );
  376. $form['actions'] = array('#tree' => FALSE, '#type' => 'actions');
  377. $form['actions']['submit'] = array(
  378. '#type' => 'submit',
  379. '#value' => isset($effect['ieid']) ? t('Update effect') : t('Add effect'),
  380. );
  381. $form['actions']['cancel'] = array(
  382. '#type' => 'link',
  383. '#title' => t('Cancel'),
  384. '#href' => 'admin/config/media/image-styles/edit/' . $style['name'],
  385. );
  386. return $form;
  387. }
  388. /**
  389. * Submit handler for updating an image effect.
  390. */
  391. function image_effect_form_submit($form, &$form_state) {
  392. $style = $form_state['image_style'];
  393. $effect = array_merge($form_state['image_effect'], $form_state['values']);
  394. $effect['isid'] = $style['isid'];
  395. image_effect_save($effect);
  396. drupal_set_message(t('The image effect was successfully applied.'));
  397. $form_state['redirect'] = 'admin/config/media/image-styles/edit/' . $style['name'];
  398. }
  399. /**
  400. * Form builder; Form for deleting an image effect.
  401. *
  402. * @param $style
  403. * Name of the image style from which the image effect will be removed.
  404. * @param $effect
  405. * Name of the image effect to remove.
  406. * @ingroup forms
  407. * @see image_effect_delete_form_submit()
  408. */
  409. function image_effect_delete_form($form, &$form_state, $style, $effect) {
  410. $form_state['image_style'] = $style;
  411. $form_state['image_effect'] = $effect;
  412. $question = t('Are you sure you want to delete the @effect effect from the %style style?', array('%style' => $style['label'], '@effect' => $effect['label']));
  413. return confirm_form($form, $question, 'admin/config/media/image-styles/edit/' . $style['name'], '', t('Delete'));
  414. }
  415. /**
  416. * Submit handler to delete an image effect.
  417. */
  418. function image_effect_delete_form_submit($form, &$form_state) {
  419. $style = $form_state['image_style'];
  420. $effect = $form_state['image_effect'];
  421. image_effect_delete($effect);
  422. drupal_set_message(t('The image effect %name has been deleted.', array('%name' => $effect['label'])));
  423. $form_state['redirect'] = 'admin/config/media/image-styles/edit/' . $style['name'];
  424. }
  425. /**
  426. * Element validate handler to ensure an integer pixel value.
  427. *
  428. * The property #allow_negative = TRUE may be set to allow negative integers.
  429. */
  430. function image_effect_integer_validate($element, &$form_state) {
  431. $value = empty($element['#allow_negative']) ? $element['#value'] : preg_replace('/^-/', '', $element['#value']);
  432. if ($element['#value'] != '' && (!is_numeric($value) || intval($value) <= 0)) {
  433. if (empty($element['#allow_negative'])) {
  434. form_error($element, t('!name must be an integer.', array('!name' => $element['#title'])));
  435. }
  436. else {
  437. form_error($element, t('!name must be a positive integer.', array('!name' => $element['#title'])));
  438. }
  439. }
  440. }
  441. /**
  442. * Element validate handler to ensure a hexadecimal color value.
  443. */
  444. function image_effect_color_validate($element, &$form_state) {
  445. if ($element['#value'] != '') {
  446. $hex_value = preg_replace('/^#/', '', $element['#value']);
  447. if (!preg_match('/^#[0-9A-F]{3}([0-9A-F]{3})?$/', $element['#value'])) {
  448. form_error($element, t('!name must be a hexadecimal color value.', array('!name' => $element['#title'])));
  449. }
  450. }
  451. }
  452. /**
  453. * Element validate handler to ensure that either a height or a width is
  454. * specified.
  455. */
  456. function image_effect_scale_validate($element, &$form_state) {
  457. if (empty($element['width']['#value']) && empty($element['height']['#value'])) {
  458. form_error($element, t('Width and height can not both be blank.'));
  459. }
  460. }
  461. /**
  462. * Form structure for the image resize form.
  463. *
  464. * Note that this is not a complete form, it only contains the portion of the
  465. * form for configuring the resize options. Therefore it does not not need to
  466. * include metadata about the effect, nor a submit button.
  467. *
  468. * @param $data
  469. * The current configuration for this resize effect.
  470. */
  471. function image_resize_form($data) {
  472. $form['width'] = array(
  473. '#type' => 'textfield',
  474. '#title' => t('Width'),
  475. '#default_value' => isset($data['width']) ? $data['width'] : '',
  476. '#field_suffix' => ' ' . t('pixels'),
  477. '#required' => TRUE,
  478. '#size' => 10,
  479. '#element_validate' => array('image_effect_integer_validate'),
  480. '#allow_negative' => FALSE,
  481. );
  482. $form['height'] = array(
  483. '#type' => 'textfield',
  484. '#title' => t('Height'),
  485. '#default_value' => isset($data['height']) ? $data['height'] : '',
  486. '#field_suffix' => ' ' . t('pixels'),
  487. '#required' => TRUE,
  488. '#size' => 10,
  489. '#element_validate' => array('image_effect_integer_validate'),
  490. '#allow_negative' => FALSE,
  491. );
  492. return $form;
  493. }
  494. /**
  495. * Form structure for the image scale form.
  496. *
  497. * Note that this is not a complete form, it only contains the portion of the
  498. * form for configuring the scale options. Therefore it does not not need to
  499. * include metadata about the effect, nor a submit button.
  500. *
  501. * @param $data
  502. * The current configuration for this scale effect.
  503. */
  504. function image_scale_form($data) {
  505. $form = image_resize_form($data);
  506. $form['#element_validate'] = array('image_effect_scale_validate');
  507. $form['width']['#required'] = FALSE;
  508. $form['height']['#required'] = FALSE;
  509. $form['upscale'] = array(
  510. '#type' => 'checkbox',
  511. '#default_value' => (isset($data['upscale'])) ? $data['upscale'] : 0,
  512. '#title' => t('Allow Upscaling'),
  513. '#description' => t('Let scale make images larger than their original size'),
  514. );
  515. return $form;
  516. }
  517. /**
  518. * Form structure for the image crop form.
  519. *
  520. * Note that this is not a complete form, it only contains the portion of the
  521. * form for configuring the crop options. Therefore it does not not need to
  522. * include metadata about the effect, nor a submit button.
  523. *
  524. * @param $data
  525. * The current configuration for this crop effect.
  526. */
  527. function image_crop_form($data) {
  528. $data += array(
  529. 'width' => '',
  530. 'height' => '',
  531. 'anchor' => 'center-center',
  532. );
  533. $form = image_resize_form($data);
  534. $form['anchor'] = array(
  535. '#type' => 'radios',
  536. '#title' => t('Anchor'),
  537. '#options' => array(
  538. 'left-top' => t('Top left'),
  539. 'center-top' => t('Top center'),
  540. 'right-top' => t('Top right'),
  541. 'left-center' => t('Center left'),
  542. 'center-center' => t('Center'),
  543. 'right-center' => t('Center right'),
  544. 'left-bottom' => t('Bottom left'),
  545. 'center-bottom' => t('Bottom center'),
  546. 'right-bottom' => t('Bottom right'),
  547. ),
  548. '#theme' => 'image_anchor',
  549. '#default_value' => $data['anchor'],
  550. '#description' => t('The part of the image that will be retained during the crop.'),
  551. );
  552. return $form;
  553. }
  554. /**
  555. * Form structure for the image rotate form.
  556. *
  557. * Note that this is not a complete form, it only contains the portion of the
  558. * form for configuring the rotate options. Therefore it does not not need to
  559. * include metadata about the effect, nor a submit button.
  560. *
  561. * @param $data
  562. * The current configuration for this rotate effect.
  563. */
  564. function image_rotate_form($data) {
  565. $form['degrees'] = array(
  566. '#type' => 'textfield',
  567. '#default_value' => (isset($data['degrees'])) ? $data['degrees'] : 0,
  568. '#title' => t('Rotation angle'),
  569. '#description' => t('The number of degrees the image should be rotated. Positive numbers are clockwise, negative are counter-clockwise.'),
  570. '#field_suffix' => '&deg;',
  571. '#required' => TRUE,
  572. '#size' => 6,
  573. '#maxlength' => 4,
  574. '#element_validate' => array('image_effect_integer_validate'),
  575. '#allow_negative' => TRUE,
  576. );
  577. $form['bgcolor'] = array(
  578. '#type' => 'textfield',
  579. '#default_value' => (isset($data['bgcolor'])) ? $data['bgcolor'] : '#FFFFFF',
  580. '#title' => t('Background color'),
  581. '#description' => t('The background color to use for exposed areas of the image. Use web-style hex colors (#FFFFFF for white, #000000 for black). Leave blank for transparency on image types that support it.'),
  582. '#size' => 7,
  583. '#maxlength' => 7,
  584. '#element_validate' => array('image_effect_color_validate'),
  585. );
  586. $form['random'] = array(
  587. '#type' => 'checkbox',
  588. '#default_value' => (isset($data['random'])) ? $data['random'] : 0,
  589. '#title' => t('Randomize'),
  590. '#description' => t('Randomize the rotation angle for each image. The angle specified above is used as a maximum.'),
  591. );
  592. return $form;
  593. }
  594. /**
  595. * Returns HTML for the page containing the list of image styles.
  596. *
  597. * @param $variables
  598. * An associative array containing:
  599. * - styles: An array of all the image styles returned by image_get_styles().
  600. *
  601. * @see image_get_styles()
  602. * @ingroup themeable
  603. */
  604. function theme_image_style_list($variables) {
  605. $styles = $variables['styles'];
  606. $header = array(t('Style name'), t('Settings'), array('data' => t('Operations'), 'colspan' => 3));
  607. $rows = array();
  608. foreach ($styles as $style) {
  609. $row = array();
  610. $row[] = l($style['label'], 'admin/config/media/image-styles/edit/' . $style['name']);
  611. $link_attributes = array(
  612. 'attributes' => array(
  613. 'class' => array('image-style-link'),
  614. ),
  615. );
  616. if ($style['storage'] == IMAGE_STORAGE_NORMAL) {
  617. $row[] = t('Custom');
  618. $row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes);
  619. $row[] = l(t('delete'), 'admin/config/media/image-styles/delete/' . $style['name'], $link_attributes);
  620. }
  621. elseif ($style['storage'] == IMAGE_STORAGE_OVERRIDE) {
  622. $row[] = t('Overridden');
  623. $row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes);
  624. $row[] = l(t('revert'), 'admin/config/media/image-styles/revert/' . $style['name'], $link_attributes);
  625. }
  626. else {
  627. $row[] = t('Default');
  628. $row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes);
  629. $row[] = '';
  630. }
  631. $rows[] = $row;
  632. }
  633. if (empty($rows)) {
  634. $rows[] = array(array(
  635. 'colspan' => 4,
  636. 'data' => t('There are currently no styles. <a href="!url">Add a new one</a>.', array('!url' => url('admin/config/media/image-styles/add'))),
  637. ));
  638. }
  639. return theme('table', array('header' => $header, 'rows' => $rows));
  640. }
  641. /**
  642. * Returns HTML for a listing of the effects within a specific image style.
  643. *
  644. * @param $variables
  645. * An associative array containing:
  646. * - form: A render element representing the form.
  647. *
  648. * @ingroup themeable
  649. */
  650. function theme_image_style_effects($variables) {
  651. $form = $variables['form'];
  652. $rows = array();
  653. foreach (element_children($form) as $key) {
  654. $row = array();
  655. $form[$key]['weight']['#attributes']['class'] = array('image-effect-order-weight');
  656. if (is_numeric($key)) {
  657. $summary = drupal_render($form[$key]['summary']);
  658. $row[] = drupal_render($form[$key]['label']) . (empty($summary) ? '' : ' ' . $summary);
  659. $row[] = drupal_render($form[$key]['weight']);
  660. $row[] = drupal_render($form[$key]['configure']);
  661. $row[] = drupal_render($form[$key]['remove']);
  662. }
  663. else {
  664. // Add the row for adding a new image effect.
  665. $row[] = '<div class="image-style-new">' . drupal_render($form['new']['new']) . drupal_render($form['new']['add']) . '</div>';
  666. $row[] = drupal_render($form['new']['weight']);
  667. $row[] = array('data' => '', 'colspan' => 2);
  668. }
  669. if (!isset($form[$key]['#access']) || $form[$key]['#access']) {
  670. $rows[] = array(
  671. 'data' => $row,
  672. 'class' => !empty($form[$key]['weight']['#access']) || $key == 'new' ? array('draggable') : array(),
  673. );
  674. }
  675. }
  676. $header = array(
  677. t('Effect'),
  678. t('Weight'),
  679. array('data' => t('Operations'), 'colspan' => 2),
  680. );
  681. if (count($rows) == 1 && $form['new']['#access']) {
  682. array_unshift($rows, array(array(
  683. 'data' => t('There are currently no effects in this style. Add one by selecting an option below.'),
  684. 'colspan' => 4,
  685. )));
  686. }
  687. $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'image-style-effects')));
  688. drupal_add_tabledrag('image-style-effects', 'order', 'sibling', 'image-effect-order-weight');
  689. return $output;
  690. }
  691. /**
  692. * Returns HTML for a preview of an image style.
  693. *
  694. * @param $variables
  695. * An associative array containing:
  696. * - style: The image style array being previewed.
  697. *
  698. * @ingroup themeable
  699. */
  700. function theme_image_style_preview($variables) {
  701. $style = $variables['style'];
  702. $sample_image = variable_get('image_style_preview_image', drupal_get_path('module', 'image') . '/sample.png');
  703. $sample_width = 160;
  704. $sample_height = 160;
  705. // Set up original file information.
  706. $original_path = $sample_image;
  707. $original_image = image_get_info($original_path);
  708. if ($original_image['width'] > $original_image['height']) {
  709. $original_width = min($original_image['width'], $sample_width);
  710. $original_height = round($original_width / $original_image['width'] * $original_image['height']);
  711. }
  712. else {
  713. $original_height = min($original_image['height'], $sample_height);
  714. $original_width = round($original_height / $original_image['height'] * $original_image['width']);
  715. }
  716. $original_attributes = array_intersect_key($original_image, array('width' => '', 'height' => ''));
  717. $original_attributes['style'] = 'width: ' . $original_width . 'px; height: ' . $original_height . 'px;';
  718. // Set up preview file information.
  719. $preview_file = image_style_path($style['name'], $original_path);
  720. if (!file_exists($preview_file)) {
  721. image_style_create_derivative($style, $original_path, $preview_file);
  722. }
  723. $preview_image = image_get_info($preview_file);
  724. if ($preview_image['width'] > $preview_image['height']) {
  725. $preview_width = min($preview_image['width'], $sample_width);
  726. $preview_height = round($preview_width / $preview_image['width'] * $preview_image['height']);
  727. }
  728. else {
  729. $preview_height = min($preview_image['height'], $sample_height);
  730. $preview_width = round($preview_height / $preview_image['height'] * $preview_image['width']);
  731. }
  732. $preview_attributes = array_intersect_key($preview_image, array('width' => '', 'height' => ''));
  733. $preview_attributes['style'] = 'width: ' . $preview_width . 'px; height: ' . $preview_height . 'px;';
  734. // In the previews, timestamps are added to prevent caching of images.
  735. $output = '<div class="image-style-preview preview clearfix">';
  736. // Build the preview of the original image.
  737. $original_url = file_create_url($original_path);
  738. $output .= '<div class="preview-image-wrapper">';
  739. $output .= t('original') . ' (' . l(t('view actual size'), $original_url) . ')';
  740. $output .= '<div class="preview-image original-image" style="' . $original_attributes['style'] . '">';
  741. $output .= '<a href="' . $original_url . '">' . theme('image', array('path' => $original_path, 'alt' => t('Sample original image'), 'title' => '', 'attributes' => $original_attributes)) . '</a>';
  742. $output .= '<div class="height" style="height: ' . $original_height . 'px"><span>' . $original_image['height'] . 'px</span></div>';
  743. $output .= '<div class="width" style="width: ' . $original_width . 'px"><span>' . $original_image['width'] . 'px</span></div>';
  744. $output .= '</div>'; // End preview-image.
  745. $output .= '</div>'; // End preview-image-wrapper.
  746. // Build the preview of the image style.
  747. $preview_url = file_create_url($preview_file) . '?cache_bypass=' . REQUEST_TIME;
  748. $output .= '<div class="preview-image-wrapper">';
  749. $output .= check_plain($style['label']) . ' (' . l(t('view actual size'), file_create_url($preview_file) . '?' . time()) . ')';
  750. $output .= '<div class="preview-image modified-image" style="' . $preview_attributes['style'] . '">';
  751. $output .= '<a href="' . file_create_url($preview_file) . '?' . time() . '">' . theme('image', array('path' => $preview_url, 'alt' => t('Sample modified image'), 'title' => '', 'attributes' => $preview_attributes)) . '</a>';
  752. $output .= '<div class="height" style="height: ' . $preview_height . 'px"><span>' . $preview_image['height'] . 'px</span></div>';
  753. $output .= '<div class="width" style="width: ' . $preview_width . 'px"><span>' . $preview_image['width'] . 'px</span></div>';
  754. $output .= '</div>'; // End preview-image.
  755. $output .= '</div>'; // End preview-image-wrapper.
  756. $output .= '</div>'; // End image-style-preview.
  757. return $output;
  758. }
  759. /**
  760. * Returns HTML for a 3x3 grid of checkboxes for image anchors.
  761. *
  762. * @param $variables
  763. * An associative array containing:
  764. * - element: A render element containing radio buttons.
  765. *
  766. * @ingroup themeable
  767. */
  768. function theme_image_anchor($variables) {
  769. $element = $variables['element'];
  770. $rows = array();
  771. $row = array();
  772. foreach (element_children($element) as $n => $key) {
  773. $element[$key]['#attributes']['title'] = $element[$key]['#title'];
  774. unset($element[$key]['#title']);
  775. $row[] = drupal_render($element[$key]);
  776. if ($n % 3 == 3 - 1) {
  777. $rows[] = $row;
  778. $row = array();
  779. }
  780. }
  781. return theme('table', array('header' => array(), 'rows' => $rows, 'attributes' => array('class' => array('image-anchor'))));
  782. }
  783. /**
  784. * Returns HTML for a summary of an image resize effect.
  785. *
  786. * @param $variables
  787. * An associative array containing:
  788. * - data: The current configuration for this resize effect.
  789. *
  790. * @ingroup themeable
  791. */
  792. function theme_image_resize_summary($variables) {
  793. $data = $variables['data'];
  794. if ($data['width'] && $data['height']) {
  795. return check_plain($data['width']) . 'x' . check_plain($data['height']);
  796. }
  797. else {
  798. return ($data['width']) ? t('width @width', array('@width' => $data['width'])) : t('height @height', array('@height' => $data['height']));
  799. }
  800. }
  801. /**
  802. * Returns HTML for a summary of an image scale effect.
  803. *
  804. * @param $variables
  805. * An associative array containing:
  806. * - data: The current configuration for this scale effect.
  807. *
  808. * @ingroup themeable
  809. */
  810. function theme_image_scale_summary($variables) {
  811. $data = $variables['data'];
  812. return theme('image_resize_summary', array('data' => $data)) . ' ' . ($data['upscale'] ? '(' . t('upscaling allowed') . ')' : '');
  813. }
  814. /**
  815. * Returns HTML for a summary of an image crop effect.
  816. *
  817. * @param $variables
  818. * An associative array containing:
  819. * - data: The current configuration for this crop effect.
  820. *
  821. * @ingroup themeable
  822. */
  823. function theme_image_crop_summary($variables) {
  824. return theme('image_resize_summary', $variables);
  825. }
  826. /**
  827. * Returns HTML for a summary of an image rotate effect.
  828. *
  829. * @param $variables
  830. * An associative array containing:
  831. * - data: The current configuration for this rotate effect.
  832. *
  833. * @ingroup themeable
  834. */
  835. function theme_image_rotate_summary($variables) {
  836. $data = $variables['data'];
  837. return ($data['random']) ? t('random between -@degrees&deg and @degrees&deg', array('@degrees' => str_replace('-', '', $data['degrees']))) : t('@degrees&deg', array('@degrees' => $data['degrees']));
  838. }