| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @file 5 * Utility functions for generating FileField content. Note that image 6 * generation support requires the GD toolkit. 7 */ 8 9 /** 10 * Private function used by filefield_content_generate(). 11 */ 12 function _filefield_content_generate($node, $field) { 13 if ($source = _filefield_generate_file($field)) { 14 $file = field_file_save_file($source, array(), filefield_widget_file_path($field)); 15 16 $item = (array) $file; 17 $item['list'] = 1; 18 $item['data']['alt'] = devel_create_greeking(4); 19 $item['data']['title'] = devel_create_greeking(10); 20 } 21 else { 22 $item = array(); 23 } 24 25 return $item; 26 } 27 28 /** 29 * Generate an image based on the properties of a field. 30 * 31 * This is made to work with ImageField, and inspects the minimum and maximum 32 * image sizes and makes sure the generated image matches the requirements. 33 * 34 * @return 35 * The path to the new file, in the temporary directory. 36 */ 37 function _filefield_generate_file($field) { 38 if (empty($field['widget']['file_extensions'])) { 39 $field['widget']['file_extensions'] = 'png jpg txt'; 40 } 41 42 $extensions = array_intersect(explode(' ', $field['widget']['file_extensions']), array('png', 'jpg', 'txt')); 43 $extension = array_rand(drupal_map_assoc($extensions)); 44 45 if ($extension == 'txt') { 46 $filesize = empty($field['widget']['max_filesize_per_file']) ? 1024 : parse_size($field['widget']['max_filesize_per_file']); 47 return _filefield_generate_textfile($filesize); 48 } 49 elseif (in_array($extension, array('png', 'jpg')) && function_exists('imagecreate')) { 50 $min_resolution = empty($field['widget']['min_resolution']) ? '100x100' : $field['widget']['min_resolution']; 51 $max_resolution = empty($field['widget']['max_resolution']) ? '600x600' : $field['widget']['max_resolution']; 52 return _filefield_generate_image($extension, $min_resolution, $max_resolution); 53 } 54 55 return FALSE; 56 } 57 58 /** 59 * Private function for generating a random text file. 60 */ 61 function _filefield_generate_textfile($filesize = 1024) { 62 static $filesizes = array(); 63 64 $temp_file = FALSE; 65 if (isset($filesizes[$filesize])) { 66 $temp_file = $filesizes[$filesize]; 67 } 68 elseif ($temp_file = tempnam(file_directory_temp(), 'filefield_')) { 69 file_move($temp_file, $temp_file .'.txt'); 70 71 $fp = fopen($temp_file, 'w'); 72 fwrite($fp, str_repeat('01', $filesize/2)); 73 fclose($fp); 74 $filesizes[$filesize] = $temp_file; 75 } 76 77 return $temp_file; 78 } 79 80 /** 81 * Private function for creating a random image. 82 * 83 * This function only works with the GD toolkit. ImageMagick is not supported. 84 */ 85 function _filefield_generate_image($extension = 'png', $min_resolution, $max_resolution) { 86 static $images = array(); 87 88 // Generate a max of 5 different images. 89 if (!isset($images[$extension][$min_resolution][$max_resolution]) || count($images[$extension][$min_resolution][$max_resolution]) < 5) { 90 if ($temp_file = tempnam(file_directory_temp(), 'filefield_')) { 91 file_move($temp_file, $temp_file .'.'. $extension); 92 93 $min = explode('x', $min_resolution); 94 $max = explode('x', $max_resolution); 95 96 $width = rand((int)$min[0], (int)$max[0]); 97 $height = rand((int)$min[0], (int)$max[0]); 98 99 // Make a image split into 4 sections with random colors. 100 $im = imagecreate($width, $height); 101 for ($n = 0; $n < 4; $n++) { 102 $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); 103 $x = $width/2 * ($n % 2); 104 $y = $height/2 * (int) ($n >= 2); 105 imagefilledrectangle($im, $x, $y, $x + $width/2, $y + $height/2, $color); 106 } 107 108 // Make a perfect circle in the image middle. 109 $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); 110 $smaller_dimension = min($width, $height); 111 $smaller_dimension = ($smaller_dimension % 2) ? $smaller_dimension : $smaller_dimension; 112 imageellipse($im, $width/2, $height/2, $smaller_dimension, $smaller_dimension, $color); 113 114 $save_function = 'image'. ($extension == 'jpg' ? 'jpeg' : $extension); 115 $save_function($im, $temp_file); 116 117 $images[$extension][$min_resolution][$max_resolution][$temp_file] = $temp_file; 118 } 119 } 120 // Select one of the images we've already generated for this field. 121 else { 122 $temp_file = array_rand($images[$extension][$min_resolution][$max_resolution]); 123 } 124 125 return $temp_file; 126 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |