function imagecache_testsuite_generate in ImageCache Actions 7
Same name and namespace in other branches
- 8 tests/imagecache_testsuite.module \imagecache_testsuite_generate()
- 6.2 tests/imagecache_testsuite.module \imagecache_testsuite_generate()
Returns the requested image derivative.
If the image derivative generation is successful, the function does not return but exits processing using drupal_exit().
Flushes the entire test cache every time anything is done.
Parameters
string $test_id: The id of the test to generate the derivative for.
string $toolkit: The toolkit to use, or empty for the default toolkit
Return value
string|bool
- The html for the page ($test_id is empty)
- False when the image derivative could not be created.
1 string reference to 'imagecache_testsuite_generate'
- imagecache_testsuite_menu in tests/
imagecache_testsuite.module - Implementation of hook_menu().
File
- tests/
imagecache_testsuite.module, line 211
Code
function imagecache_testsuite_generate($test_id = '', $toolkit = '') {
module_load_include('inc', 'image', 'image.admin');
module_load_include('inc', 'image', 'image.effects');
if (empty($toolkit)) {
$toolkit = image_get_toolkit();
}
else {
// Set the toolkit for this invocation only, so do not use variable_set.
global $conf;
$conf['image_toolkit'] = $toolkit;
if ($toolkit === 'gd') {
// This seems not to be done automatically elsewhere.
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'system') . '/' . 'image.gd.inc';
}
}
$target = 'module://imagecache_testsuite/sample.jpg';
$tests = array_merge(image_styles(), imagecache_testsuite_get_tests());
// Run the process and return the image, @see image_style_create_derivative().
$style = $tests[$test_id];
if (!$style) {
trigger_error("Unknown test style preset '{$test_id}' ", E_USER_ERROR);
return FALSE;
}
// @todo: should we let the image style system do its work and just interfere on hook_init with setting the toolkit?
// @todo: this would make the page generator easier as well and keep it working with secure image derivatives.
// Start emulating image_style_create_derivative()
// The main difference being I determine the toolkit I want to use.
// SOME of this code is probably redundant, was a lot of copy&paste without true understanding of the new image.module
if (!($image = image_load($target, $toolkit))) {
trigger_error("Failed to open original image {$target} with toolkit {$toolkit}", E_USER_ERROR);
return FALSE;
}
// Need to save the result before returning it - to stay compatible with imagemagick
$filename = "{$test_id}-{$toolkit}.{$image->info['extension']}";
$derivative_uri = image_style_path($style['name'], $filename);
$directory = dirname($derivative_uri);
file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
watchdog('imagecache_testsuite', 'Checking a save dir %dir', array(
'%dir' => dirname($derivative_uri),
), WATCHDOG_DEBUG);
// Imagemagick is not quite right? place a file where the file is supposed to go
// before I put the real path there? else drupal_realpath() says nuh.
#file_save_data('touch this for imagemagick', $derivative_uri, FILE_EXISTS_REPLACE);
foreach ($style['effects'] as $effect) {
// Need to load the full effect definitions, our test ones don't know all the callback info
$effect_definition = image_effect_definition_load($effect['name']);
if (empty($effect_definition)) {
watchdog('imagecache_testsuite', 'I have no idea what %name is', array(
'%name' => $effect['name'],
), WATCHDOG_ERROR);
continue;
}
$full_effect = array_merge($effect_definition, array(
'data' => $effect['data'],
));
// @todo: effects that involve other images (overlay, underlay) will load that image with the default toolkit which may differ from the toolkit tested here.
if (!image_effect_apply($image, $full_effect)) {
watchdog('imagecache_testsuite', 'action: %action (%callback) failed for %src', array(
'%action' => $full_effect['label'],
'%src' => $target,
'%callback' => $full_effect['effect callback'],
), WATCHDOG_ERROR);
}
}
if (!image_save($image, $derivative_uri)) {
watchdog('imagecache_testsuite', 'saving image %label failed for %derivative_uri', array(
'%derivative_uri' => $derivative_uri,
'%label' => isset($style['label']) ? $style['label'] : $style['name'],
), WATCHDOG_ERROR);
return FALSE;
}
if ($result_image = image_load($derivative_uri)) {
file_transfer($result_image->source, array(
'Content-Type' => $result_image->info['mime_type'],
'Content-Length' => $result_image->info['file_size'],
));
drupal_exit();
}
return "Failed to load the expected result from {$derivative_uri}";
}