function create_image($file_name, $base = '', $height = '', $percent = '', $width = '', $direction = '', $numcolors = '', $color1 = '', $color2 = '') { global $DEFAULTS, $MAX_COLORS, $MAX_HEIGHT, $MAX_WIDTH, $MIN_COLORS; if ($base != '') { $color1 = $color2 = $base; } $color1 = $color1 == '' ? colorToRGB($DEFAULTS['color1']) : (preg_match("/^#?([0-9a-fA-F]{3,6})/", $color1, $matches) ? colorToRGB($matches[1]) : colorToRGB($DEFAULTS['color1'])); $color2 = $color2 == '' ? colorToRGB($DEFAULTS['color2']) : (preg_match("/^#?([0-9a-fA-F]{3,6})/", $color2, $matches) ? colorToRGB($matches[1]) : colorToRGB($DEFAULTS['color2'])); if (empty($height)) { $height = $DEFAULTS['height']; } if ($height > $MAX_HEIGHT) { $height = $MAX_HEIGHT; } if ($direction == '' || $direction % 90 != 0) { $direction = $DEFAULTS['direction']; } else { while ($direction > 360) { $direction -= 360; } } if ($direction == 90 || $direction == 270) { // Vertical gradient if (empty($height)) { $height = $DEFAULTS['height']; } if ($height > $MAX_HEIGHT) { $height = $MAX_HEIGHT; } $width = 1; } else { // Horizontal gradient if (empty($width)) { $width = $DEFAULTS['width']; } if ($width > $MAX_WIDTH) { $width = $MAX_WIDTH; } $height = 1; } if (empty($numcolors)) { $numcolors = $DEFAULTS['colors']; } else { if (preg_match('/^\\d+$/', $numcolors)) { if ($numcolors < $MIN_COLORS) { $numcolors = $MIN_COLORS; } else { if ($numcolors > $MAX_COLORS) { $numcolors = $MAX_COLORS; } } } else { $numcolors = $DEFAULTS['colors']; } } if ($percent == '' || $percent < 0 || $percent > 100) { $percent = $DEFAULTS['percent']; } $percent *= 2.55; $color2['red'] = min($color2['red'] + $percent, 255); $color2['green'] = min($color2['green'] + $percent, 255); $color2['blue'] = min($color2['blue'] + $percent, 255); $image = imagecreate($width, $height); // Allocate array of colors $colors = array(); $deltared = $color2['red'] - $color1['red']; $deltagreen = $color2['green'] - $color1['green']; $deltablue = $color2['blue'] - $color1['blue']; $tmp_c = $numcolors - 1; for ($i = 0; $i < $numcolors; $i++) { $thisred = floor(min($color1['red'] + $deltared * $i / $tmp_c, 255)); $thisgreen = floor(min($color1['green'] + $deltagreen * $i / $tmp_c, 255)); $thisblue = floor(min($color1['blue'] + $deltablue * $i / $tmp_c, 255)); $colors[$i] = imagecolorallocate($image, $thisred, $thisgreen, $thisblue); } $dim = $width; $dx = $dy = $i = $x1 = $y1 = 0; $x2 = $width - 1; $y2 = $height - 1; switch ($direction) { case 0: $dx = 1; $x2 = 0; break; case 90: $dim = $height; $dy = -1; $y1 = $height - 1; break; case 180: $dx = -1; $x1 = $width - 1; break; case 270: $dim = $height; $dy = 1; $y2 = 0; break; } while ($x1 >= 0 && $x1 < $width && $x2 >= 0 && $x2 < $width && $y1 >= 0 && $y1 < $height && $y2 >= 0 && $y2 < $height) { // Which color for this line? $ind = floor($numcolors * $i / $dim); if ($ind >= $numcolors) { $ind = $numcolors; } imageline($image, $x1, $y1, $x2, $y2, $colors[$ind]); $x1 += $dx; $y1 += $dy; $x2 += $dx; $y2 += $dy; $i++; } if (function_exists('imagepng')) { if ($file_name == '') { header('Content-type: image/png'); imagepng($image); } else { imagepng($image, $file_name); } } elseif (function_exists('imagegif')) { if ($file_name == '') { header('Content-type: image/gif'); imagegif($image); } else { imagegif($image, $file_name); } } else { echo 'No image formats supported!<br />' . "\n"; } imagedestroy($image); return; }
} else { if (preg_match("/^#?([0-9a-fA-F]{3,6})/", $color1, $matches)) { $color1 = colorToRGB($matches[1]); } else { $color1 = colorToRGB($DEFAULTS['color1']); } } if ($base === null) { $color2 = getGetValue('color2'); if ($color2 === null) { $color2 = colorToRGB($DEFAULTS['color2']); } else { if (preg_match("/^#?([0-9a-fA-F]{3,6})/", $color2, $matches)) { $color2 = colorToRGB($matches[1]); } else { $color2 = colorToRGB($DEFAULTS['color2']); } } } else { $color2 = $color1; $percent = getGetValue('percent'); if ($percent === null || $percent < 0 || $percent > 100) { $percent = $DEFAULTS['percent']; } $color2['red'] = min($color2['red'] + $percent * 255 / 100, 255); $color2['green'] = min($color2['green'] + $percent * 255 / 100, 255); $color2['blue'] = min($color2['blue'] + $percent * 255 / 100, 255); } $image = imagecreate($width, $height); // Allocate array of colors $colors = array();