imagecache_testsuite.module 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. <?php
  2. /**
  3. * @file An admin-only utility to test image styles and effects.
  4. *
  5. * It provides a page Test Suite in Administration > Configuration > Media >
  6. * Image Styles (admin/config/media/image-styles/testsuite) that displays
  7. * results for all existing image styles for all toolkits as well as for a set
  8. * of test image styles defined in the various modules.
  9. */
  10. include_once('imagecache_testsuite.features.inc');
  11. /**
  12. * Implementation of hook_menu().
  13. */
  14. function imagecache_testsuite_menu() {
  15. $items = array();
  16. $items['admin/config/media/image-styles/testsuite'] = array(
  17. 'title' => 'Test Suite',
  18. 'page callback' => 'imagecache_testsuite_page',
  19. 'access arguments' => array('administer image styles'),
  20. 'type' => MENU_LOCAL_TASK,
  21. 'weight' => 10,
  22. );
  23. $items['admin/config/media/image-styles/testsuite/%/%'] = array(
  24. 'title' => 'Test Suite Image',
  25. 'page callback' => 'imagecache_testsuite_generate',
  26. 'page arguments' => array(5, 6),
  27. 'access arguments' => array('administer image styles'),
  28. 'type' => MENU_CALLBACK,
  29. );
  30. $items['admin/config/media/image-styles/testsuite/positioning_test'] = array(
  31. 'title' => 'Positioning Test',
  32. 'page callback' => 'imagecache_testsuite_positioning',
  33. 'access arguments' => array('administer image styles'),
  34. 'type' => MENU_LOCAL_TASK,
  35. );
  36. return $items;
  37. }
  38. /**
  39. * Implementation of hook_help()
  40. */
  41. function imagecache_testsuite_help($path /*, $arg*/) {
  42. switch ($path) {
  43. // @todo: this path does not exist anymore.
  44. case 'admin/build/imagecache/test' :
  45. $output = file_get_contents(drupal_get_path('module', 'imagecache_testsuite') . "/README.txt");
  46. return _filter_autop($output);
  47. break;
  48. case 'admin/config/media/image-styles/testsuite' :
  49. return t("<p>
  50. This page displays a number of examples of image effects.
  51. Illustrated are both the expected result and the actual result.
  52. </p><p>
  53. This page is just for debugging to confirm that this behavior doesn't
  54. change as the code gets updated.
  55. If the two illustrations do not match, there is probably something
  56. that needs fixing.
  57. </p><p>
  58. More actions are provided by each of the imagecache actions submodules
  59. and will be shown as you enable them.
  60. </p>");
  61. break;
  62. case 'admin/config/media/image-styles' :
  63. return t('
  64. A number of styles here are provided by the Imagecache
  65. Testsuite module as examples.
  66. Disable this module to make them go away.
  67. ');
  68. break;
  69. }
  70. return '';
  71. }
  72. /**
  73. * Returns the test suite page.
  74. *
  75. * The test suite page contians img links to all image derivatives to create as
  76. * part of the test suite.
  77. *
  78. * Samples to test are scanned from:
  79. * - The existing image styles.
  80. * - The file features.inc attached to this module. (@todo: no longer eisting?)
  81. * - Individual *.imagecache_preset.inc files found near any known modules.
  82. * Images illustrating the named preset are looked for also.
  83. *
  84. * Flushes the entire test cache every time anything is done.
  85. *
  86. * @return string
  87. * The html for the page.
  88. */
  89. function imagecache_testsuite_page() {
  90. module_load_include('inc', 'image', 'image.admin');
  91. module_load_include('inc', 'image', 'image.effects');
  92. $tests = array_merge(image_styles(), imagecache_testsuite_get_tests());
  93. $toolkits = image_get_available_toolkits();
  94. // Present the all-in-one overview page.
  95. $sample_folders = imagecache_testsuite_get_folders();
  96. // Draw the admin table.
  97. $test_table = array();
  98. foreach ($tests as $style_name => $style) {
  99. // Firstly, remove any previous images for the current style
  100. image_style_flush($style);
  101. $row = array();
  102. $row_class = 'test';
  103. $details_list = array();
  104. // Render the details.
  105. foreach ($style['effects'] as $effect) {
  106. if (!isset($effect['name'])) {
  107. // badness
  108. watchdog('imagecache_testsuite', 'invalid testcase within %style_name. No effect name', array('%style_name' => $style_name), WATCHDOG_ERROR);
  109. $details_list[] = '<div>Unidentified effect</div>';
  110. $row_class = 'error';
  111. continue;
  112. }
  113. $effect_definition = image_effect_definition_load($effect['name']);
  114. if (function_exists($effect_definition['effect callback'])) {
  115. $description = "<strong>{$effect_definition['label']}</strong> ";
  116. $description .= isset($effect_definition['summary theme']) ? theme($effect_definition['summary theme'], array('data' => $effect['data'])) : '';
  117. $details_list[] = "<div>$description</div>";
  118. }
  119. else {
  120. // Probably an action that requires a module that is not installed.
  121. $strings = array(
  122. '%action' => $effect['name'],
  123. '%module' => $effect['module'],
  124. );
  125. $details_list[$effect['name']] = t("<div><b>%action unavailable</b>. Please enable %module module.</div>", $strings);
  126. $row_class = 'error';
  127. }
  128. }
  129. $row['details'] = "<h3>{$style['name']}</h3><p>" . implode($details_list) . "</p>";
  130. // Look for a sample image. May also be defined by the definition itself,
  131. // but normally assume a file named after the image style, in (one of the)
  132. // directories with test styles.
  133. foreach ($sample_folders as $sample_folder) {
  134. if (file_exists("{$sample_folder}/{$style_name}.png")) {
  135. $style['sample'] = "{$sample_folder}/{$style_name}.png";
  136. }
  137. elseif (file_exists("{$sample_folder}/{$style_name}.jpg")) {
  138. $style['sample'] = "{$sample_folder}/{$style_name}.jpg";
  139. }
  140. }
  141. if (isset($style['sample']) && file_exists($style['sample'])) {
  142. $sample_img = theme('image', array('path' => $style['sample']));
  143. // I was having trouble with permissions on an OSX dev machine.
  144. if (!is_readable($style['sample'])) {
  145. $sample_img = "FILE UNREADABLE: {$style['sample']}";
  146. }
  147. }
  148. else {
  149. $sample_img = "[no sample]";
  150. }
  151. $row['sample'] = $sample_img;
  152. // Generate a result for each available toolkit.
  153. foreach ($toolkits as $toolkit => $toolkit_info) {
  154. $test_url = "admin/config/media/image-styles/testsuite/$style_name/$toolkit";
  155. $test_img = theme('image', array(
  156. 'path' => $test_url,
  157. 'alt' => "$style_name/$toolkit"
  158. ));
  159. $row[$toolkit] = l($test_img, $test_url, array('html' => TRUE));
  160. }
  161. $test_table[$style_name] = array(
  162. 'data' => $row,
  163. 'class' => array($row_class)
  164. );
  165. }
  166. $header = array_merge(array('test', 'sample'), array_keys($toolkits));
  167. $output = theme('table', array(
  168. 'header' => $header,
  169. 'rows' => $test_table,
  170. 'id' => 'imagecache-testsuite'
  171. ));
  172. // @todo: zebra striping can be disabled in D7.
  173. // Default system zebra-striping fails to show my transparency on white.
  174. drupal_add_html_head('<style type="text/css" >#imagecache-testsuite tr.even{background-color:#EEEEEE !important;} #imagecache-testsuite td{vertical-align:top;} #imagecache-testsuite tr.error{background-color:#FFCCCC !important;}</style>');
  175. return $output;
  176. }
  177. /**
  178. * Returns the requested image derivative.
  179. *
  180. * If the image derivative generation is successful, the function does not
  181. * return but exits processing using drupal_exit().
  182. *
  183. * Flushes the entire test cache every time anything is done.
  184. *
  185. * @param string $test_id
  186. * The id of the test to generate the derivative for.
  187. * @param string $toolkit
  188. * The toolkit to use, or empty for the default toolkit
  189. *
  190. * @return string|bool
  191. * - The html for the page ($test_id is empty)
  192. * - False when the image derivative could not be created.
  193. */
  194. function imagecache_testsuite_generate($test_id = '', $toolkit = '') {
  195. module_load_include('inc', 'image', 'image.admin');
  196. module_load_include('inc', 'image', 'image.effects');
  197. if (empty($toolkit)) {
  198. $toolkit = image_get_toolkit();
  199. }
  200. else {
  201. // Set the toolkit for this invocation only, so do not use variable_set.
  202. global $conf;
  203. $conf['image_toolkit'] = $toolkit;
  204. if ($toolkit === 'gd') {
  205. // This seems not to be done automatically elsewhere.
  206. include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'system') . '/' . 'image.gd.inc';
  207. }
  208. }
  209. $target = 'module://imagecache_testsuite/sample.jpg';
  210. $tests = array_merge(image_styles(), imagecache_testsuite_get_tests());
  211. // Run the process and return the image, @see image_style_create_derivative().
  212. $style = $tests[$test_id];
  213. if (!$style) {
  214. trigger_error("Unknown test style preset '$test_id' ", E_USER_ERROR);
  215. return FALSE;
  216. }
  217. // @todo: should we let the image style system do its work and just interfere on hook_init with setting the toolkit?
  218. // @todo: this would make the page generator easier as well and keep it working with secure image derivatives.
  219. // Start emulating image_style_create_derivative()
  220. // The main difference being I determine the toolkit I want to use.
  221. // SOME of this code is probably redundant, was a lot of copy&paste without true understanding of the new image.module
  222. if (!$image = image_load($target, $toolkit)) {
  223. trigger_error("Failed to open original image $target with toolkit $toolkit", E_USER_ERROR);
  224. return FALSE;
  225. }
  226. // Need to save the result before returning it - to stay compatible with imagemagick
  227. $filename = "$test_id-$toolkit.{$image->info['extension']}";
  228. $derivative_uri = image_style_path($style['name'], $filename);
  229. $directory = dirname($derivative_uri);
  230. file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
  231. watchdog('imagecache_testsuite', 'Checking a save dir %dir', array('%dir' => dirname($derivative_uri)), WATCHDOG_DEBUG);
  232. // Imagemagick is not quite right? place a file where the file is supposed to go
  233. // before I put the real path there? else drupal_realpath() says nuh.
  234. #file_save_data('touch this for imagemagick', $derivative_uri, FILE_EXISTS_REPLACE);
  235. foreach ($style['effects'] as $effect) {
  236. // Need to load the full effect definitions, our test ones don't know all the callback info
  237. $effect_definition = image_effect_definition_load($effect['name']);
  238. if (empty($effect_definition)) {
  239. watchdog('imagecache_testsuite', 'I have no idea what %name is', array('%name' => $effect['name']), WATCHDOG_ERROR);
  240. continue;
  241. }
  242. $full_effect = array_merge($effect_definition, array('data' => $effect['data']));
  243. // @todo: effects that involve other images (overlay, underlay) will load that image with the default toolkit which may differ from the toolkit tested here.
  244. if (!image_effect_apply($image, $full_effect)) {
  245. watchdog('imagecache_testsuite', 'action: %action (%callback) failed for %src', array(
  246. '%action' => $full_effect['label'],
  247. '%src' => $target,
  248. '%callback' => $full_effect['effect callback']
  249. ), WATCHDOG_ERROR);
  250. }
  251. }
  252. if (!image_save($image, $derivative_uri)) {
  253. watchdog('imagecache_testsuite', 'saving image %label failed for %derivative_uri', array(
  254. '%derivative_uri' => $derivative_uri,
  255. '%label' => isset($style['label']) ? $style['label'] : $style['name']
  256. ), WATCHDOG_ERROR);
  257. return FALSE;
  258. }
  259. if ($result_image = image_load($derivative_uri)) {
  260. file_transfer($result_image->source, array(
  261. 'Content-Type' => $result_image->info['mime_type'],
  262. 'Content-Length' => $result_image->info['file_size']
  263. ));
  264. drupal_exit();
  265. }
  266. return "Failed to load the expected result from $derivative_uri";
  267. }
  268. /**
  269. * Implements hook_image_default_styles().
  270. *
  271. * Lists all our individual test cases and makes them available
  272. * as enabled styles
  273. */
  274. function imagecache_testsuite_image_default_styles() {
  275. $styles = imagecache_testsuite_get_tests();
  276. // Need to filter out the invalid test cases
  277. // (ones that use unavailable actions)
  278. // or the core complains with notices.
  279. // foreach ($styles as $id => $style) {
  280. // foreach ($style['effects'] as $delta => $action) {
  281. // if (!empty($action['module']) && ($action['module'] != 'imagecache') && !module_exists($action['module'])) {
  282. // unset($styles[$id]);
  283. // break;
  284. // }
  285. // }
  286. // }
  287. return $styles;
  288. }
  289. /**
  290. * Retrieve the list of presets, each of which contain actions and action
  291. * definitions.
  292. *
  293. * Scans all the module folders for files named *.imagecache_preset.inc
  294. *
  295. * It seems that the required shape in D7 is
  296. * $style=>array(
  297. * 'effects' => array(
  298. * 0 => array('name' => 'something', 'data' => array())
  299. * )
  300. * )
  301. */
  302. function imagecache_testsuite_get_tests() {
  303. $presets = array();
  304. $folders = imagecache_testsuite_get_folders();
  305. foreach ($folders as $folder) {
  306. $preset_files = file_scan_directory($folder, "/.*.imagecache_preset.inc/");
  307. // Setting filepath in this scope allows the tests to know where they are.
  308. // The inc files may use it to create their rules.
  309. $filepath = $folder;
  310. foreach ($preset_files as $preset_file) {
  311. include_once($preset_file->uri);
  312. }
  313. }
  314. uasort($presets, 'element_sort');
  315. return $presets;
  316. }
  317. /**
  318. * Places to scan for test presets and sample images.
  319. *
  320. * @return array
  321. * an array of folder names of everything that implements imagecache_actions.
  322. */
  323. function imagecache_testsuite_get_folders() {
  324. $folders = array(drupal_get_path('module', 'imagecache_testsuite'));
  325. foreach (module_implements('image_effect_info') as $module_name) {
  326. $folders[] = drupal_get_path('module', $module_name) . '/tests';
  327. }
  328. return $folders;
  329. }
  330. /**
  331. * Display a page demonstrating a number of positioning tests
  332. *
  333. * Tests both styles of positioning - the x=, y= original, used in most places,
  334. * pls the css-like left=, top= version also.
  335. */
  336. function imagecache_testsuite_positioning() {
  337. module_load_include('inc', 'imagecache_actions', 'utility');
  338. drupal_set_title("Testing the positioning algorithm");
  339. $tests = imagecache_testsuite_positioning_get_tests();
  340. $table = array();
  341. // $dst_image represents tha field or canvas.
  342. // $src_image is the item being placed on it.
  343. // Both these represent an imageapi-type image resource handle, but contain just dimensions
  344. $src_image = new stdClass();
  345. $src_image->info = array('width' => 75, 'height' => 100);
  346. $dst_image = new stdClass();
  347. $dst_image->info = array('width' => 200, 'height' => 150);
  348. foreach ($tests as $testname => $test) {
  349. // calc it, using either old or new method
  350. if (isset($test['parameters']['x']) || isset($test['parameters']['y'])) {
  351. $result['x'] = imagecache_actions_keyword_filter($test['parameters']['x'], $dst_image->info['width'], $src_image->info['width']);
  352. $result['y'] = imagecache_actions_keyword_filter($test['parameters']['y'], $dst_image->info['height'], $src_image->info['height']);
  353. }
  354. else {
  355. // use css style
  356. $result = imagecache_actions_calculate_relative_position($dst_image, $src_image, $test['parameters']);
  357. }
  358. $expected_illustration = theme_positioning_test($test['expected']['x'], $test['expected']['y']);
  359. $result_illustration = theme_positioning_test($result['x'], $result['y']);
  360. $row = array();
  361. $row['name'] = array('data' => '<h3>' . $testname . '</h3>' . $test['description']);
  362. $row['parameters'] = theme_positioning_parameters($test['parameters']);
  363. $row['expected'] = theme_positioning_parameters($test['expected']);
  364. $row['expected_image'] = $expected_illustration;
  365. $row['result'] = theme_positioning_parameters($result);
  366. $row['result_image'] = $result_illustration;
  367. $table[] = $row;
  368. }
  369. return 'Result of test:' . theme('table', array(
  370. 'test',
  371. 'parameters',
  372. 'expected',
  373. 'image',
  374. 'result',
  375. 'actual image',
  376. 'status'
  377. ), $table);
  378. }
  379. function theme_positioning_test($x, $y) {
  380. $inner = "<div style='background-color:red; width:75px; height:100px; position:absolute; left:{$x}px; top:{$y}px'>";
  381. $outer = "<div style='background-color:blue; width:200px; height:150px; position:absolute; left:25px; top:25px'><div style='position:relative'>$inner</div></div>";
  382. $wrapper = "<div style='background-color:#CCCCCC; width:250px; height:200px; position:relative'>$outer</div>";
  383. return $wrapper;
  384. }
  385. function theme_positioning_parameters($parameters) {
  386. $outputs = array();
  387. foreach ($parameters as $key => $value) {
  388. $outputs[] = "[$key] => $value";
  389. }
  390. return '<pre>' . join("\n", $outputs) . '</pre>';
  391. }
  392. function imagecache_testsuite_positioning_get_tests() {
  393. return array(
  394. 'base' => array(
  395. 'parameters' => array(
  396. 'x' => '0',
  397. 'y' => '0',
  398. ),
  399. 'description' => '0 is top left.',
  400. 'expected' => array(
  401. 'x' => '0',
  402. 'y' => '0',
  403. ),
  404. ),
  405. 'numbers' => array(
  406. 'parameters' => array(
  407. 'x' => '50',
  408. 'y' => '-50',
  409. ),
  410. 'description' => 'Basic numbers indicate distance and direction from top left.',
  411. 'expected' => array(
  412. 'x' => '50',
  413. 'y' => '-50',
  414. ),
  415. ),
  416. 'keywords' => array(
  417. 'parameters' => array(
  418. 'x' => 'center',
  419. 'y' => 'bottom',
  420. ),
  421. 'description' => "Plain keywords will align against the region",
  422. 'expected' => array(
  423. 'x' => '62.5',
  424. 'y' => '50',
  425. ),
  426. ),
  427. 'keyword with offsets' => array(
  428. 'parameters' => array(
  429. 'x' => 'right+10',
  430. 'y' => 'bottom+10',
  431. ),
  432. 'description' => "Keywords can be used with offsets. Positive numbers are in from the named side",
  433. 'expected' => array(
  434. 'x' => '115',
  435. 'y' => '40',
  436. ),
  437. ),
  438. 'keyword with negative offsets' => array(
  439. 'parameters' => array(
  440. 'x' => 'right-10',
  441. 'y' => 'bottom-10',
  442. ),
  443. 'description' => "Negative numbers place the item outside the boundry",
  444. 'expected' => array(
  445. 'x' => '135',
  446. 'y' => '60',
  447. ),
  448. ),
  449. 'percent' => array(
  450. 'parameters' => array(
  451. 'x' => '50%',
  452. 'y' => '50%',
  453. ),
  454. 'description' => "Percentages on their own will CENTER on both the source and destination items",
  455. 'expected' => array(
  456. 'x' => '62.5',
  457. 'y' => '25',
  458. ),
  459. ),
  460. 'keyword with percent' => array(
  461. 'parameters' => array(
  462. 'x' => 'right+10%',
  463. 'y' => 'bottom+10%',
  464. ),
  465. 'description' => "Percentages can be used with keywords, though the placed image will be centered on the calculated position.",
  466. 'expected' => array(
  467. 'x' => '142.5',
  468. 'y' => '85',
  469. ),
  470. ),
  471. 'css styles' => array(
  472. 'parameters' => array(
  473. 'left' => '10px',
  474. 'bottom' => '10px',
  475. ),
  476. 'description' => "A different method uses css-like parameters.",
  477. 'expected' => array(
  478. 'x' => '10',
  479. 'y' => '40',
  480. ),
  481. ),
  482. 'css negatives' => array(
  483. 'parameters' => array(
  484. 'left' => '-10px',
  485. 'bottom' => '-10px',
  486. ),
  487. 'description' => "Negative numbers from sides always move outside of the boundries.",
  488. 'expected' => array(
  489. 'x' => '-10',
  490. 'y' => '60',
  491. ),
  492. ),
  493. 'css with percents' => array(
  494. 'parameters' => array(
  495. 'right' => '+10%',
  496. 'bottom' => '+10%',
  497. ),
  498. 'description' => "Using percents with sides calculates the percent location on the base, then centers the source item on that point.",
  499. 'expected' => array(
  500. 'x' => '142.5',
  501. 'y' => '85',
  502. ),
  503. ),
  504. 'css centering' => array(
  505. 'parameters' => array(
  506. 'right' => '50%',
  507. 'top' => '50%',
  508. ),
  509. 'description' => "The auto-centering that happens when percents are used means you can easily center things at 50%.",
  510. 'expected' => array(
  511. 'x' => '62.5',
  512. 'y' => '25',
  513. ),
  514. ),
  515. 'css positioning' => array(
  516. 'parameters' => array(
  517. 'right' => 'left+20',
  518. 'top' => 'bottom-20',
  519. ),
  520. 'description' => "It's also possible to use keywords there, though it's not smart to do so",
  521. 'expected' => array(
  522. 'x' => '-55',
  523. 'y' => '130',
  524. ),
  525. ),
  526. );
  527. }