123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- <?php
- function imagecache_actions_file_field_description() {
- return t('File is either a file with one of the valid schemes (e.g. private://, public://, module://{module}/{component}, temporary://), or an absolute or relative path (relative to the current directory, probably the Drupal site root).');
- }
- function imagecache_actions_validate_file(&$element, &$form_status) {
- if (!imagecache_actions_find_file($element['#value'])) {
- form_error($element, t("Unable to find the file '%file'. Please check the path.", array('%file' => $element['#value'])) );
- }
- }
- function imagecache_actions_find_file($file) {
- $result = FALSE;
- if (is_readable($file)) {
- $result = drupal_realpath($file);
- }
- return $result;
- }
- function imagecache_actions_image_load($file, $toolkit = FALSE) {
- $full_path = imagecache_actions_find_file($file);
- if (!$full_path) {
- trigger_error("Failed to find file $file.", E_USER_ERROR);
- return FALSE;
- }
- $image = image_load($full_path, $toolkit);
- if (!$image) {
- trigger_error("Failed to open file '$file' as an image resource.", E_USER_ERROR);
- }
- return $image;
- }
- function imagecache_actions_get_image_context($image, $data) {
-
- $image_context = array(
- 'effect_data' => $data,
- 'managed_file' => NULL,
- 'referring_entities' => array(),
- 'entity' => NULL,
- 'image_field' => NULL,
- );
-
- $managed_file = reset(file_load_multiple(array(), array('uri' => $image->source)));
- if ($managed_file !== FALSE) {
- $image_context['managed_file'] = $managed_file;
-
- $references = file_get_file_references($managed_file, NULL, FIELD_LOAD_CURRENT, 'image');
- if ($references) {
-
- foreach ($references as $field_name => $field_references) {
- foreach ($field_references as $entity_type => $entity_stubs) {
- $image_context['referring_entities'][$field_name][$entity_type] = entity_load($entity_type, array_keys($entity_stubs));
- }
- }
-
- reset($image_context['referring_entities']);
- list($field_name, $field_references) = each($image_context['referring_entities']);
- reset($field_references);
- list($entity_type, $entities) = each($field_references);
- reset($entities);
- list($entity_id, $image_context['entity']) = each($entities);
- $image_field = field_get_items($entity_type, $image_context['entity'], $field_name);
- if ($image_field !== FALSE) {
-
- foreach ($image_field as $image_field_value) {
- if ($image_field_value['fid'] === $managed_file->fid) {
- $image_context['image_field'] = $image_field_value;
- }
- }
- }
- }
- }
-
- return $image_context;
- }
- function imagecache_actions_calculate_relative_position($base, $layer, $style) {
-
- if (isset($style['bottom'])) {
- $ypos = imagecache_actions_calculate_offset('bottom', $style['bottom'], $base->info['height'], $layer->info['height']);
- }
- if (isset($style['top'])) {
- $ypos = imagecache_actions_calculate_offset('top', $style['top'], $base->info['height'], $layer->info['height']);
- }
- if (isset($style['right'])) {
- $xpos = imagecache_actions_calculate_offset('right', $style['right'], $base->info['width'], $layer->info['width']);
- }
- if (isset($style['left'])) {
- $xpos = imagecache_actions_calculate_offset('left', $style['left'], $base->info['width'], $layer->info['width']);
- }
- if (! isset($ypos)) {
-
- $ypos = ($base->info['height'] / 2) - ($layer->info['height'] / 2);
- }
- if (! isset($xpos)) {
-
- $xpos = ($base->info['width'] / 2) - ($layer->info['width'] / 2);
- }
-
-
- return array('x' => $xpos, 'y' => $ypos);
- }
- function imagecache_actions_calculate_offset($keyword, $value, $base_size, $layer_size) {
- $offset = 0;
- $direction = 1;
- $base = 0;
- if ($keyword == 'right' || $keyword == 'bottom') {
- $direction = -1;
- $offset = -1 * $layer_size;
- $base = $base_size;
- }
- if ($keyword == 'middle' || $keyword == 'center') {
- $base = $base_size / 2;
- $offset = -1 * ($layer_size / 2);
- }
-
- switch ($value) {
- case 'left':
- case 'top':
- $value = 0;
- break;
- case 'middle':
- case 'center':
- $value = $base_size / 2;
- break;
- case 'bottom':
- case 'right':
- $value = $base_size;
- }
-
-
-
- if (preg_match('/^(.+)([\+\-])(\d+)([^\d]*)$/', $value, $results)) {
- list($match, $value_key, $value_mod, $mod_value, $mod_unit) = $results;
- if ($mod_unit == '%') {
- $mod_value = $mod_value / 100 * $base_size;
- $mod_unit = 'px';
- }
- $mod_direction = ($value_mod == '-') ? -1 : + 1;
- switch ($value_key) {
- case 'left':
- case 'top':
- $mod_base = 0;
- break;
- case 'middle':
- case 'center':
- $mod_base = $base_size / 2;
- break;
- case 'bottom':
- case 'right':
- $mod_base = $base_size;
- }
- $modified_value = $mod_base + ($mod_direction * $mod_value);
- return $modified_value;
- }
-
-
- if (substr($value, strlen($value) -1, 1) == '%') {
- $value = intval($value / 100 * $base_size);
- $offset = -1 * ($layer_size / 2);
- }
- $value = $base + ($direction * $value);
-
-
-
- return $value + $offset;
- }
- function imagecache_actions_hex2rgba($hex) {
- $hex = ltrim($hex, '#');
- if (preg_match('/^[0-9a-f]{3}$/i', $hex)) {
-
- $r = str_repeat($hex{0}, 2);
- $g = str_repeat($hex{1}, 2);
- $b = str_repeat($hex{2}, 2);
- $a = '0';
- }
- elseif (preg_match('/^[0-9a-f]{6}$/i', $hex)) {
-
- list($r, $g, $b) = str_split($hex, 2);
- $a = '0';
- }
- elseif (preg_match('/^[0-9a-f]{8}$/i', $hex)) {
-
- list($r, $g, $b, $a) = str_split($hex, 2);
- }
- elseif (preg_match('/^[0-9a-f]{4}$/i', $hex)) {
-
- $r = str_repeat($hex{0}, 2);
- $g = str_repeat($hex{1}, 2);
- $b = str_repeat($hex{2}, 2);
- $a = str_repeat($hex{3}, 2);
- }
- else {
-
- return FALSE;
- }
- $r = hexdec($r);
- $g = hexdec($g);
- $b = hexdec($b);
- $a = hexdec($a);
-
- if ($a > 127) {
- $a = (int)$a/2;
- }
- return array('red' => $r, 'green' => $g, 'blue' => $b, 'alpha' => $a);
- }
- function imagecache_actions_keyword_filter($value, $base_size, $layer_size) {
-
- if (! preg_match('/([a-z]*)([\+\-]?)(\d*)([^\d]*)/', $value, $results) ) {
- trigger_error("imagecache_actions had difficulty parsing the string '$value' when calculating position. Please check the syntax.", E_USER_WARNING);
- }
- list($match, $keyword, $plusminus, $value, $unit) = $results;
-
-
- return imagecache_actions_calculate_offset($keyword, $plusminus . $value . $unit, $base_size, $layer_size);
- }
- function imagecache_include_standard_actions() {
-
-
- }
- function imagecache_actions_fields_from_filepath($filepath, $load_entity = TRUE) {
- if (!module_exists('file')) {
- return;
- }
-
-
- if (! file_valid_uri($filepath)) {
- $filepath = file_build_uri($filepath);
- }
- $files = file_load_multiple(array(), array('uri' => $filepath));
- if ($files) {
- $fields = array();
- foreach ($files as $fid => $file) {
- $references = file_get_file_references($file, NULL, FIELD_LOAD_CURRENT, 'image');
- $fields[$fid] = $references;
- }
- if ($load_entity) {
-
- imagecache_actions_entities_from_references($fields);
- }
- return $fields;
- }
- }
- function imagecache_actions_entities_from_references(&$fields) {
- foreach ($fields as $fid => $field_ids) {
- foreach ($field_ids as $field_id => $entity_types) {
- foreach ($entity_types as $entity_type => $entity_instances) {
-
- foreach ($entity_instances as $entity_id => $entity) {
- $entity = entity_load_single($entity_type, $entity_id);
-
- $fields[$fid][$field_id][$entity_type][$entity_id] = $entity;
-
-
-
-
-
-
-
-
-
-
-
- }
- }
- }
- }
- }
- function imagecache_actions_percent_filter($value, $current_pixels) {
- if (strpos($value, '%') !== false) {
- $value = str_replace('%', '', $value) * 0.01 * $current_pixels;
- }
- return $value;
- }
|