public static function fromBinary($binary) { $handle = imagecreatefromstring($binary); $whiteColor = imagecolorresolve($handle, 0xff, 0xff, 0xff); $blackColor = imagecolorresolve($handle, 0x0, 0x0, 0x0); return new self($handle, $whiteColor, $blackColor); }
public function toPngColor($image) { $color = imagecolorallocate($image, $this->red, $this->green, $this->blue); if ($color == false) { $color = imagecolorresolve($image, $this->red, $this->green, $this->blue); } return $color; }
public function testGetColorAt() { $CGraphImageHandler = new ImageHandler(new Image(__DIR__ . '/resource/color_red.jpg')); $this->assertEquals(imagecolorresolve($CGraphImageHandler->getImage(), 254, 0, 0), $CGraphImageHandler->getColorAt(1, 1)); $CGraphImageHandler = new ImageHandler(new Image(__DIR__ . '/resource/color_green.jpg')); $this->assertEquals(imagecolorresolve($CGraphImageHandler->getImage(), 0, 255, 1), $CGraphImageHandler->getColorAt(1, 1)); $CGraphImageHandler = new ImageHandler(new Image(__DIR__ . '/resource/color_blue.jpg')); $this->assertEquals(imagecolorresolve($CGraphImageHandler->getImage(), 0, 0, 254), $CGraphImageHandler->getColorAt(1, 1)); }
/** * This function will be available automatically after the module is imported with the module control. * Using the custom prefix this function will be callable from PHP and JSON-RPC through: * * UWZ_RequestInfo($id); * */ public function RequestInfo() { $imagePath = IPS_GetKernelDir() . $this->imagePath; $area = $this->ReadPropertyString("area"); $homeX = $this->ReadPropertyInteger("homeX"); $homeY = $this->ReadPropertyInteger("homeY"); $homeRadius = $this->ReadPropertyInteger("homeRadius"); //Calculate time $minute = floor(date("i") / 15) * 15; $dateline = mktime(date("H"), $minute, 0, date("m"), date("d"), date("y")); //Download picture $opts = array('http' => array('method' => "GET", 'max_redirects' => 1)); $context = stream_context_create($opts); $remoteImage = "http://www.wetteronline.de/daten/radar/{$area}/" . gmdate("Y", $dateline) . "/" . gmdate("m", $dateline) . "/" . gmdate("d", $dateline) . "/" . gmdate("Hi", $dateline) . ".gif"; $data = @file_get_contents($remoteImage, false, $context); if ($data === false) { //No new picture. Download old one. $dateline -= 15 * 60; $remoteImage = "http://www.wetteronline.de/daten/radar/{$area}/" . gmdate("Y", $dateline) . "/" . gmdate("m", $dateline) . "/" . gmdate("d", $dateline) . "/" . gmdate("Hi", $dateline) . ".gif"; $data = @file_get_contents($remoteImage, false, $context); if ($data === false) { return; } } if (strpos($http_response_header[0], "200") === false) { return; } file_put_contents($imagePath, $data); //Radarbild auswerten $im = ImageCreateFromGIF($imagePath); //Stärken $regen[6] = imagecolorresolve($im, 250, 2, 250); $regen[5] = imagecolorresolve($im, 156, 50, 156); $regen[4] = imagecolorresolve($im, 28, 126, 220); $regen[3] = imagecolorresolve($im, 44, 170, 252); $regen[2] = imagecolorresolve($im, 84, 210, 252); $regen[1] = imagecolorresolve($im, 172, 254, 252); //Pixel durchgehen $regenmenge = 0; for ($x = $homeX - $homeRadius; $x <= $homeX + $homeRadius; $x++) { for ($y = $homeY - $homeRadius; $y <= $homeY + $homeRadius; $y++) { $found = array_search(imagecolorat($im, $x, $y), $regen); if (!($found === FALSE)) { $regenmenge += $found; } } } // Bereich zeichnen $schwarz = ImageColorAllocate($im, 0, 0, 0); $rot = ImageColorAllocate($im, 255, 0, 0); imagerectangle($im, $homeX - $homeRadius, $homeY - $homeRadius, $homeX + $homeRadius, $homeY + $homeRadius, $rot); imagesetpixel($im, $homeX, $homeY, $rot); imagegif($im, $localImage); imagedestroy($im); SetValue($this->GetIDForIdent("RainValue"), $regenmenge); }
function horizontal($start, $end) { for ($x = 0; $x < $this->gradient_width; $x++) { foreach ($start as $k => $v) { $diff = $start[$k] - $end[$k]; $new[$k] = $start[$k] - intval($diff / $this->gradient_width * $x); } $col_color = imagecolorresolve($this->image, $new['red'], $new['green'], $new['blue']); imagefilledrectangle($this->image, $x, 0, $x, $this->gradient_height, $col_color); } }
function my_draw($img, $plot) { $black = imagecolorresolve($img, 0, 0, 0); $x = 100; $y = 50; $dy = 30; $plot->DrawText('', 0, $x, $y += $dy, $black, 'Font="" (generic): sans italic 12pt'); $plot->DrawText('generic', 0, $x, $y += $dy, $black, 'Font="generic": sans italic 12pt'); $plot->DrawText('legend', 0, $x, $y += $dy, $black, 'Font="legend": serif bold 14pt'); $plot->DrawText($plot->fonts['title'], 0, $x, $y += $dy, $black, 'Font=fonts["title"]: sans 12pt'); $plot->DrawText('x_title', 0, $x, $y += $dy, $black, 'Font="x_title": mono bold 10pt'); }
function captcha() { require dirname(__FILE__) . '/captcha-config.php'; $this->keystring = ''; for ($i = 0; $i < $length; $i++) { $this->keystring .= $use_symbols[mt_rand(0, $use_symbols_len - 1)]; } $im = imagecreatefromgif(dirname(__FILE__) . "/back.gif"); $width = imagesx($im); $height = imagesy($im); $rc = mt_rand(120, 140); $font_color = imagecolorresolve($im, $rc, $rc, $rc); $px = $margin_left; for ($i = 0; $i < $length; $i++) { imagettftext($im, $font_size, 0, $px, $margin_top, $font_color, dirname(__FILE__) . "/CARTOON8.TTF", $this->keystring[$i]); $px += $font_width + mt_rand($rand_bsimb_min, $rand_bsimb_max); } $h_y = mt_rand(0, $height); $h_y1 = mt_rand(0, $height); imageline($im, mt_rand(0, 20), $h_y, mt_rand($width - 20, $width), $h_y1, $font_color); imageline($im, mt_rand(0, 20), $h_y, mt_rand($width - 20, $width), $h_y1, $font_color); $h_y = mt_rand(0, $height); $h_y1 = mt_rand(0, $height); imageline($im, mt_rand(0, 20), $h_y, mt_rand($width - 20, $width), $h_y1, $font_color); imageline($im, mt_rand(0, 20), $h_y, mt_rand($width - 20, $width), $h_y1, $font_color); image_make_pomexi($im, 50, 80); $rand = mt_rand(0, 1); if ($rand) { $rand = -1; } else { $rand = 1; } wave_region($im, 0, 0, $width, $height, $rand * mt_rand($amplitude_min, $amplitude_max), mt_rand(30, 40)); header('Expires: Sat, 17 May 2008 05:00:00 GMT'); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', FALSE); header('Pragma: no-cache'); if (function_exists("imagejpeg")) { header("Content-Type: image/jpeg"); imagejpeg($im, null, $jpeg_quality); } else { if (function_exists("imagegif")) { header("Content-Type: image/gif"); imagegif($im); } else { if (function_exists("imagepng")) { header("Content-Type: image/x-png"); imagepng($im); } } } }
function twMachTransparent($img) { $farbePixel = imagecolorat($img, $_SESSION['klamotteDruckmass'][0] + 1, $_SESSION['klamotteDruckmass'][1] + 1); $farbwerte = imagecolorsforindex($img, $farbePixel); $transp = imagecolorresolve($img, $farbwerte["red"], $farbwerte["green"], $farbwerte["blue"]); ///$transp = imagecolorallocate($ziel, $farbwerte["red"], $farbwerte["green"], $farbwerte["blue"]); ///$transp = imagecolorallocatealpha($ziel, $farbwerte["red"], $farbwerte["green"], $farbwerte["blue"], 53); imagefill($img, 0, 0, $transp); // sonst is Rest des Bildes schwarz imagecolortransparent($img, $transp); //imagealphablending ($ziel, false); //imagesavealpha($target, true); return $img; }
/** * Write text on images */ function write_text($source_pic, $dest_pic, $text_content, $text_font, $text_size = 10, $text_color = '#FFFFFF', $text_position = '0') { $temp_pic = imagecreatefromgif($source_pic); list($image_width, $image_height) = getimagesize($source_pic); /* $temp_pic_empty = imagecreatetruecolor ($image_width, $image_height); imagealphablending($temp_pic_empty, false); imagecopyresampled($temp_pic_empty, $temp_pic, 0, 0, 0, 0, $image_width, $image_height, $image_width, $image_height); imagesavealpha($temp_pic_empty, true); //imagepng($im_re, 'small_redfade.png'); $temp_pic2 = imagecreatefrompng($source_pic); */ // Calculate the centre for (;;) { list($left_x, , $right_x) = imagettfbbox($text_size, $text_position, $text_font, $text_content); $text_width = $right_x - $left_x; if ($image_width > $text_width + 5) { break; } $text_size = $text_size - 0.5; if ($text_size == 1) { die('Font size may not be reduced further, try to insert a shorter text'); } } $text_padding = ($image_width - $text_width) / 2; $text_color = substr($text_color, 0, 1) == '#' ? substr($text_color, 1, 6) : $text_color; $text_color_r = hexdec(substr($text_color, 0, 2)); $text_color_g = hexdec(substr($text_color, 2, 2)); $text_color_b = hexdec(substr($text_color, 4, 2)); $text_color = imagecolorresolve($temp_pic, $text_color_r, $text_color_g, $text_color_b); //$text_color = imagecolorallocate($temp_pic, $text_color_r, $text_color_g, $text_color_b); imagettftext($temp_pic, $text_size, $text_position, $text_padding, $image_height - $text_size / 2, $text_color, $text_font, $text_content); if ($_GET['dl']) { header('Content-Disposition: attachment; filename="avatar.gif"'); } /* header("Content-type: image/png"); imagepng($temp_pic, $dest_pic); imagepng($temp_pic); imagedestroy($temp_pic); */ header('Content-type: image/gif'); imagegif($temp_pic, $dest_pic); imagegif($temp_pic); imagedestroy($temp_pic); return true; }
function cb($img, $arg) { global $wasnt_called; static $x = 20; static $y = 20; static $color_index = -1; // Color not yet allocated. # Cannot output this in normal test mode or it thinks we failed. # fwrite(STDERR, "callback: $arg\n"); # Make a color index. Use gray (146,146,146) for compatibility with # older versions of this test which blindly used color index 1. if ($color_index == -1) { $color_index = imagecolorresolve($img, 146, 146, 146); } imagestring($img, 3, $x, $y, "CB: {$arg}", $color_index); $y += 20; # Mark it has being called: unset($wasnt_called[$arg]); }
function annotate_plot($img, $plot) { global $best_index, $best_sales, $worst_index, $worst_sales; # Allocate our own colors, rather than poking into the PHPlot object: $red = imagecolorresolve($img, 255, 0, 0); $green = imagecolorresolve($img, 0, 216, 0); # Get the pixel coordinates of the data points for the best and worst: list($best_x, $best_y) = $plot->GetDeviceXY($best_index, $best_sales); list($worst_x, $worst_y) = $plot->GetDeviceXY($worst_index, $worst_sales); # Draw ellipses centered on those two points: imageellipse($img, $best_x, $best_y, 50, 20, $green); imageellipse($img, $worst_x, $worst_y, 50, 20, $red); # Place some text above the points: $font = '3'; $fh = imagefontheight($font); $fw = imagefontwidth($font); imagestring($img, $font, $best_x - $fw * 4, $best_y - $fh - 10, 'Good Job!', $green); # We can also use the PHPlot internal function for text. # It does the center/bottom alignment calculations for us. # Specify the font argument as NULL or '' to use the generic one. $plot->DrawText('', 0, $worst_x, $worst_y - 10, $red, 'Bad News!', 'center', 'bottom'); }
/** * Rotates image by the given angle * * Uses a fast rotation algorythm for custom angles * or lines copy for multiple of 90 degrees * * @param int $angle Rotation angle * @param array $options array( * 'canvasColor' => array(r ,g, b), named color or #rrggbb * ) * @author Pierre-Alain Joye * @return bool|PEAR_Error TRUE or a PEAR_Error object on error * @access public */ function rotate($angle, $options = null) { if ($angle % 360 == 0) { return true; } $color_mask = $this->_getColor('canvasColor', $options, array(255, 255, 255)); $mask = imagecolorresolve($this->imageHandle, $color_mask[0], $color_mask[1], $color_mask[2]); $this->old_image = $this->imageHandle; // Multiply by -1 to change the sign, so the image is rotated clockwise $this->imageHandle = ImageRotate($this->imageHandle, $angle * -1, $mask); return true; }
/** * addText * * @param array options Array contains options * array( * 'text' The string to draw * 'x' Horizontal position * 'y' Vertical Position * 'Color' Font color * 'font' Font to be used * 'size' Size of the fonts in pixel * 'resize_first' Tell if the image has to be resized * before drawing the text * ) * * @return none * @see PEAR::isError() */ function addText($params) { $default_params = array('text' => 'This is Text', 'x' => 10, 'y' => 20, 'color' => array(255, 0, 0), 'font' => 'Arial.ttf', 'size' => '12', 'angle' => 0, 'resize_first' => false); $params = array_merge($default_params, $params); extract($params); if (!is_array($color)) { if ($color[0] == '#') { $this->colorhex2colorarray($color); } else { include_once 'Image/Transform/Driver/ColorsDefs.php'; $color = isset($colornames[$color]) ? $colornames[$color] : false; } } $c = imagecolorresolve($this->imageHandle, $color[0], $color[1], $color[2]); if ('ttf' == substr($font, -3)) { ImageTTFText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); } else { ImagePSText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); } return true; }
function DrawMessage($text, $options = NULL) { // Merge options with defaults, and set as local variables: extract(array_merge(array('draw_background' => FALSE, 'draw_border' => FALSE, 'force_print' => TRUE, 'reset_font' => TRUE, 'text_color' => '', 'text_wrap' => TRUE, 'wrap_width' => 75), (array) $options)); // Do colors, background, and border: if ($draw_border && !isset($this->ndx_i_border) || $draw_background && !isset($this->ndx_bg_color)) { $this->SetBgColorIndexes(); } if ($draw_background) { // User-specified background $this->DrawBackground(TRUE); // TRUE means force overwriting of background } else { // Default to plain white background $bgcolor = imagecolorresolve($this->img, 255, 255, 255); ImageFilledRectangle($this->img, 0, 0, $this->image_width, $this->image_height, $bgcolor); } if ($draw_border) { $this->DrawImageBorder(TRUE); } if (empty($text_color)) { $rgb = array(0, 0, 0); } else { $rgb = $this->SetRGBColor($text_color); } $ndx_text_color = imagecolorresolve($this->img, $rgb[0], $rgb[1], $rgb[2]); // Error images should reset fonts, to avoid chance of a TTF error when displaying an error. if ($reset_font) { $this->SetUseTTF(FALSE); } // Determine the space needed for the text, and center the text box within the image: if ($text_wrap) { $text = wordwrap($text, $wrap_width); } list($text_width, $text_height) = $this->SizeText('generic', 0, $text); $x = max($this->safe_margin, ($this->image_width - $text_width) / 2); $y = max($this->safe_margin, ($this->image_height - $text_height) / 2); $this->DrawText('generic', 0, $x, $y, $ndx_text_color, $text, 'left', 'top'); if ($force_print || $this->print_image) { $this->PrintImage(); } return TRUE; }
function imagegreyscale(&$img) { $x = imagesx($img); $y = imagesy($img); for ($i = 0; $i < $y; $i++) { for ($j = 0; $j < $x; $j++) { $pos = imagecolorat($img, $j, $i); $f = imagecolorsforindex($img, $pos); $gst = $f['red'] * 0.15 + $f['green'] * 0.5 + $f['blue'] * 0.35; $col = imagecolorresolve($img, $gst, $gst, $gst); imagesetpixel($img, $j, $i, $col); } } }
/** * Apply a wave filter to an image * * @param image image Image to convert * @param int wave Amount of wave to apply * @param bool randirection Randomize direction of wave * * @return image */ function _wave(&$image, $wave = 10, $randirection = true) { $image_width = imagesx($image); $image_height = imagesy($image); $temp = $this->_imagecreate($image_width, $image_height); if ($randirection) { $direction = mt_rand(0, 1) == 1 ? true : false; } for ($x = 0; $x < $image_width; $x++) { for ($y = 0; $y < $image_height; $y++) { $xo = $wave * sin(2 * 3.1415 * $y / 128); $yo = $wave * cos(2 * 3.1415 * $x / 128); if ($direction) { $newx = $x - $xo; $newy = $y - $yo; } else { $newx = $x + $xo; $newy = $y + $yo; } if ($newx > 0 and $newx < $image_width and ($newy > 0 and $newy < $image_height)) { $index = imagecolorat($image, $newx, $newy); $colors = imagecolorsforindex($image, $index); $color = imagecolorresolve($temp, $colors['red'], $colors['green'], $colors['blue']); } else { $color = imagecolorresolve($temp, 255, 255, 255); } imagesetpixel($temp, $x, $y, $color); } } return $temp; }
/** * * Apply a wave filter to an image * * @param image image Image to convert * @param int wave Amount of wave to apply * @param bool randirection Randomize direction of wave * * @return image */ protected function &wave(&$image, $wave = 10, $randirection = true) { $image_width = imagesx($image); $image_height = imagesy($image); $temp = imagecreatetruecolor($image_width, $image_height); if ($randirection) { $direction = vB::getRequest()->getTimeNow() & 2 ? true : false; } $middlex = floor($image_width / 2); $middley = floor($image_height / 2); for ($x = 0; $x < $image_width; $x++) { for ($y = 0; $y < $image_height; $y++) { $xo = $wave * sin(2 * 3.1415 * $y / 128); $yo = $wave * cos(2 * 3.1415 * $x / 128); if ($direction) { $newx = $x - $xo; $newy = $y - $yo; } else { $newx = $x + $xo; $newy = $y + $yo; } if ($newx > 0 and $newx < $image_width and ($newy > 0 and $newy < $image_height)) { $index = imagecolorat($image, $newx, $newy); $colors = imagecolorsforindex($image, $index); $color = imagecolorresolve($temp, $colors['red'], $colors['green'], $colors['blue']); } else { $color = imagecolorresolve($temp, 255, 255, 255); } imagesetpixel($temp, $x, $y, $color); } } return $temp; }
/** * For GDLib V1.0 * Creates the rectangle sets the colors and the text and saves the new image. Returns void. * @public * @returns void */ function make_copyright_gd1($datei) { $this->set_imageinfo($datei); $rectanglelen = strlen($this->copytext) * 7.5; if ($this->imageinfo[2] == 1) { $img = imagecreatefromgif("{$datei}"); } if ($this->imageinfo[2] == 2) { $img = imagecreatefromjpeg("{$datei}"); } if ($this->imageinfo[2] == 3) { $img = imagecreatefrompng("{$datei}"); } $textcolor = imagecolorresolve($img, $this->copytextcolor[red], $this->copytextcolor[green], $this->copytextcolor[blue]); $bgcolor = imagecolorresolve($img, $this->copybgcolor[red], $this->copybgcolor[green], $this->copybgcolor[blue]); $korrektur = -25; if ($this->copyinpicture == 2) { if ($this->copyposition == 1 or $this->copyposition == 4) { $up = 25; $rectx1 = 0; $recty1 = 0; $rectx2 = $this->imageinfo[0]; $recty2 = 25; } if ($this->copyposition == 2 or $this->copyposition == 3) { $up = 0; $rectx1 = 0; $recty1 = $this->imageinfo[1]; $rectx2 = $this->imageinfo[0]; $recty2 = $this->imageinfo[1] + 25; } $korrektur = 0; $imgh = imagecreate($this->imageinfo[0], $this->imageinfo[1] + 25); imagecopy($imgh, $img, 0, $up, 0, 0, $this->imageinfo[0], $this->imageinfo[1]); $img = $imgh; $textcolor = imagecolorresolve($img, $this->copytextcolor[red], $this->copytextcolor[green], $this->copytextcolor[blue]); $bgcolor = imagecolorresolve($img, $this->copybgcolor[red], $this->copybgcolor[green], $this->copybgcolor[blue]); imagefilledrectangle($img, $rectx1, $recty1, $rectx2, $recty2, $bgcolor); } if ($this->copyposition == 1) { $recty1 = 0; $rectx1 = 0; $rectx2 = $rectx1 + $rectanglelen; $recty2 = $recty1 + 25; $textup = 5; $textleft = 7; } elseif ($this->copyposition == 2) { $recty1 = $this->imageinfo[1] + $korrektur; $rectx1 = 0; $rectx2 = $rectx1 + $rectanglelen; $recty2 = $recty1 + 25; $textup = $recty1 + 5; $textleft = 7; } elseif ($this->copyposition == 3) { $rectx1 = $this->imageinfo[0] - $rectanglelen; $rectx2 = $this->imageinfo[0]; $recty1 = $this->imageinfo[1] + $korrektur; $recty2 = $recty1 + 25; $textup = $recty1 + 5; $textleft = $this->imageinfo[0] - $rectanglelen + 7; } elseif ($this->copyposition == 4) { $recty1 = 0; $rectx1 = $this->imageinfo[0] - $rectanglelen; $rectx2 = $rectx1 + $rectanglelen; $recty2 = $recty1 + 25; $textup = 5; $textleft = $this->imageinfo[0] - $rectanglelen + 7; } if ($this->ractanglebg == 1) { imagefilledrectangle($img, $rectx1, $recty1, $rectx2, $recty2, $bgcolor); } imagestring($img, 3, $textleft, $textup, $this->copytext, $textcolor); if ($this->imageinfo[2] == 1) { imagegif($img, "{$datei}"); } if ($this->imageinfo[2] == 2) { if ($this->{$jpg_IPTC}['create'] == 1) { /* $this->jpg_IPTC['name'] = $name; $this->jpg_IPTC['copyright'] = $copyright; $this->jpg_IPTC['creator'] = $creator; //array iptcembed ( string iptcdata, string jpeg_file_name [, int spool]) */ } imagejpeg($img, "{$datei}"); } if ($this->imageinfo[2] == 3) { imagepng($img, "{$datei}"); } }
/** * Rotate image by the given angle * Uses a fast rotation algorythm for custom angles * or lines copy for multiple of 90 degrees * * @param int $angle Rotation angle * @param array $options array( 'autoresize'=>true|false, * 'color_mask'=>array(r,g,b), named color or #rrggbb * ) * @author Pierre-Alain Joye * @return mixed none or a PEAR error object on error * @see PEAR::isError() */ function rotate($angle, $options = null) { if (function_exists('imagerotate')) { $white = imagecolorallocate($this->imageHandle, 255, 255, 255); $this->imageHandle = imagerotate($this->imageHandle, $angle, $white); return true; } if ($options == null) { $autoresize = true; $color_mask = array(255, 255, 0); } else { extract($options); } while ($angle <= -45) { $angle += 360; } while ($angle > 270) { $angle -= 360; } $t = deg2rad($angle); if (!is_array($color_mask)) { if ($color[0] == '#') { $this->colorhex2colorarray($color_mask); } else { include_once 'Image/Transform/Driver/ColorDefs.php'; $color = isset($colornames[$color_mask]) ? $colornames[$color_mask] : false; } } // Do not round it, too much lost of quality $cosT = cos($t); $sinT = sin($t); $img =& $this->imageHandle; $width = $max_x = $this->img_x; $height = $max_y = $this->img_y; $min_y = 0; $min_x = 0; $x1 = round($max_x / 2, 0); $y1 = round($max_y / 2, 0); if ($autoresize) { $t = abs($t); $a = round($angle, 0); switch ((int) $angle) { case 0: $width2 = $width; $height2 = $height; break; case 90: $width2 = $height; $height2 = $width; break; case 180: $width2 = $width; $height2 = $height; break; case 270: $width2 = $height; $height2 = $width; break; default: $width2 = (int) abs(sin($t) * $height + cos($t) * $width); $height2 = (int) abs(cos($t) * $height + sin($t) * $width); } $width2 -= $width2 % 2; $height2 -= $height2 % 2; $d_width = abs($width - $width2); $d_height = abs($height - $height2); $x_offset = $d_width / 2; $y_offset = $d_height / 2; $min_x2 = -abs($x_offset); $min_y2 = -abs($y_offset); $max_x2 = $width2; $max_y2 = $height2; } $img2 = @imagecreate($width2, $height2); if (!is_resource($img2)) { return false; /*PEAR::raiseError('Cannot create buffer for the rotataion.', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE);*/ } $this->img_x = $width2; $this->img_y = $height2; imagepalettecopy($img2, $img); $mask = imagecolorresolve($img2, $color_mask[0], $color_mask[1], $color_mask[2]); // use simple lines copy for axes angles switch ((int) $angle) { case 0: imagefill($img2, 0, 0, $mask); for ($y = 0; $y < $max_y; $y++) { for ($x = $min_x; $x < $max_x; $x++) { $c = @imagecolorat($img, $x, $y); imagesetpixel($img2, $x + $x_offset, $y + $y_offset, $c); } } break; case 90: imagefill($img2, 0, 0, $mask); for ($x = $min_x; $x < $max_x; $x++) { for ($y = $min_y; $y < $max_y; $y++) { $c = imagecolorat($img, $x, $y); imagesetpixel($img2, $max_y - $y - 1, $x, $c); } } break; case 180: imagefill($img2, 0, 0, $mask); for ($y = 0; $y < $max_y; $y++) { for ($x = $min_x; $x < $max_x; $x++) { $c = @imagecolorat($img, $x, $y); imagesetpixel($img2, $max_x2 - $x - 1, $max_y2 - $y - 1, $c); } } break; case 270: imagefill($img2, 0, 0, $mask); for ($y = 0; $y < $max_y; $y++) { for ($x = $max_x; $x >= $min_x; $x--) { $c = @imagecolorat($img, $x, $y); imagesetpixel($img2, $y, $max_x - $x - 1, $c); } } break; // simple reverse rotation algo // simple reverse rotation algo default: $i = 0; for ($y = $min_y2; $y < $max_y2; $y++) { // Algebra :) $x2 = round(($min_x2 - $x1) * $cosT + (($y - $y1) * $sinT + $x1), 0); $y2 = round(($y - $y1) * $cosT - ($min_x2 - $x1) * $sinT + $y1, 0); for ($x = $min_x2; $x < $max_x2; $x++) { // Check if we are out of original bounces, if we are // use the default color mask if ($x2 >= 0 && $x2 < $max_x && $y2 >= 0 && $y2 < $max_y) { $c = imagecolorat($img, $x2, $y2); } else { $c = $mask; } imagesetpixel($img2, $x + $x_offset, $y + $y_offset, $c); // round verboten! $x2 += $cosT; $y2 -= $sinT; } } break; } $this->old_image = $this->imageHandle; $this->imageHandle = $img2; return true; }
protected function renderAlphaForPalette(&$dst, $src, $dstx, $dsty, $srcx, $srcy, $srcw, $srch) { $partialtransparent = array(); imagealphablending($src, false); imagesavealpha($src, true); $width = $srcx + $srcw - 1; $height = $srcy + $srch - 1; $x = $srcx; $y = $srcy; while ($y <= $height) { $index = imagecolorat($src, $x, $y); $color = imagecolorsforindex($src, $index); if ($color['alpha'] > 0 && $color['alpha'] < 127) { $partialtransparent[] = array('x' => $x - $srcx, 'y' => $y - $srcy); } if ($x < $width) { $x++; } else { if ($x === $width && $y < $height) { $x = $srcx; $y++; } else { if ($x === $width && $y === $height) { break; } } } } $whiteindex = imagecolorresolve($dst, 255, 255, 255); foreach ($partialtransparent as $value) { $index = imagecolorat($dst, $value['x'] + $dstx, $value['y'] + $dsty); $color = imagecolorsforindex($dst, $index); if ($color['alpha'] === 127) { imagesetpixel($dst, $value['x'] + $dstx, $value['y'] + $dsty, $whiteindex); } } }
function draw_data_table($img, $settings) { // Apply defaults, then extract all settings as variables named 'o_*': extract(array_merge(array('color' => array(0, 0, 0), 'font' => 2, 'position' => array(0, 0), 'width' => 0, 'height' => 0, 'cellpadding' => 4), $settings), EXTR_PREFIX_ALL, 'o'); list($x, $y) = $o_position; // Expand to separate variables // Check for mandatory settings: if (!isset($o_data, $o_headers)) { trigger_error("draw_data_table error: 'headers' and 'data' are required"); return FALSE; // In case error handler returns } // Font and color setup: $char_width = imagefontwidth($o_font); $char_height = imagefontheight($o_font); $color = imagecolorresolve($img, $o_color[0], $o_color[1], $o_color[2]); $pad2 = 2 * $o_cellpadding; // Pad all 4 sides of cells // Calculate the number of rows and columns in the table: $n_rows = count($o_data) + 1; // Add 1 for header row // Count non-skipped columns: $n_cols = 0; foreach ($o_headers as $h) { if (!is_null($h)) { $n_cols++; } } // Number of columns in the data array and in $o_headers: $n_data_cols = count($o_headers); // Default column weights so all columns have equal width. if (empty($o_column_widths)) { $o_column_widths = array_fill(0, $n_cols, 1); } // Default column formats to no formatting: if (empty($o_column_formats)) { $o_column_formats = array_fill(0, $n_cols, ''); } // Default column alignments to auto align: if (empty($o_column_alignments)) { $o_column_alignments = array_fill(0, $n_cols, ''); } // Make sure there are the right number of entries. if (count($o_column_widths) != $n_cols || count($o_column_formats) != $n_cols || count($o_column_alignments) != $n_cols) { trigger_error("draw_data_table error: Mismatch in size of column spec arrays"); return FALSE; // In case error handler returns } // If the table height is not supplied, calculate the space needed. if (empty($o_height)) { $o_height = $n_rows * ($char_height + $pad2); } // Then calculate the height of each row. $row_height = $o_height / $n_rows; // If the table width is not supplied, calculate the space needed based // on the widest value for each column (including header). The column width // factors are ignored, since each column will be as wide as needed. if (empty($o_width)) { $o_width = 0; $col = 0; // Index to unskipped columns for ($i = 0; $i < $n_data_cols; $i++) { // Index to all columns if (is_null($o_headers[$i])) { continue; } // Skip column // Find the longest string in this column, post-formatting. $len = strlen($o_headers[$i]); // Start with the header for ($row = 1; $row < $n_rows; $row++) { if (($cell = $o_data[$row - 1][$i]) !== '') { // Non-empty // Apply cell format if specified: if (($fmt = $o_column_formats[$col]) != '') { $cell = sprintf($fmt, $cell); } if (($this_len = strlen($cell)) > $len) { $len = $this_len; } } } // Assign column width, and accumulate the total: $o_width += $col_width[$col++] = $len * $char_width + $pad2; } } else { // Table width, and optionally column width factors, are supplied. // Calculate the width of each column. $col_width_scale = $o_width / array_sum($o_column_widths); for ($col = 0; $col < $n_cols; $col++) { $col_width[$col] = $o_column_widths[$col] * $col_width_scale; } } // Calculate the column start positions within the table: $col_start[0] = 0; for ($i = 1; $i < $n_cols; $i++) { $col_start[$i] = $col_start[$i - 1] + $col_width[$i - 1]; } // Draw the table grid (without outer border) $x2 = $x + $o_width - 1; for ($row = 1; $row < $n_rows; $row++) { $y0 = $y + $row_height * $row; // Avoid accumulating errors. imageline($img, $x, $y0, $x2, $y0, $color); } $y2 = $y + $o_height - 1; for ($col = 1; $col < $n_cols; $col++) { $x0 = $x + $col_start[$col]; imageline($img, $x0, $y, $x0, $y2, $color); } // Draw the header row, then the data rows for ($row = 0; $row < $n_rows; $row++) { // Vertically center the cell contents within the cell: $y0 = $y + $row_height * ($row + 0.5) - $char_height / 2; if ($row == 0) { $cells = $o_headers; } else { $cells = $o_data[$row - 1]; } // -1 accounts for header row. $col = 0; // Index to unskipped columns for ($i = 0; $i < $n_data_cols; $i++) { // Index to all columns if (is_null($o_headers[$i])) { continue; } // NULL header => skip column if (($cell = $cells[$i]) !== '') { // Empty cell? if ($row == 0) { $alg = 'C'; // Header row forces center alignment } else { // Apply cell format if specified: if (($fmt = $o_column_formats[$col]) != '') { $cell = sprintf($fmt, $cell); } // Get cell alignment: $alg = $o_column_alignments[$col]; } // Calculate upper left position for this cell's text: if (empty($alg)) { // Default alignment: numbers right, else left. $alg = is_numeric($cell) ? 'R' : 'L'; } $x0 = $x + $col_start[$col]; if ($alg == 'R') { $x0 += $col_width[$col] - strlen($cell) * $char_width - $o_cellpadding; } elseif ($alg == 'C') { $x0 += ($col_width[$col] - strlen($cell) * $char_width) / 2; } else { // Default, assume L $x0 += $o_cellpadding; } imagestring($img, $o_font, $x0, $y0, $cell, $color); } $col++; } } return TRUE; }
<?php # $Id: data_table.example1.php 999 2011-08-05 19:00:48Z lbayuk $ # phplot / contrib / data_table example 1: Stand-alone data tables # This example does not use PHPlot. Output is a PNG file with multiple # data tables, with varying parameters. require_once 'data_table.php'; $data = array(array('January', 1, 10, 100000.0), array('February', 2, 20, 10000.0), array('March', 3, 30, 1000.0), array('April', 4, 40, 100.0), array('May', 5, '', 10.0), array('June', 6, 60, 1.0), array('July', 7, 70, 0), array('August', 8, 80, 0.1), array('September', 9, 90, 0.01), array('October', 10, 100, 0.001), array('November', 11, 110, 0.0001), array('December', 12, 120, 1.0E-5)); $img_width = 800; $img_height = 600; $cx = $img_width / 2; $cy = $img_height / 2; $tbl_width = $cx - 20; $tbl_height = $cy - 20; $base_settings = array('headers' => array("Label", 'Y1', 'Y2', 'Y3'), 'column_widths' => array(3, 1, 2, 3), 'column_formats' => array('%s', '%d', '%5.2f', '%6g'), 'width' => $tbl_width, 'height' => $tbl_height, 'data' => $data, 'column_alignments' => array('C', 'R', 'R', 'R')); $img = imagecreate($img_width, $img_height); $white = imagecolorresolve($img, 255, 255, 255); $red = imagecolorresolve($img, 255, 0, 0); imageline($img, 0, $cy, $img_width - 1, $cy, $red); imageline($img, $cx, 0, $cx, $img_height - 1, $red); draw_data_table($img, array_merge($base_settings, array('position' => array(10, 10)))); draw_data_table($img, array_merge($base_settings, array('position' => array($cx + 10, 10), 'cellpadding' => 12, 'column_alignments' => array('R', 'L', 'R', 'R')))); draw_data_table($img, array_merge($base_settings, array('position' => array(10, $cy + 10), 'color' => array(0x66, 0x66, 0xcc)))); draw_data_table($img, array_merge($base_settings, array('position' => array($cx + 10, $cy + 10), 'font' => 4))); imagepng($img);
/** * Create wave effect for GD images * * @param resources $im Image resource * @param integer $wave Wave factor * @return @e resource * * <b>Example Usage:</b> * @code * $imageResource = $this->_showGDImageWave( $captcha_string ); * @endcode */ protected function _showGDImageWave($im, $wave = 10) { $_width = imagesx($im); $_height = imagesy($im); $tmp = imagecreatetruecolor($_width, $_height); $_direction = time() % 2 ? TRUE : FALSE; for ($x = 0; $x < $_width; $x++) { for ($y = 0; $y < $_height; $y++) { $xo = $wave * sin(2 * 3.1415 * $y / 128); $yo = $wave * cos(2 * 3.1415 * $x / 128); $_x = $x - $xo; $_y = $y - $yo; if ($_x > 0 and $_x < $_width and ($_y > 0 and $_y < $_height)) { $index = imagecolorat($im, $_x, $_y); $colors = imagecolorsforindex($im, $index); $color = imagecolorresolve($tmp, $colors['red'], $colors['green'], $colors['blue']); } else { $color = imagecolorresolve($tmp, 255, 255, 255); } imagesetpixel($tmp, $x, $y, $color); } } return $tmp; }
/** * Creates resized GIF image. Returns path of new file if creation * succeded. On error returns FALSE * * @param string $sSrc GIF source * @param string $sTarget new image location * @param int $iWidth new width * @param int $iHeight new height * @param int $iOriginalWidth original width * @param int $iOriginalHeigth original heigth * @param int $iGDVer GD library version * * @return string | false */ function resizeGif($sSrc, $sTarget, $iWidth, $iHeight, $iOriginalWidth, $iOriginalHeigth, $iGDVer) { $aResult = checkSizeAndCopy($sSrc, $sTarget, $iWidth, $iHeight, $iOriginalWidth, $iOriginalHeigth); if (is_array($aResult)) { list($iNewWidth, $iNewHeight) = $aResult; $hDestinationImage = imagecreate($iNewWidth, $iNewHeight); $hSourceImage = imagecreatefromgif($sSrc); $iTransparentColor = imagecolorresolve($hSourceImage, 255, 255, 255); $iFillColor = imagecolorresolve($hDestinationImage, 255, 255, 255); imagefill($hDestinationImage, 0, 0, $iFillColor); imagecolortransparent($hSourceImage, $iTransparentColor); if ($iGDVer == 1) { imagecopyresized($hDestinationImage, $hSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth); } else { imagecopyresampled($hDestinationImage, $hSourceImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeigth); } imagecolortransparent($hDestinationImage, $iFillColor); imagegif($hDestinationImage, $sTarget); imagedestroy($hDestinationImage); imagedestroy($hSourceImage); } return makeReadable($sTarget); }
function imagecopyresamplebicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { $palsize = imagecolorstotal($src_img); for ($i = 0; $i < $palsize; $i++) { $colors = imagecolorsforindex($src_img, $i); imagecolorallocate($dst_img, $colors['red'], $colors['green'], $colors['blue']); } $scaleX = ($src_w - 1) / $dst_w; $scaleY = ($src_h - 1) / $dst_h; $scaleX2 = (int) $scaleX / 2; $scaleY2 = (int) $scaleY / 2; for ($j = $src_y; $j < $dst_h; $j++) { $sY = (int) $j * $scaleY; $y13 = $sY + $scaleY2; for ($i = $src_x; $i < $dst_w; $i++) { $sX = (int) $i * $scaleX; $x34 = $sX + $scaleX2; $color1 = imagecolorsforindex($src_img, imagecolorat($src_img, $sX, $y13)); $color2 = imagecolorsforindex($src_img, imagecolorat($src_img, $sX, $sY)); $color3 = imagecolorsforindex($src_img, imagecolorat($src_img, $x34, $y13)); $color4 = imagecolorsforindex($src_img, imagecolorat($src_img, $x34, $sY)); $red = ($color1['red'] + $color2['red'] + $color3['red'] + $color4['red']) / 4; $green = ($color1['green'] + $color2['green'] + $color3['green'] + $color4['green']) / 4; $blue = ($color1['blue'] + $color2['blue'] + $color3['blue'] + $color4['blue']) / 4; $color = imagecolorresolve($dst_img, $red, $green, $blue); if ($color == -1) { if ($palsize++ < 256) { imagecolorallocate($dst_img, $red, $green, $blue); } $color = imagecolorclosest($dst_img, $red, $green, $blue); } imagesetpixel($dst_img, $i + $dst_x - $src_x, $j + $dst_y - $src_y, $color); } } }
/** * Rotates image by the given angle * Uses a fast rotation algorythm for custom angles or lines copy for multiple of 90 degrees * * @author Pierre-Alain Joye * * @access public * @param int $angle Rotation angle * @param array $options An arra like array('canvasColor' => array(r ,g, b), named color or #rrggbb) * @return bool True on success or False on error */ function rotate($angle, $options = null) { if ($angle % 360 == 0) { return true; } $color_mask = $this->_getColor('canvasColor', $options, array(255, 255, 255)); $mask = imagecolorresolve($this->_hImage, $color_mask[0], $color_mask[1], $color_mask[2]); // Multiply by -1 to change the sign, so the image is rotated clockwise $this->_hImage = imagerotate($this->_hImage, $angle * -1, $mask); if (false === $this->_hImage) { return Jaws_Error::raiseError('Failed transformation: rotate().', __FUNCTION__); } $this->_img_w = imagesx($this->_hImage); $this->_img_h = imagesy($this->_hImage); $new_img = $this->_createImage(); if (!imagecopy($new_img, $this->_hImage, 0, 0, 0, 0, $this->_img_w, $this->_img_h)) { imagedestroy($new_img); return Jaws_Error::raiseError('Failed transformation: rotate().', __FUNCTION__); } imagedestroy($this->_hImage); $this->_hImage = $new_img; return true; }
function Allocate($color) { list($r, $g, $b) = $this->color($color); $index = imagecolorexact($this->img, $r, $g, $b); if ($index == -1) { $index = imagecolorallocate($this->img, $r, $g, $b); if (USE_APPROX_COLORS && $index == -1) { $index = imagecolorresolve($this->img, $r, $g, $b); } } return $index; }
function Allocate($aColor) { list($r, $g, $b) = $this->color($aColor); if ($GLOBALS['gd2'] == true) { return imagecolorresolvealpha($this->img, $r, $g, $b, 0); } else { $index = imagecolorexact($this->img, $r, $g, $b); if ($index == -1) { $index = imagecolorallocate($this->img, $r, $g, $b); if (USE_APPROX_COLORS && $index == -1) { $index = imagecolorresolve($this->img, $r, $g, $b); } } return $index; } }
function Allocate($aColor, $aAlpha = 0.0) { list($r, $g, $b, $a) = $this->color($aColor); // If alpha is specified in the color string then this // takes precedence over the second argument if ($a > 0) { $aAlpha = $a; } if (@$GLOBALS['gd2'] == true) { if ($aAlpha < 0 || $aAlpha > 1) { JpGraphError::Raise('Alpha parameter for color must be between 0.0 and 1.0'); exit(1); } return imagecolorresolvealpha($this->img, $r, $g, $b, round($aAlpha * 127)); } else { $index = imagecolorexact($this->img, $r, $g, $b); if ($index == -1) { $index = imagecolorallocate($this->img, $r, $g, $b); if (USE_APPROX_COLORS && $index == -1) { $index = imagecolorresolve($this->img, $r, $g, $b); } } return $index; } }
usage(); } $color1 = rgb($_SERVER['argv'][1]); $color2 = rgb($_SERVER['argv'][2]); $n_col = (int) $_SERVER['argv'][3]; if ($n_col < 2) { usage(); } # Build a color map from colors[0]=color1 to colors[$n_col-1]=color2. $colors = color_range($color1, $color2, $n_col); # Make a picture: $w = 800; $h = 800; $im = imagecreate($w, $h); $background = imagecolorresolve($im, 0, 0, 0); for ($col = 0; $col < $n_col; $col++) { list($r, $g, $b) = $colors[$col]; $colmap[$col] = imagecolorresolve($im, $r, $g, $b); } $margin = 20; $bar_width = (int) (($w - 2 * $margin) / $n_col); $x1 = $margin; $x2 = $x1 + $bar_width; $y1 = $margin; $y2 = $h - $margin; for ($col = 0; $col < $n_col; $col++) { imagefilledrectangle($im, $x1, $y1, $x2, $y2, $colmap[$col]); $x1 = $x2; $x2 += $bar_width; } imagepng($im);