image.module 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. <?php
  2. /**
  3. * @file
  4. * Exposes global functionality for creating image styles.
  5. */
  6. /**
  7. * Image style constant for user presets in the database.
  8. */
  9. define('IMAGE_STORAGE_NORMAL', 1);
  10. /**
  11. * Image style constant for user presets that override module-defined presets.
  12. */
  13. define('IMAGE_STORAGE_OVERRIDE', 2);
  14. /**
  15. * Image style constant for module-defined presets in code.
  16. */
  17. define('IMAGE_STORAGE_DEFAULT', 4);
  18. /**
  19. * Image style constant to represent an editable preset.
  20. */
  21. define('IMAGE_STORAGE_EDITABLE', IMAGE_STORAGE_NORMAL | IMAGE_STORAGE_OVERRIDE);
  22. /**
  23. * Image style constant to represent any module-based preset.
  24. */
  25. define('IMAGE_STORAGE_MODULE', IMAGE_STORAGE_OVERRIDE | IMAGE_STORAGE_DEFAULT);
  26. // Load all Field module hooks for Image.
  27. require_once DRUPAL_ROOT . '/modules/image/image.field.inc';
  28. /**
  29. * Implements hook_help().
  30. */
  31. function image_help($path, $arg) {
  32. switch ($path) {
  33. case 'admin/help#image':
  34. $output = '';
  35. $output .= '<h3>' . t('About') . '</h3>';
  36. $output .= '<p>' . t('The Image module allows you to manipulate images on your website. It exposes a setting for using the <em>Image toolkit</em>, allows you to configure <em>Image styles</em> that can be used for resizing or adjusting images on display, and provides an <em>Image</em> field for attaching images to content. For more information, see the online handbook entry for <a href="@image">Image module</a>.', array('@image' => 'http://drupal.org/documentation/modules/image')) . '</p>';
  37. $output .= '<h3>' . t('Uses') . '</h3>';
  38. $output .= '<dl>';
  39. $output .= '<dt>' . t('Manipulating images') . '</dt>';
  40. $output .= '<dd>' . t('With the Image module you can scale, crop, resize, rotate and desaturate images without affecting the original image using <a href="@image">image styles</a>. When you change an image style, the module automatically refreshes all created images. Every image style must have a name, which will be used in the URL of the generated images. There are two common approaches to naming image styles (which you use will depend on how the image style is being applied):',array('@image' => url('admin/config/media/image-styles')));
  41. $output .= '<ul><li>' . t('Based on where it will be used: eg. <em>profile-picture</em>') . '</li>';
  42. $output .= '<li>' . t('Describing its appearance: eg. <em>square-85x85</em>') . '</li></ul>';
  43. $output .= t('After you create an image style, you can add effects: crop, scale, resize, rotate, and desaturate (other contributed modules provide additional effects). For example, by combining effects as crop, scale, and desaturate, you can create square, grayscale thumbnails.') . '<dd>';
  44. $output .= '<dt>' . t('Attaching images to content as fields') . '</dt>';
  45. $output .= '<dd>' . t("Image module also allows you to attach images to content as fields. To add an image field to a <a href='@content-type'>content type</a>, go to the content type's <em>manage fields</em> page, and add a new field of type <em>Image</em>. Attaching images to content this way allows image styles to be applied and maintained, and also allows you more flexibility when theming.", array('@content-type' => url('admin/structure/types'))) . '</dd>';
  46. $output .= '</dl>';
  47. return $output;
  48. case 'admin/config/media/image-styles':
  49. return '<p>' . t('Image styles commonly provide thumbnail sizes by scaling and cropping images, but can also add various effects before an image is displayed. When an image is displayed with a style, a new file is created and the original image is left unchanged.') . '</p>';
  50. case 'admin/config/media/image-styles/edit/%/add/%':
  51. $effect = image_effect_definition_load($arg[7]);
  52. return isset($effect['help']) ? ('<p>' . $effect['help'] . '</p>') : NULL;
  53. case 'admin/config/media/image-styles/edit/%/effects/%':
  54. $effect = ($arg[5] == 'add') ? image_effect_definition_load($arg[6]) : image_effect_load($arg[6], $arg[4]);
  55. return isset($effect['help']) ? ('<p>' . $effect['help'] . '</p>') : NULL;
  56. }
  57. }
  58. /**
  59. * Implements hook_menu().
  60. */
  61. function image_menu() {
  62. $items = array();
  63. // Generate image derivatives of publicly available files.
  64. // If clean URLs are disabled, image derivatives will always be served
  65. // through the menu system.
  66. // If clean URLs are enabled and the image derivative already exists,
  67. // PHP will be bypassed.
  68. $directory_path = file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath();
  69. $items[$directory_path . '/styles/%image_style'] = array(
  70. 'title' => 'Generate image style',
  71. 'page callback' => 'image_style_deliver',
  72. 'page arguments' => array(count(explode('/', $directory_path)) + 1),
  73. 'access callback' => TRUE,
  74. 'type' => MENU_CALLBACK,
  75. );
  76. // Generate and deliver image derivatives of private files.
  77. // These image derivatives are always delivered through the menu system.
  78. $items['system/files/styles/%image_style'] = array(
  79. 'title' => 'Generate image style',
  80. 'page callback' => 'image_style_deliver',
  81. 'page arguments' => array(3),
  82. 'access callback' => TRUE,
  83. 'type' => MENU_CALLBACK,
  84. );
  85. $items['admin/config/media/image-styles'] = array(
  86. 'title' => 'Image styles',
  87. 'description' => 'Configure styles that can be used for resizing or adjusting images on display.',
  88. 'page callback' => 'image_style_list',
  89. 'access arguments' => array('administer image styles'),
  90. 'file' => 'image.admin.inc',
  91. );
  92. $items['admin/config/media/image-styles/list'] = array(
  93. 'title' => 'List',
  94. 'description' => 'List the current image styles on the site.',
  95. 'page callback' => 'image_style_list',
  96. 'access arguments' => array('administer image styles'),
  97. 'type' => MENU_DEFAULT_LOCAL_TASK,
  98. 'weight' => 1,
  99. 'file' => 'image.admin.inc',
  100. );
  101. $items['admin/config/media/image-styles/add'] = array(
  102. 'title' => 'Add style',
  103. 'description' => 'Add a new image style.',
  104. 'page callback' => 'drupal_get_form',
  105. 'page arguments' => array('image_style_add_form'),
  106. 'access arguments' => array('administer image styles'),
  107. 'type' => MENU_LOCAL_ACTION,
  108. 'weight' => 2,
  109. 'file' => 'image.admin.inc',
  110. );
  111. $items['admin/config/media/image-styles/edit/%image_style'] = array(
  112. 'title' => 'Edit style',
  113. 'description' => 'Configure an image style.',
  114. 'page callback' => 'drupal_get_form',
  115. 'page arguments' => array('image_style_form', 5),
  116. 'access arguments' => array('administer image styles'),
  117. 'file' => 'image.admin.inc',
  118. );
  119. $items['admin/config/media/image-styles/delete/%image_style'] = array(
  120. 'title' => 'Delete style',
  121. 'description' => 'Delete an image style.',
  122. 'load arguments' => array(NULL, (string) IMAGE_STORAGE_NORMAL),
  123. 'page callback' => 'drupal_get_form',
  124. 'page arguments' => array('image_style_delete_form', 5),
  125. 'access arguments' => array('administer image styles'),
  126. 'file' => 'image.admin.inc',
  127. );
  128. $items['admin/config/media/image-styles/revert/%image_style'] = array(
  129. 'title' => 'Revert style',
  130. 'description' => 'Revert an image style.',
  131. 'load arguments' => array(NULL, (string) IMAGE_STORAGE_OVERRIDE),
  132. 'page callback' => 'drupal_get_form',
  133. 'page arguments' => array('image_style_revert_form', 5),
  134. 'access arguments' => array('administer image styles'),
  135. 'file' => 'image.admin.inc',
  136. );
  137. $items['admin/config/media/image-styles/edit/%image_style/effects/%image_effect'] = array(
  138. 'title' => 'Edit image effect',
  139. 'description' => 'Edit an existing effect within a style.',
  140. 'load arguments' => array(5, (string) IMAGE_STORAGE_EDITABLE),
  141. 'page callback' => 'drupal_get_form',
  142. 'page arguments' => array('image_effect_form', 5, 7),
  143. 'access arguments' => array('administer image styles'),
  144. 'file' => 'image.admin.inc',
  145. );
  146. $items['admin/config/media/image-styles/edit/%image_style/effects/%image_effect/delete'] = array(
  147. 'title' => 'Delete image effect',
  148. 'description' => 'Delete an existing effect from a style.',
  149. 'load arguments' => array(5, (string) IMAGE_STORAGE_EDITABLE),
  150. 'page callback' => 'drupal_get_form',
  151. 'page arguments' => array('image_effect_delete_form', 5, 7),
  152. 'access arguments' => array('administer image styles'),
  153. 'file' => 'image.admin.inc',
  154. );
  155. $items['admin/config/media/image-styles/edit/%image_style/add/%image_effect_definition'] = array(
  156. 'title' => 'Add image effect',
  157. 'description' => 'Add a new effect to a style.',
  158. 'load arguments' => array(5),
  159. 'page callback' => 'drupal_get_form',
  160. 'page arguments' => array('image_effect_form', 5, 7),
  161. 'access arguments' => array('administer image styles'),
  162. 'file' => 'image.admin.inc',
  163. );
  164. return $items;
  165. }
  166. /**
  167. * Implements hook_theme().
  168. */
  169. function image_theme() {
  170. return array(
  171. // Theme functions in image.module.
  172. 'image_style' => array(
  173. 'variables' => array(
  174. 'style_name' => NULL,
  175. 'path' => NULL,
  176. 'width' => NULL,
  177. 'height' => NULL,
  178. 'alt' => '',
  179. 'title' => NULL,
  180. 'attributes' => array(),
  181. ),
  182. ),
  183. // Theme functions in image.admin.inc.
  184. 'image_style_list' => array(
  185. 'variables' => array('styles' => NULL),
  186. ),
  187. 'image_style_effects' => array(
  188. 'render element' => 'form',
  189. ),
  190. 'image_style_preview' => array(
  191. 'variables' => array('style' => NULL),
  192. ),
  193. 'image_anchor' => array(
  194. 'render element' => 'element',
  195. ),
  196. 'image_resize_summary' => array(
  197. 'variables' => array('data' => NULL),
  198. ),
  199. 'image_scale_summary' => array(
  200. 'variables' => array('data' => NULL),
  201. ),
  202. 'image_crop_summary' => array(
  203. 'variables' => array('data' => NULL),
  204. ),
  205. 'image_rotate_summary' => array(
  206. 'variables' => array('data' => NULL),
  207. ),
  208. // Theme functions in image.field.inc.
  209. 'image_widget' => array(
  210. 'render element' => 'element',
  211. ),
  212. 'image_formatter' => array(
  213. 'variables' => array('item' => NULL, 'path' => NULL, 'image_style' => NULL),
  214. ),
  215. );
  216. }
  217. /**
  218. * Implements hook_permission().
  219. */
  220. function image_permission() {
  221. return array(
  222. 'administer image styles' => array(
  223. 'title' => t('Administer image styles'),
  224. 'description' => t('Create and modify styles for generating image modifications such as thumbnails.'),
  225. ),
  226. );
  227. }
  228. /**
  229. * Implements hook_form_FORM_ID_alter().
  230. */
  231. function image_form_system_file_system_settings_alter(&$form, &$form_state) {
  232. $form['#submit'][] = 'image_system_file_system_settings_submit';
  233. }
  234. /**
  235. * Submit handler for the file system settings form.
  236. *
  237. * Adds a menu rebuild after the public file path has been changed, so that the
  238. * menu router item depending on that file path will be regenerated.
  239. */
  240. function image_system_file_system_settings_submit($form, &$form_state) {
  241. if ($form['file_public_path']['#default_value'] !== $form_state['values']['file_public_path']) {
  242. variable_set('menu_rebuild_needed', TRUE);
  243. }
  244. }
  245. /**
  246. * Implements hook_flush_caches().
  247. */
  248. function image_flush_caches() {
  249. return array('cache_image');
  250. }
  251. /**
  252. * Implements hook_file_download().
  253. *
  254. * Control the access to files underneath the styles directory.
  255. */
  256. function image_file_download($uri) {
  257. $path = file_uri_target($uri);
  258. // Private file access for image style derivatives.
  259. if (strpos($path, 'styles/') === 0) {
  260. $args = explode('/', $path);
  261. // Discard the first part of the path (styles).
  262. array_shift($args);
  263. // Get the style name from the second part.
  264. $style_name = array_shift($args);
  265. // Remove the scheme from the path.
  266. array_shift($args);
  267. // Then the remaining parts are the path to the image.
  268. $original_uri = file_uri_scheme($uri) . '://' . implode('/', $args);
  269. // Check that the file exists and is an image.
  270. if ($info = image_get_info($uri)) {
  271. // Check the permissions of the original to grant access to this image.
  272. $headers = module_invoke_all('file_download', $original_uri);
  273. if (!in_array(-1, $headers)) {
  274. return array(
  275. // Send headers describing the image's size, and MIME-type...
  276. 'Content-Type' => $info['mime_type'],
  277. 'Content-Length' => $info['file_size'],
  278. // By not explicitly setting them here, this uses normal Drupal
  279. // Expires, Cache-Control and ETag headers to prevent proxy or
  280. // browser caching of private images.
  281. );
  282. }
  283. }
  284. return -1;
  285. }
  286. // Private file access for the original files. Note that we only
  287. // check access for non-temporary images, since file.module will
  288. // grant access for all temporary files.
  289. $files = file_load_multiple(array(), array('uri' => $uri));
  290. if (count($files)) {
  291. $file = reset($files);
  292. if ($file->status) {
  293. return file_file_download($uri, 'image');
  294. }
  295. }
  296. }
  297. /**
  298. * Implements hook_file_move().
  299. */
  300. function image_file_move($file, $source) {
  301. // Delete any image derivatives at the original image path.
  302. image_path_flush($source->uri);
  303. }
  304. /**
  305. * Implements hook_file_delete().
  306. */
  307. function image_file_delete($file) {
  308. // Delete any image derivatives of this image.
  309. image_path_flush($file->uri);
  310. }
  311. /**
  312. * Implements hook_image_default_styles().
  313. */
  314. function image_image_default_styles() {
  315. $styles = array();
  316. $styles['thumbnail'] = array(
  317. 'effects' => array(
  318. array(
  319. 'name' => 'image_scale',
  320. 'data' => array('width' => 100, 'height' => 100, 'upscale' => 1),
  321. 'weight' => 0,
  322. ),
  323. )
  324. );
  325. $styles['medium'] = array(
  326. 'effects' => array(
  327. array(
  328. 'name' => 'image_scale',
  329. 'data' => array('width' => 220, 'height' => 220, 'upscale' => 1),
  330. 'weight' => 0,
  331. ),
  332. )
  333. );
  334. $styles['large'] = array(
  335. 'effects' => array(
  336. array(
  337. 'name' => 'image_scale',
  338. 'data' => array('width' => 480, 'height' => 480, 'upscale' => 0),
  339. 'weight' => 0,
  340. ),
  341. )
  342. );
  343. return $styles;
  344. }
  345. /**
  346. * Implements hook_image_style_save().
  347. */
  348. function image_image_style_save($style) {
  349. if (isset($style['old_name']) && $style['old_name'] != $style['name']) {
  350. $instances = field_read_instances();
  351. // Loop through all fields searching for image fields.
  352. foreach ($instances as $instance) {
  353. if ($instance['widget']['module'] == 'image') {
  354. $instance_changed = FALSE;
  355. foreach ($instance['display'] as $view_mode => $display) {
  356. // Check if the formatter involves an image style.
  357. if ($display['type'] == 'image' && $display['settings']['image_style'] == $style['old_name']) {
  358. // Update display information for any instance using the image
  359. // style that was just deleted.
  360. $instance['display'][$view_mode]['settings']['image_style'] = $style['name'];
  361. $instance_changed = TRUE;
  362. }
  363. }
  364. if ($instance['widget']['settings']['preview_image_style'] == $style['old_name']) {
  365. $instance['widget']['settings']['preview_image_style'] = $style['name'];
  366. $instance_changed = TRUE;
  367. }
  368. if ($instance_changed) {
  369. field_update_instance($instance);
  370. }
  371. }
  372. }
  373. }
  374. }
  375. /**
  376. * Implements hook_image_style_delete().
  377. */
  378. function image_image_style_delete($style) {
  379. image_image_style_save($style);
  380. }
  381. /**
  382. * Implements hook_field_delete_field().
  383. */
  384. function image_field_delete_field($field) {
  385. if ($field['type'] != 'image') {
  386. return;
  387. }
  388. // The value of a managed_file element can be an array if #extended == TRUE.
  389. $fid = (is_array($field['settings']['default_image']) ? $field['settings']['default_image']['fid'] : $field['settings']['default_image']);
  390. if ($fid && ($file = file_load($fid))) {
  391. file_usage_delete($file, 'image', 'default_image', $field['id']);
  392. }
  393. }
  394. /**
  395. * Implements hook_field_update_field().
  396. */
  397. function image_field_update_field($field, $prior_field, $has_data) {
  398. if ($field['type'] != 'image') {
  399. return;
  400. }
  401. // The value of a managed_file element can be an array if #extended == TRUE.
  402. $fid_new = (is_array($field['settings']['default_image']) ? $field['settings']['default_image']['fid'] : $field['settings']['default_image']);
  403. $fid_old = (is_array($prior_field['settings']['default_image']) ? $prior_field['settings']['default_image']['fid'] : $prior_field['settings']['default_image']);
  404. $file_new = $fid_new ? file_load($fid_new) : FALSE;
  405. if ($fid_new != $fid_old) {
  406. // Is there a new file?
  407. if ($file_new) {
  408. $file_new->status = FILE_STATUS_PERMANENT;
  409. file_save($file_new);
  410. file_usage_add($file_new, 'image', 'default_image', $field['id']);
  411. }
  412. // Is there an old file?
  413. if ($fid_old && ($file_old = file_load($fid_old))) {
  414. file_usage_delete($file_old, 'image', 'default_image', $field['id']);
  415. }
  416. }
  417. // If the upload destination changed, then move the file.
  418. if ($file_new && (file_uri_scheme($file_new->uri) != $field['settings']['uri_scheme'])) {
  419. $directory = $field['settings']['uri_scheme'] . '://default_images/';
  420. file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
  421. file_move($file_new, $directory . $file_new->filename);
  422. }
  423. }
  424. /**
  425. * Implements hook_field_delete_instance().
  426. */
  427. function image_field_delete_instance($instance) {
  428. // Only act on image fields.
  429. $field = field_read_field($instance['field_name']);
  430. if ($field['type'] != 'image') {
  431. return;
  432. }
  433. // The value of a managed_file element can be an array if the #extended
  434. // property is set to TRUE.
  435. $fid = $instance['settings']['default_image'];
  436. if (is_array($fid)) {
  437. $fid = $fid['fid'];
  438. }
  439. // Remove the default image when the instance is deleted.
  440. if ($fid && ($file = file_load($fid))) {
  441. file_usage_delete($file, 'image', 'default_image', $instance['id']);
  442. }
  443. }
  444. /**
  445. * Implements hook_field_update_instance().
  446. */
  447. function image_field_update_instance($instance, $prior_instance) {
  448. // Only act on image fields.
  449. $field = field_read_field($instance['field_name']);
  450. if ($field['type'] != 'image') {
  451. return;
  452. }
  453. // The value of a managed_file element can be an array if the #extended
  454. // property is set to TRUE.
  455. $fid_new = $instance['settings']['default_image'];
  456. if (is_array($fid_new)) {
  457. $fid_new = $fid_new['fid'];
  458. }
  459. $fid_old = $prior_instance['settings']['default_image'];
  460. if (is_array($fid_old)) {
  461. $fid_old = $fid_old['fid'];
  462. }
  463. // If the old and new files do not match, update the default accordingly.
  464. $file_new = $fid_new ? file_load($fid_new) : FALSE;
  465. if ($fid_new != $fid_old) {
  466. // Save the new file, if present.
  467. if ($file_new) {
  468. $file_new->status = FILE_STATUS_PERMANENT;
  469. file_save($file_new);
  470. file_usage_add($file_new, 'image', 'default_image', $instance['id']);
  471. }
  472. // Delete the old file, if present.
  473. if ($fid_old && ($file_old = file_load($fid_old))) {
  474. file_usage_delete($file_old, 'image', 'default_image', $instance['id']);
  475. }
  476. }
  477. // If the upload destination changed, then move the file.
  478. if ($file_new && (file_uri_scheme($file_new->uri) != $field['settings']['uri_scheme'])) {
  479. $directory = $field['settings']['uri_scheme'] . '://default_images/';
  480. file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
  481. file_move($file_new, $directory . $file_new->filename);
  482. }
  483. }
  484. /**
  485. * Clear cached versions of a specific file in all styles.
  486. *
  487. * @param $path
  488. * The Drupal file path to the original image.
  489. */
  490. function image_path_flush($path) {
  491. $styles = image_styles();
  492. foreach ($styles as $style) {
  493. $image_path = image_style_path($style['name'], $path);
  494. if (file_exists($image_path)) {
  495. file_unmanaged_delete($image_path);
  496. }
  497. }
  498. }
  499. /**
  500. * Get an array of all styles and their settings.
  501. *
  502. * @return
  503. * An array of styles keyed by the image style ID (isid).
  504. * @see image_style_load()
  505. */
  506. function image_styles() {
  507. $styles = &drupal_static(__FUNCTION__);
  508. // Grab from cache or build the array.
  509. if (!isset($styles)) {
  510. if ($cache = cache_get('image_styles', 'cache')) {
  511. $styles = $cache->data;
  512. }
  513. else {
  514. $styles = array();
  515. // Select the module-defined styles.
  516. foreach (module_implements('image_default_styles') as $module) {
  517. $module_styles = module_invoke($module, 'image_default_styles');
  518. foreach ($module_styles as $style_name => $style) {
  519. $style['name'] = $style_name;
  520. $style['module'] = $module;
  521. $style['storage'] = IMAGE_STORAGE_DEFAULT;
  522. foreach ($style['effects'] as $key => $effect) {
  523. $definition = image_effect_definition_load($effect['name']);
  524. $effect = array_merge($definition, $effect);
  525. $style['effects'][$key] = $effect;
  526. }
  527. $styles[$style_name] = $style;
  528. }
  529. }
  530. // Select all the user-defined styles.
  531. $user_styles = db_select('image_styles', NULL, array('fetch' => PDO::FETCH_ASSOC))
  532. ->fields('image_styles')
  533. ->orderBy('name')
  534. ->execute()
  535. ->fetchAllAssoc('name', PDO::FETCH_ASSOC);
  536. // Allow the user styles to override the module styles.
  537. foreach ($user_styles as $style_name => $style) {
  538. $style['module'] = NULL;
  539. $style['storage'] = IMAGE_STORAGE_NORMAL;
  540. $style['effects'] = image_style_effects($style);
  541. if (isset($styles[$style_name]['module'])) {
  542. $style['module'] = $styles[$style_name]['module'];
  543. $style['storage'] = IMAGE_STORAGE_OVERRIDE;
  544. }
  545. $styles[$style_name] = $style;
  546. }
  547. drupal_alter('image_styles', $styles);
  548. cache_set('image_styles', $styles);
  549. }
  550. }
  551. return $styles;
  552. }
  553. /**
  554. * Load a style by style name or ID. May be used as a loader for menu items.
  555. *
  556. * @param $name
  557. * The name of the style.
  558. * @param $isid
  559. * Optional. The numeric id of a style if the name is not known.
  560. * @param $include
  561. * If set, this loader will restrict to a specific type of image style, may be
  562. * one of the defined Image style storage constants.
  563. * @return
  564. * An image style array containing the following keys:
  565. * - "isid": The unique image style ID.
  566. * - "name": The unique image style name.
  567. * - "effects": An array of image effects within this image style.
  568. * If the image style name or ID is not valid, an empty array is returned.
  569. * @see image_effect_load()
  570. */
  571. function image_style_load($name = NULL, $isid = NULL, $include = NULL) {
  572. $styles = image_styles();
  573. // If retrieving by name.
  574. if (isset($name) && isset($styles[$name])) {
  575. $style = $styles[$name];
  576. }
  577. // If retrieving by image style id.
  578. if (!isset($name) && isset($isid)) {
  579. foreach ($styles as $name => $database_style) {
  580. if (isset($database_style['isid']) && $database_style['isid'] == $isid) {
  581. $style = $database_style;
  582. break;
  583. }
  584. }
  585. }
  586. // Restrict to the specific type of flag. This bitwise operation basically
  587. // states "if the storage is X, then allow".
  588. if (isset($style) && (!isset($include) || ($style['storage'] & (int) $include))) {
  589. return $style;
  590. }
  591. // Otherwise the style was not found.
  592. return FALSE;
  593. }
  594. /**
  595. * Save an image style.
  596. *
  597. * @param style
  598. * An image style array.
  599. * @return
  600. * An image style array. In the case of a new style, 'isid' will be populated.
  601. */
  602. function image_style_save($style) {
  603. if (isset($style['isid']) && is_numeric($style['isid'])) {
  604. // Load the existing style to make sure we account for renamed styles.
  605. $old_style = image_style_load(NULL, $style['isid']);
  606. image_style_flush($old_style);
  607. drupal_write_record('image_styles', $style, 'isid');
  608. if ($old_style['name'] != $style['name']) {
  609. $style['old_name'] = $old_style['name'];
  610. }
  611. }
  612. else {
  613. drupal_write_record('image_styles', $style);
  614. $style['is_new'] = TRUE;
  615. }
  616. // Let other modules update as necessary on save.
  617. module_invoke_all('image_style_save', $style);
  618. // Clear all caches and flush.
  619. image_style_flush($style);
  620. return $style;
  621. }
  622. /**
  623. * Delete an image style.
  624. *
  625. * @param $style
  626. * An image style array.
  627. * @param $replacement_style_name
  628. * (optional) When deleting a style, specify a replacement style name so
  629. * that existing settings (if any) may be converted to a new style.
  630. * @return
  631. * TRUE on success.
  632. */
  633. function image_style_delete($style, $replacement_style_name = '') {
  634. image_style_flush($style);
  635. db_delete('image_effects')->condition('isid', $style['isid'])->execute();
  636. db_delete('image_styles')->condition('isid', $style['isid'])->execute();
  637. // Let other modules update as necessary on save.
  638. $style['old_name'] = $style['name'];
  639. $style['name'] = $replacement_style_name;
  640. module_invoke_all('image_style_delete', $style);
  641. return TRUE;
  642. }
  643. /**
  644. * Load all the effects for an image style.
  645. *
  646. * @param $style
  647. * An image style array.
  648. * @return
  649. * An array of image effects associated with specified image style in the
  650. * format array('isid' => array()), or an empty array if the specified style
  651. * has no effects.
  652. */
  653. function image_style_effects($style) {
  654. $effects = image_effects();
  655. $style_effects = array();
  656. foreach ($effects as $effect) {
  657. if ($style['isid'] == $effect['isid']) {
  658. $style_effects[$effect['ieid']] = $effect;
  659. }
  660. }
  661. return $style_effects;
  662. }
  663. /**
  664. * Get an array of image styles suitable for using as select list options.
  665. *
  666. * @param $include_empty
  667. * If TRUE a <none> option will be inserted in the options array.
  668. * @return
  669. * Array of image styles both key and value are set to style name.
  670. */
  671. function image_style_options($include_empty = TRUE) {
  672. $styles = image_styles();
  673. $options = array();
  674. if ($include_empty && !empty($styles)) {
  675. $options[''] = t('<none>');
  676. }
  677. // Use the array concatenation operator '+' here instead of array_merge(),
  678. // because the latter loses the datatype of the array keys, turning
  679. // associative string keys into numeric ones without warning.
  680. $options = $options + drupal_map_assoc(array_keys($styles));
  681. if (empty($options)) {
  682. $options[''] = t('No defined styles');
  683. }
  684. return $options;
  685. }
  686. /**
  687. * Menu callback; Given a style and image path, generate a derivative.
  688. *
  689. * After generating an image, transfer it to the requesting agent.
  690. *
  691. * @param $style
  692. * The image style
  693. */
  694. function image_style_deliver($style, $scheme) {
  695. // Check that the style is defined and the scheme is valid.
  696. if (!$style || !file_stream_wrapper_valid_scheme($scheme)) {
  697. drupal_exit();
  698. }
  699. $args = func_get_args();
  700. array_shift($args);
  701. array_shift($args);
  702. $target = implode('/', $args);
  703. $image_uri = $scheme . '://' . $target;
  704. $derivative_uri = image_style_path($style['name'], $image_uri);
  705. // If using the private scheme, let other modules provide headers and
  706. // control access to the file.
  707. if ($scheme == 'private') {
  708. if (file_exists($derivative_uri)) {
  709. file_download($scheme, file_uri_target($derivative_uri));
  710. }
  711. else {
  712. $headers = module_invoke_all('file_download', $image_uri);
  713. if (in_array(-1, $headers) || empty($headers)) {
  714. return drupal_access_denied();
  715. }
  716. if (count($headers)) {
  717. foreach ($headers as $name => $value) {
  718. drupal_add_http_header($name, $value);
  719. }
  720. }
  721. }
  722. }
  723. // Don't start generating the image if the derivative already exists or if
  724. // generation is in progress in another thread.
  725. $lock_name = 'image_style_deliver:' . $style['name'] . ':' . drupal_hash_base64($image_uri);
  726. if (!file_exists($derivative_uri)) {
  727. $lock_acquired = lock_acquire($lock_name);
  728. if (!$lock_acquired) {
  729. // Tell client to retry again in 3 seconds. Currently no browsers are known
  730. // to support Retry-After.
  731. drupal_add_http_header('Status', '503 Service Unavailable');
  732. drupal_add_http_header('Retry-After', 3);
  733. print t('Image generation in progress. Try again shortly.');
  734. drupal_exit();
  735. }
  736. }
  737. // Try to generate the image, unless another thread just did it while we were
  738. // acquiring the lock.
  739. $success = file_exists($derivative_uri) || image_style_create_derivative($style, $image_uri, $derivative_uri);
  740. if (!empty($lock_acquired)) {
  741. lock_release($lock_name);
  742. }
  743. if ($success) {
  744. $image = image_load($derivative_uri);
  745. file_transfer($image->source, array('Content-Type' => $image->info['mime_type'], 'Content-Length' => $image->info['file_size']));
  746. }
  747. else {
  748. watchdog('image', 'Unable to generate the derived image located at %path.', array('%path' => $derivative_uri));
  749. drupal_add_http_header('Status', '500 Internal Server Error');
  750. print t('Error generating image.');
  751. drupal_exit();
  752. }
  753. }
  754. /**
  755. * Creates a new image derivative based on an image style.
  756. *
  757. * Generates an image derivative by creating the destination folder (if it does
  758. * not already exist), applying all image effects defined in $style['effects'],
  759. * and saving a cached version of the resulting image.
  760. *
  761. * @param $style
  762. * An image style array.
  763. * @param $source
  764. * Path of the source file.
  765. * @param $destination
  766. * Path or URI of the destination file.
  767. *
  768. * @return
  769. * TRUE if an image derivative was generated, or FALSE if the image derivative
  770. * could not be generated.
  771. *
  772. * @see image_style_load()
  773. */
  774. function image_style_create_derivative($style, $source, $destination) {
  775. // Get the folder for the final location of this style.
  776. $directory = drupal_dirname($destination);
  777. // Build the destination folder tree if it doesn't already exist.
  778. if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  779. watchdog('image', 'Failed to create style directory: %directory', array('%directory' => $directory), WATCHDOG_ERROR);
  780. return FALSE;
  781. }
  782. if (!$image = image_load($source)) {
  783. return FALSE;
  784. }
  785. foreach ($style['effects'] as $effect) {
  786. image_effect_apply($image, $effect);
  787. }
  788. if (!image_save($image, $destination)) {
  789. if (file_exists($destination)) {
  790. watchdog('image', 'Cached image file %destination already exists. There may be an issue with your rewrite configuration.', array('%destination' => $destination), WATCHDOG_ERROR);
  791. }
  792. return FALSE;
  793. }
  794. return TRUE;
  795. }
  796. /**
  797. * Determines the dimensions of the styled image.
  798. *
  799. * Applies all of an image style's effects to $dimensions.
  800. *
  801. * @param $style_name
  802. * The name of the style to be applied.
  803. * @param $dimensions
  804. * Dimensions to be modified - an array with components width and height, in
  805. * pixels.
  806. */
  807. function image_style_transform_dimensions($style_name, array &$dimensions) {
  808. module_load_include('inc', 'image', 'image.effects');
  809. $style = image_style_load($style_name);
  810. if (!is_array($style)) {
  811. return;
  812. }
  813. foreach ($style['effects'] as $effect) {
  814. if (isset($effect['dimensions passthrough'])) {
  815. continue;
  816. }
  817. if (isset($effect['dimensions callback'])) {
  818. $effect['dimensions callback']($dimensions, $effect['data']);
  819. }
  820. else {
  821. $dimensions['width'] = $dimensions['height'] = NULL;
  822. }
  823. }
  824. }
  825. /**
  826. * Flush cached media for a style.
  827. *
  828. * @param $style
  829. * An image style array.
  830. */
  831. function image_style_flush($style) {
  832. $style_directory = drupal_realpath(file_default_scheme() . '://styles/' . $style['name']);
  833. if (is_dir($style_directory)) {
  834. file_unmanaged_delete_recursive($style_directory);
  835. }
  836. // Let other modules update as necessary on flush.
  837. module_invoke_all('image_style_flush', $style);
  838. // Clear image style and effect caches.
  839. cache_clear_all('image_styles', 'cache');
  840. cache_clear_all('image_effects:', 'cache', TRUE);
  841. drupal_static_reset('image_styles');
  842. drupal_static_reset('image_effects');
  843. // Clear field caches so that formatters may be added for this style.
  844. field_info_cache_clear();
  845. drupal_theme_rebuild();
  846. // Clear page caches when flushing.
  847. if (module_exists('block')) {
  848. cache_clear_all('*', 'cache_block', TRUE);
  849. }
  850. cache_clear_all('*', 'cache_page', TRUE);
  851. }
  852. /**
  853. * Return the URL for an image derivative given a style and image path.
  854. *
  855. * @param $style_name
  856. * The name of the style to be used with this image.
  857. * @param $path
  858. * The path to the image.
  859. * @return
  860. * The absolute URL where a style image can be downloaded, suitable for use
  861. * in an <img> tag. Requesting the URL will cause the image to be created.
  862. * @see image_style_deliver()
  863. */
  864. function image_style_url($style_name, $path) {
  865. $uri = image_style_path($style_name, $path);
  866. // If not using clean URLs, the image derivative callback is only available
  867. // with the query string. If the file does not exist, use url() to ensure
  868. // that it is included. Once the file exists it's fine to fall back to the
  869. // actual file path, this avoids bootstrapping PHP once the files are built.
  870. if (!variable_get('clean_url') && file_uri_scheme($uri) == 'public' && !file_exists($uri)) {
  871. $directory_path = file_stream_wrapper_get_instance_by_uri($uri)->getDirectoryPath();
  872. return url($directory_path . '/' . file_uri_target($uri), array('absolute' => TRUE));
  873. }
  874. return file_create_url($uri);
  875. }
  876. /**
  877. * Return the URI of an image when using a style.
  878. *
  879. * The path returned by this function may not exist. The default generation
  880. * method only creates images when they are requested by a user's browser.
  881. *
  882. * @param $style_name
  883. * The name of the style to be used with this image.
  884. * @param $uri
  885. * The URI or path to the image.
  886. * @return
  887. * The URI to an image style image.
  888. * @see image_style_url()
  889. */
  890. function image_style_path($style_name, $uri) {
  891. $scheme = file_uri_scheme($uri);
  892. if ($scheme) {
  893. $path = file_uri_target($uri);
  894. }
  895. else {
  896. $path = $uri;
  897. $scheme = file_default_scheme();
  898. }
  899. return $scheme . '://styles/' . $style_name . '/' . $scheme . '/' . $path;
  900. }
  901. /**
  902. * Save a default image style to the database.
  903. *
  904. * @param style
  905. * An image style array provided by a module.
  906. * @return
  907. * An image style array. The returned style array will include the new 'isid'
  908. * assigned to the style.
  909. */
  910. function image_default_style_save($style) {
  911. $style = image_style_save($style);
  912. $effects = array();
  913. foreach ($style['effects'] as $effect) {
  914. $effect['isid'] = $style['isid'];
  915. $effect = image_effect_save($effect);
  916. $effects[$effect['ieid']] = $effect;
  917. }
  918. $style['effects'] = $effects;
  919. return $style;
  920. }
  921. /**
  922. * Revert the changes made by users to a default image style.
  923. *
  924. * @param style
  925. * An image style array.
  926. * @return
  927. * Boolean TRUE if the operation succeeded.
  928. */
  929. function image_default_style_revert($style) {
  930. image_style_flush($style);
  931. db_delete('image_effects')->condition('isid', $style['isid'])->execute();
  932. db_delete('image_styles')->condition('isid', $style['isid'])->execute();
  933. return TRUE;
  934. }
  935. /**
  936. * Pull in image effects exposed by modules implementing hook_image_effect_info().
  937. *
  938. * @return
  939. * An array of image effects to be used when transforming images.
  940. * @see hook_image_effect_info()
  941. * @see image_effect_definition_load()
  942. */
  943. function image_effect_definitions() {
  944. global $language;
  945. // hook_image_effect_info() includes translated strings, so each language is
  946. // cached separately.
  947. $langcode = $language->language;
  948. $effects = &drupal_static(__FUNCTION__);
  949. if (!isset($effects)) {
  950. if ($cache = cache_get("image_effects:$langcode")) {
  951. $effects = $cache->data;
  952. }
  953. else {
  954. $effects = array();
  955. include_once DRUPAL_ROOT . '/modules/image/image.effects.inc';
  956. foreach (module_implements('image_effect_info') as $module) {
  957. foreach (module_invoke($module, 'image_effect_info') as $name => $effect) {
  958. // Ensure the current toolkit supports the effect.
  959. $effect['module'] = $module;
  960. $effect['name'] = $name;
  961. $effect['data'] = isset($effect['data']) ? $effect['data'] : array();
  962. $effects[$name] = $effect;
  963. }
  964. }
  965. uasort($effects, '_image_effect_definitions_sort');
  966. drupal_alter('image_effect_info', $effects);
  967. cache_set("image_effects:$langcode", $effects);
  968. }
  969. }
  970. return $effects;
  971. }
  972. /**
  973. * Load the definition for an image effect.
  974. *
  975. * The effect definition is a set of core properties for an image effect, not
  976. * containing any user-settings. The definition defines various functions to
  977. * call when configuring or executing an image effect. This loader is mostly for
  978. * internal use within image.module. Use image_effect_load() or
  979. * image_style_load() to get image effects that contain configuration.
  980. *
  981. * @param $effect
  982. * The name of the effect definition to load.
  983. * @param $style
  984. * An image style array to which this effect will be added.
  985. * @return
  986. * An array containing the image effect definition with the following keys:
  987. * - "effect": The unique name for the effect being performed. Usually prefixed
  988. * with the name of the module providing the effect.
  989. * - "module": The module providing the effect.
  990. * - "help": A description of the effect.
  991. * - "function": The name of the function that will execute the effect.
  992. * - "form": (optional) The name of a function to configure the effect.
  993. * - "summary": (optional) The name of a theme function that will display a
  994. * one-line summary of the effect. Does not include the "theme_" prefix.
  995. */
  996. function image_effect_definition_load($effect, $style_name = NULL) {
  997. $definitions = image_effect_definitions();
  998. // If a style is specified, do not allow loading of default style
  999. // effects.
  1000. if (isset($style_name)) {
  1001. $style = image_style_load($style_name, NULL);
  1002. if ($style['storage'] == IMAGE_STORAGE_DEFAULT) {
  1003. return FALSE;
  1004. }
  1005. }
  1006. return isset($definitions[$effect]) ? $definitions[$effect] : FALSE;
  1007. }
  1008. /**
  1009. * Load all image effects from the database.
  1010. *
  1011. * @return
  1012. * An array of all image effects.
  1013. * @see image_effect_load()
  1014. */
  1015. function image_effects() {
  1016. $effects = &drupal_static(__FUNCTION__);
  1017. if (!isset($effects)) {
  1018. $effects = array();
  1019. // Add database image effects.
  1020. $result = db_select('image_effects', NULL, array('fetch' => PDO::FETCH_ASSOC))
  1021. ->fields('image_effects')
  1022. ->orderBy('image_effects.weight', 'ASC')
  1023. ->execute();
  1024. foreach ($result as $effect) {
  1025. $effect['data'] = unserialize($effect['data']);
  1026. $definition = image_effect_definition_load($effect['name']);
  1027. // Do not load image effects whose definition cannot be found.
  1028. if ($definition) {
  1029. $effect = array_merge($definition, $effect);
  1030. $effects[$effect['ieid']] = $effect;
  1031. }
  1032. }
  1033. }
  1034. return $effects;
  1035. }
  1036. /**
  1037. * Load a single image effect.
  1038. *
  1039. * @param $ieid
  1040. * The image effect ID.
  1041. * @param $style_name
  1042. * The image style name.
  1043. * @param $include
  1044. * If set, this loader will restrict to a specific type of image style, may be
  1045. * one of the defined Image style storage constants.
  1046. * @return
  1047. * An image effect array, consisting of the following keys:
  1048. * - "ieid": The unique image effect ID.
  1049. * - "isid": The unique image style ID that contains this image effect.
  1050. * - "weight": The weight of this image effect within the image style.
  1051. * - "name": The name of the effect definition that powers this image effect.
  1052. * - "data": An array of configuration options for this image effect.
  1053. * Besides these keys, the entirety of the image definition is merged into
  1054. * the image effect array. Returns FALSE if the specified effect cannot be
  1055. * found.
  1056. * @see image_style_load()
  1057. * @see image_effect_definition_load()
  1058. */
  1059. function image_effect_load($ieid, $style_name, $include = NULL) {
  1060. if (($style = image_style_load($style_name, NULL, $include)) && isset($style['effects'][$ieid])) {
  1061. return $style['effects'][$ieid];
  1062. }
  1063. return FALSE;
  1064. }
  1065. /**
  1066. * Save an image effect.
  1067. *
  1068. * @param $effect
  1069. * An image effect array.
  1070. * @return
  1071. * An image effect array. In the case of a new effect, 'ieid' will be set.
  1072. */
  1073. function image_effect_save($effect) {
  1074. if (!empty($effect['ieid'])) {
  1075. drupal_write_record('image_effects', $effect, 'ieid');
  1076. }
  1077. else {
  1078. drupal_write_record('image_effects', $effect);
  1079. }
  1080. $style = image_style_load(NULL, $effect['isid']);
  1081. image_style_flush($style);
  1082. return $effect;
  1083. }
  1084. /**
  1085. * Delete an image effect.
  1086. *
  1087. * @param $effect
  1088. * An image effect array.
  1089. */
  1090. function image_effect_delete($effect) {
  1091. db_delete('image_effects')->condition('ieid', $effect['ieid'])->execute();
  1092. $style = image_style_load(NULL, $effect['isid']);
  1093. image_style_flush($style);
  1094. }
  1095. /**
  1096. * Given an image object and effect, perform the effect on the file.
  1097. *
  1098. * @param $image
  1099. * An image object returned by image_load().
  1100. * @param $effect
  1101. * An image effect array.
  1102. * @return
  1103. * TRUE on success. FALSE if unable to perform the image effect on the image.
  1104. */
  1105. function image_effect_apply($image, $effect) {
  1106. module_load_include('inc', 'image', 'image.effects');
  1107. $function = $effect['effect callback'];
  1108. if (function_exists($function)) {
  1109. return $function($image, $effect['data']);
  1110. }
  1111. return FALSE;
  1112. }
  1113. /**
  1114. * Returns HTML for an image using a specific image style.
  1115. *
  1116. * @param $variables
  1117. * An associative array containing:
  1118. * - style_name: The name of the style to be used to alter the original image.
  1119. * - path: The path of the image file relative to the Drupal files directory.
  1120. * This function does not work with images outside the files directory nor
  1121. * with remotely hosted images. This should be in a format such as
  1122. * 'images/image.jpg', or using a stream wrapper such as
  1123. * 'public://images/image.jpg'.
  1124. * - width: The width of the source image (if known).
  1125. * - height: The height of the source image (if known).
  1126. * - alt: The alternative text for text-based browsers.
  1127. * - title: The title text is displayed when the image is hovered in some
  1128. * popular browsers.
  1129. * - attributes: Associative array of attributes to be placed in the img tag.
  1130. *
  1131. * @ingroup themeable
  1132. */
  1133. function theme_image_style($variables) {
  1134. // Determine the dimensions of the styled image.
  1135. $dimensions = array(
  1136. 'width' => $variables['width'],
  1137. 'height' => $variables['height'],
  1138. );
  1139. image_style_transform_dimensions($variables['style_name'], $dimensions);
  1140. $variables['width'] = $dimensions['width'];
  1141. $variables['height'] = $dimensions['height'];
  1142. // Determine the URL for the styled image.
  1143. $variables['path'] = image_style_url($variables['style_name'], $variables['path']);
  1144. return theme('image', $variables);
  1145. }
  1146. /**
  1147. * Accept a keyword (center, top, left, etc) and return it as a pixel offset.
  1148. *
  1149. * @param $value
  1150. * @param $current_pixels
  1151. * @param $new_pixels
  1152. */
  1153. function image_filter_keyword($value, $current_pixels, $new_pixels) {
  1154. switch ($value) {
  1155. case 'top':
  1156. case 'left':
  1157. return 0;
  1158. case 'bottom':
  1159. case 'right':
  1160. return $current_pixels - $new_pixels;
  1161. case 'center':
  1162. return $current_pixels / 2 - $new_pixels / 2;
  1163. }
  1164. return $value;
  1165. }
  1166. /**
  1167. * Internal function for sorting image effect definitions through uasort().
  1168. *
  1169. * @see image_effect_definitions()
  1170. */
  1171. function _image_effect_definitions_sort($a, $b) {
  1172. return strcasecmp($a['name'], $b['name']);
  1173. }