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