image.admin.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. // Use a strict (===) comparison since $key can be 0.
  673. 'class' => !empty($form[$key]['weight']['#access']) || $key === 'new' ? array('draggable') : array(),
  674. );
  675. }
  676. }
  677. $header = array(
  678. t('Effect'),
  679. t('Weight'),
  680. array('data' => t('Operations'), 'colspan' => 2),
  681. );
  682. if (count($rows) == 1 && $form['new']['#access']) {
  683. array_unshift($rows, array(array(
  684. 'data' => t('There are currently no effects in this style. Add one by selecting an option below.'),
  685. 'colspan' => 4,
  686. )));
  687. }
  688. $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'image-style-effects')));
  689. drupal_add_tabledrag('image-style-effects', 'order', 'sibling', 'image-effect-order-weight');
  690. return $output;
  691. }
  692. /**
  693. * Returns HTML for a preview of an image style.
  694. *
  695. * @param $variables
  696. * An associative array containing:
  697. * - style: The image style array being previewed.
  698. *
  699. * @ingroup themeable
  700. */
  701. function theme_image_style_preview($variables) {
  702. $style = $variables['style'];
  703. $sample_image = variable_get('image_style_preview_image', drupal_get_path('module', 'image') . '/sample.png');
  704. $sample_width = 160;
  705. $sample_height = 160;
  706. // Set up original file information.
  707. $original_path = $sample_image;
  708. $original_image = image_get_info($original_path);
  709. if ($original_image['width'] > $original_image['height']) {
  710. $original_width = min($original_image['width'], $sample_width);
  711. $original_height = round($original_width / $original_image['width'] * $original_image['height']);
  712. }
  713. else {
  714. $original_height = min($original_image['height'], $sample_height);
  715. $original_width = round($original_height / $original_image['height'] * $original_image['width']);
  716. }
  717. $original_attributes = array_intersect_key($original_image, array('width' => '', 'height' => ''));
  718. $original_attributes['style'] = 'width: ' . $original_width . 'px; height: ' . $original_height . 'px;';
  719. // Set up preview file information.
  720. $preview_file = image_style_path($style['name'], $original_path);
  721. if (!file_exists($preview_file)) {
  722. image_style_create_derivative($style, $original_path, $preview_file);
  723. }
  724. $preview_image = image_get_info($preview_file);
  725. if ($preview_image['width'] > $preview_image['height']) {
  726. $preview_width = min($preview_image['width'], $sample_width);
  727. $preview_height = round($preview_width / $preview_image['width'] * $preview_image['height']);
  728. }
  729. else {
  730. $preview_height = min($preview_image['height'], $sample_height);
  731. $preview_width = round($preview_height / $preview_image['height'] * $preview_image['width']);
  732. }
  733. $preview_attributes = array_intersect_key($preview_image, array('width' => '', 'height' => ''));
  734. $preview_attributes['style'] = 'width: ' . $preview_width . 'px; height: ' . $preview_height . 'px;';
  735. // In the previews, timestamps are added to prevent caching of images.
  736. $output = '<div class="image-style-preview preview clearfix">';
  737. // Build the preview of the original image.
  738. $original_url = file_create_url($original_path);
  739. $output .= '<div class="preview-image-wrapper">';
  740. $output .= t('original') . ' (' . l(t('view actual size'), $original_url) . ')';
  741. $output .= '<div class="preview-image original-image" style="' . $original_attributes['style'] . '">';
  742. $output .= '<a href="' . $original_url . '">' . theme('image', array('path' => $original_path, 'alt' => t('Sample original image'), 'title' => '', 'attributes' => $original_attributes)) . '</a>';
  743. $output .= '<div class="height" style="height: ' . $original_height . 'px"><span>' . $original_image['height'] . 'px</span></div>';
  744. $output .= '<div class="width" style="width: ' . $original_width . 'px"><span>' . $original_image['width'] . 'px</span></div>';
  745. $output .= '</div>'; // End preview-image.
  746. $output .= '</div>'; // End preview-image-wrapper.
  747. // Build the preview of the image style.
  748. $preview_url = file_create_url($preview_file) . '?cache_bypass=' . REQUEST_TIME;
  749. $output .= '<div class="preview-image-wrapper">';
  750. $output .= check_plain($style['label']) . ' (' . l(t('view actual size'), file_create_url($preview_file) . '?' . time()) . ')';
  751. $output .= '<div class="preview-image modified-image" style="' . $preview_attributes['style'] . '">';
  752. $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>';
  753. $output .= '<div class="height" style="height: ' . $preview_height . 'px"><span>' . $preview_image['height'] . 'px</span></div>';
  754. $output .= '<div class="width" style="width: ' . $preview_width . 'px"><span>' . $preview_image['width'] . 'px</span></div>';
  755. $output .= '</div>'; // End preview-image.
  756. $output .= '</div>'; // End preview-image-wrapper.
  757. $output .= '</div>'; // End image-style-preview.
  758. return $output;
  759. }
  760. /**
  761. * Returns HTML for a 3x3 grid of checkboxes for image anchors.
  762. *
  763. * @param $variables
  764. * An associative array containing:
  765. * - element: A render element containing radio buttons.
  766. *
  767. * @ingroup themeable
  768. */
  769. function theme_image_anchor($variables) {
  770. $element = $variables['element'];
  771. $rows = array();
  772. $row = array();
  773. foreach (element_children($element) as $n => $key) {
  774. $element[$key]['#attributes']['title'] = $element[$key]['#title'];
  775. unset($element[$key]['#title']);
  776. $row[] = drupal_render($element[$key]);
  777. if ($n % 3 == 3 - 1) {
  778. $rows[] = $row;
  779. $row = array();
  780. }
  781. }
  782. return theme('table', array('header' => array(), 'rows' => $rows, 'attributes' => array('class' => array('image-anchor'))));
  783. }
  784. /**
  785. * Returns HTML for a summary of an image resize effect.
  786. *
  787. * @param $variables
  788. * An associative array containing:
  789. * - data: The current configuration for this resize effect.
  790. *
  791. * @ingroup themeable
  792. */
  793. function theme_image_resize_summary($variables) {
  794. $data = $variables['data'];
  795. if ($data['width'] && $data['height']) {
  796. return check_plain($data['width']) . 'x' . check_plain($data['height']);
  797. }
  798. else {
  799. return ($data['width']) ? t('width @width', array('@width' => $data['width'])) : t('height @height', array('@height' => $data['height']));
  800. }
  801. }
  802. /**
  803. * Returns HTML for a summary of an image scale effect.
  804. *
  805. * @param $variables
  806. * An associative array containing:
  807. * - data: The current configuration for this scale effect.
  808. *
  809. * @ingroup themeable
  810. */
  811. function theme_image_scale_summary($variables) {
  812. $data = $variables['data'];
  813. return theme('image_resize_summary', array('data' => $data)) . ' ' . ($data['upscale'] ? '(' . t('upscaling allowed') . ')' : '');
  814. }
  815. /**
  816. * Returns HTML for a summary of an image crop effect.
  817. *
  818. * @param $variables
  819. * An associative array containing:
  820. * - data: The current configuration for this crop effect.
  821. *
  822. * @ingroup themeable
  823. */
  824. function theme_image_crop_summary($variables) {
  825. return theme('image_resize_summary', $variables);
  826. }
  827. /**
  828. * Returns HTML for a summary of an image rotate effect.
  829. *
  830. * @param $variables
  831. * An associative array containing:
  832. * - data: The current configuration for this rotate effect.
  833. *
  834. * @ingroup themeable
  835. */
  836. function theme_image_rotate_summary($variables) {
  837. $data = $variables['data'];
  838. return ($data['random']) ? t('random between -@degrees&deg and @degrees&deg', array('@degrees' => str_replace('-', '', $data['degrees']))) : t('@degrees&deg', array('@degrees' => $data['degrees']));
  839. }