/** * @param resource $image GD image resource * @return int Returns the index of the specified color+alpha in the palette of the image, * or -1 if the color does not exist in the image's palette. */ public function getIndex($image) { if ($this->hasAlphaChannel()) { return imagecolorexactalpha($image, $this->red, $this->green, $this->blue, $this->alpha); } else { return imagecolorexact($image, $this->red, $this->green, $this->blue); } }
/** * Convert resource $imges with rules from $info * * @param resource $image * @param array $info * @param array $MergedImage * @return resource */ public function transformImage($image, &$info, $MergedImage = null) { $h = imagesy($image); $w = imagesx($image); //$imageTransform = $image; if ($h != round($info['height']) || $w != round($info['width'])) { $imageTransform = imagecreatetruecolor($info['width'], $info['height']); imageAlphaBlending($imageTransform, false); imagesavealpha($imageTransform, true); imagecopyresampled($imageTransform, $image, 0, 0, 0, 0, round($info['width']), round($info['height']), $w, $h); $image = $imageTransform; } //$info['rotation'] = 90; if (!empty($info['rotation'])) { #$col = imagecolorexactalpha ($image, 57, 57, 57, 127); $col = imagecolorexactalpha($image, 255, 255, 255, 127); $imageTransform = imagerotate($image, -$info['rotation'], $col); $ims = imagecreatetruecolor(imagesx($imageTransform), imagesy($imageTransform)); imagecolortransparent($ims, $col); imagefill($ims, 0, 0, $col); imagecopy($ims, $imageTransform, 0, 0, 0, 0, imagesx($imageTransform), imagesy($imageTransform)); $info['x'] -= (imagesx($ims) - imagesx($image)) / 2; $info['y'] -= (imagesy($ims) - imagesy($image)) / 2; $image = $ims; //imagepng($image);die(); } $info['width'] = imagesx($image); $info['height'] = imagesy($image); if (!empty($MergedImage)) { $new_image = array(); if ($info['x'] > 0) { $info['x'] = $info['x'] + $MergedImage['areaOffsetX']; $info['beginX'] = 0; } else { $info['beginX'] = -$info['x']; $info['width'] = $info['width'] + $info['x']; $info['x'] = $MergedImage['areaOffsetX']; } if ($info['y'] > 0) { $info['y'] = $info['y'] + $MergedImage['areaOffsetY']; $info['beginY'] = 0; } else { $info['beginY'] = -$info['y']; $info['height'] = $info['height'] + $info['y']; $info['y'] = $MergedImage['areaOffsetY']; } if ($info['x'] + $info['width'] > $MergedImage['areaSizeX'] + $MergedImage['areaOffsetX']) { $info['width'] = $MergedImage['areaSizeX'] + $MergedImage['areaOffsetX'] - $info['x']; } if ($info['y'] + $info['height'] > $MergedImage['areaSizeY'] + $MergedImage['areaOffsetY']) { $info['height'] = $MergedImage['areaSizeY'] + $MergedImage['areaOffsetY'] - $info['y']; } } // die(); //imagepng($imageTransform);die(); return $image; }
protected function getColorIndex($v) { if (count($v) == 3) { $color = imagecolorexact($this->im, $v[0], $v[1], $v[2]); } elseif (count($v) == 4) { $color = imagecolorexactalpha($this->im, $v[0], $v[1], $v[2], $v[3]); } return $color; }
function genStoneOverlay($rockImg, $overlayImg, $wdt, $hgt) { imagealphablending($rockImg, false); imagesavealpha($rockImg, true); $alph = imagecolorallocatealpha($rockImg, 0, 0, 0, 255); imagefill($rockImg, 0, 0, $alph); $color = imagecolorallocatealpha($rockImg, 0, 0, 0, 127); imagefill($rockImg, 0, 0, $color); for ($x = 0; $x < $wdt; $x++) { for ($y = 0; $y < $hgt; $y++) { $alphaIndex = imagecolorat($overlayImg, $x, $y); $alphaColor = imagecolorsforindex($overlayImg, $alphaIndex); $colorIndex = imagecolorat($rockImg, $x, $y); $color = imagecolorsforindex($rockImg, $colorIndex); $newcolor = imagecolorexactalpha($rockImg, $color['red'], $color['green'], $color['blue'], 255 - $alphaColor['red']); if ($color == -1) { $newcolor = imagecolorallocatealpha($rockImg, $color['red'], $color['green'], $color['blue'], 255 - $alphaColor['red']); } imagesetpixel($rockImg, $x, $y, $newcolor); } } }
/** * Rotate an image the given number of degrees. * * @param Image $image An image object. The $image->resource, $image->info['width'], and $image->info['height'] values will be modified by this call. * @param int $degrees The number of (clockwise) degrees to rotate the image. * @param null $background An hexadecimal integer specifying the background color to use for the uncovered area of the image after the rotation. E.g. 0x000000 for black, 0xff00ff for magenta, and 0xffffff for white. For images that support transparency, this will default to transparent. Otherwise it will be white. * @return bool true or false, based on success. */ public static function rotate(Image $image, $degrees, $background = null) { // PHP installations using non-bundled GD do not have imagerotate. if (!function_exists('imagerotate')) { throw new \Exception("The image {$image->source} could not be rotated because the imagerotate() function is not available in this PHP installation."); return false; } // Convert the hexadecimal background value to a color index value. if (isset($background)) { $rgb = array(); for ($i = 16; $i >= 0; $i -= 8) { $rgb[] = $background >> $i & 0xff; } $background = imagecolorallocatealpha($image->resource, $rgb[0], $rgb[1], $rgb[2], 0); } else { // Set the background color as transparent if $background is NULL. // Get the current transparent color. $background = imagecolortransparent($image->resource); // If no transparent colors, use white. if ($background == 0) { $background = imagecolorallocatealpha($image->resource, 255, 255, 255, 0); } } // Images are assigned a new color palette when rotating, removing any // transparency flags. For GIF images, keep a record of the transparent color. if ($image->getExtension() == 'gif') { $transparent_index = imagecolortransparent($image->resource); if ($transparent_index != 0) { $transparent_gif_color = imagecolorsforindex($image->resource, $transparent_index); } } $image->resource = imagerotate($image->resource, 360 - $degrees, $background); // GIFs need to reassign the transparent color after performing the rotate. if (isset($transparent_gif_color)) { $background = imagecolorexactalpha($image->resource, $transparent_gif_color['red'], $transparent_gif_color['green'], $transparent_gif_color['blue'], $transparent_gif_color['alpha']); imagecolortransparent($image->resource, $background); } return true; }
/** * Returns a greyscale copy of an image * * @param \WideImage\Image $image * @return \WideImage\Image */ public function execute($image) { $palette = !$image->isTrueColor(); $transparent = $image->isTransparent(); if ($palette && $transparent) { $tcrgb = $image->getTransparentColorRGB(); } $new = $image->asTrueColor(); if (!imagefilter($new->getHandle(), IMG_FILTER_NEGATE)) { throw new GDFunctionResultException("imagefilter() returned false"); } if ($palette) { $new = $new->asPalette(); if ($transparent) { $irgb = array('red' => 255 - $tcrgb['red'], 'green' => 255 - $tcrgb['green'], 'blue' => 255 - $tcrgb['blue'], 'alpha' => 127); // needs imagecolorexactalpha instead of imagecolorexact, otherwise doesn't work on some transparent GIF images $new_tci = imagecolorexactalpha($new->getHandle(), $irgb['red'], $irgb['green'], $irgb['blue'], 127); $new->setTransparentColor($new_tci); } } return $new; }
/** * {@inheritdoc} */ protected function execute(array $arguments) { // PHP installations using non-bundled GD do not have imagerotate. if (!function_exists('imagerotate')) { \Drupal::logger('image')->notice('The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', array('%file' => $this->getToolkit()->getImage()->getSource())); return FALSE; } // Convert the hexadecimal background value to a color index value. if (!empty($arguments['background'])) { $rgb = array(); for ($i = 16; $i >= 0; $i -= 8) { $rgb[] = $arguments['background'] >> $i & 0xff; } $arguments['background'] = imagecolorallocatealpha($this->getToolkit()->getResource(), $rgb[0], $rgb[1], $rgb[2], 0); } else { // Get the current transparent color. $arguments['background'] = imagecolortransparent($this->getToolkit()->getResource()); // If no transparent colors, use white. if ($arguments['background'] == 0) { $arguments['background'] = imagecolorallocatealpha($this->getToolkit()->getResource(), 255, 255, 255, 0); } } // Images are assigned a new color palette when rotating, removing any // transparency flags. For GIF images, keep a record of the transparent color. if ($this->getToolkit()->getType() == IMAGETYPE_GIF) { $transparent_index = imagecolortransparent($this->getToolkit()->getResource()); if ($transparent_index != 0) { $transparent_gif_color = imagecolorsforindex($this->getToolkit()->getResource(), $transparent_index); } } $this->getToolkit()->setResource(imagerotate($this->getToolkit()->getResource(), 360 - $arguments['degrees'], $arguments['background'])); // GIFs need to reassign the transparent color after performing the rotate. if (isset($transparent_gif_color)) { $arguments['background'] = imagecolorexactalpha($this->getToolkit()->getResource(), $transparent_gif_color['red'], $transparent_gif_color['green'], $transparent_gif_color['blue'], $transparent_gif_color['alpha']); imagecolortransparent($this->getToolkit()->getResource(), $arguments['background']); } return TRUE; }
<?php $im = imagecreatetruecolor(5, 5); $c = imagecolorexact($im, 255, 0, 255); $c2 = imagecolorexactalpha($im, 255, 0, 255, 100); printf("%X\n", $c); printf("%X\n", $c2); imagedestroy($im); $im = imagecreate(5, 5); $c = imagecolorallocate($im, 255, 0, 255); $c2 = imagecolorallocate($im, 255, 200, 0); $c3 = imagecolorallocatealpha($im, 255, 200, 0, 100); echo imagecolorexact($im, 255, 0, 255) . "\n"; echo imagecolorexact($im, 255, 200, 0) . "\n"; echo imagecolorexactalpha($im, 255, 200, 0, 100) . "\n"; // unallocated index echo imagecolorexact($im, 12, 12, 12) . "\n"; imagedestroy($im);
public function getShadow($color, $alfa = 0) { if (isset($this->colorsrgb[$color])) { $red = $this->colorsrgb[$color][0]; $green = $this->colorsrgb[$color][1]; $blue = $this->colorsrgb[$color][2]; } else { list($red, $green, $blue) = hex2rgb($color); } if ($this->sum > 0) { $red = (int) ($red * 0.6); $green = (int) ($green * 0.6); $blue = (int) ($blue * 0.6); } $RGB = array($red, $green, $blue); if (isset($alfa) && function_exists('imagecolorexactalpha') && function_exists('imagecreatetruecolor') && @imagecreatetruecolor(1, 1)) { return imagecolorexactalpha($this->im, $RGB[0], $RGB[1], $RGB[2], $alfa); } return imagecolorallocate($this->im, $RGB[0], $RGB[1], $RGB[2]); }
/** * rotates an image * @param $degrees clockwise * @return bool */ protected function rotate($degrees) { if (!function_exists('imagerotate')) { $this->setError('imagerotate is not a function'); return false; } $ext = $this->getInfo('ext'); $width = $this->getInfo('width'); $height = $this->getInfo('height'); $bg_color = $this->getConfig('bg_color'); if ($bg_color) { $rgb = array(); for ($i = 16; $i >= 0; $i -= 8) { $rgb[] = $bg_color >> $i & 0xff; } $bg_color = imagecolorallocatealpha($this->_resource, $rgb[0], $rgb[1], $rgb[2], 0); } else { $bg_color = imagecolortransparent($this->_resource); // If no transparent colors, use white. if (0 == $bg_color) { $bg_color = imagecolorallocatealpha($this->_resource, 255, 255, 255, 0); } } if ('gif' == 'ext') { $trans = imagecolortransparent($this->_resource); if ($trans != 0) { $trans_gif = imagecolorsforindex($this->_resource, $trans); } } $this->_resource = imagerotate($this->_resource, 360 - $degrees, $bg_color); if (isset($trans_gif)) { $bg_color = imagecolorexactalpha($this->_resource, $trans_gif['red'], $trans_gif['green'], $trans_gif['blue'], $trans_gif['alpha']); imagecolortransparent($this->_resource, $bg_color); } $this->_info['width'] = imagesx($this->_resource); $this->_info['height'] = imagesy($this->_resource); return true; }
/** * Returns the index of the color that exactly match the given color components * * This method accepts either each component as an integer value, * or an associative array that holds the color's components in keys * 'red', 'green', 'blue', 'alpha'. * * @param mixed $R Red component value or an associative array * @param int $G Green component * @param int $B Blue component * @param int $A Alpha component * @return int The color index */ function getExactColorAlpha($R, $G = null, $B = null, $A = null) { if (is_array($R)) { return imagecolorexactalpha($this->handle, $R['red'], $R['green'], $R['blue'], $R['alpha']); } else { return imagecolorexactalpha($this->handle, $R, $G, $B, $A); } }
function _createImageResource() { if ($newImage = @imagecreatefromstring($this->_headerIconFormat . $this->_imageIconFormat)) { // Vista supports PNG. $this->_headerIconFormat = ""; $this->_imageIconFormat = $this->_headerIconFormat . $this->_imageIconFormat; imagesavealpha($newImage, true); imagealphablending($newImage, false); $this->_imageResource = $newImage; } elseif ($this->_entry["Height"] <= 1024 && $this->_entry["Width"] <= 1024) { $newImage = imagecreatetruecolor($this->_entry["Width"], $this->_entry["Height"]); imagesavealpha($newImage, true); imagealphablending($newImage, false); $readPosition = 0; $palette = array(); if ($this->_header["BitCount"] < 24) { // Read Palette for low bitcounts $colorsInPalette = $this->_header["ColorsUsed"] ? $this->_header["ColorsUsed"] : $this->_entry["ColorCount"]; for ($t = 0; $t < pow(2, $this->_header["BitCount"]); $t++) { $blue = ord($this->_imageIconFormat[$readPosition++]); $green = ord($this->_imageIconFormat[$readPosition++]); $red = ord($this->_imageIconFormat[$readPosition++]); $readPosition++; // Unused "Reserved" value. $existingPaletteEntry = imagecolorexactalpha($newImage, $red, $green, $blue, 0); if ($existingPaletteEntry >= 0) { $palette[] = $existingPaletteEntry; } else { $palette[] = imagecolorallocatealpha($newImage, $red, $green, $blue, 0); } } // XOR for ($y = 0; $y < $this->_entry["Height"]; $y++) { $colors = array(); for ($x = 0; $x < $this->_entry["Width"]; $x++) { if ($this->_header["BitCount"] < 8) { $color = array_shift($colors); if (is_null($color)) { $byte = ord($this->_imageIconFormat[$readPosition++]); $tmp_color = 0; for ($t = 7; $t >= 0; $t--) { $bit_value = pow(2, $t); $bit = floor($byte / $bit_value); $byte = $byte - $bit * $bit_value; $tmp_color += $bit * pow(2, $t % $this->_header["BitCount"]); if ($t % $this->_header["BitCount"] == 0) { array_push($colors, $tmp_color); $tmp_color = 0; } } $color = array_shift($colors); } } else { $color = ord($this->_imageIconFormat[$readPosition++]); } imagesetpixel($newImage, $x, $this->_entry["Height"] - $y - 1, $palette[$color]) or die("can't set pixel"); } // All rows end on the 32 bit if ($readPosition % 4) { $readPosition += 4 - $readPosition % 4; } } } else { // BitCount >= 24, No Palette. // marking position because some icons mark all pixels transparent when using an AND map. $markPosition = $readPosition; $retry = true; $ignoreAlpha = false; while ($retry) { $alphas = array(); $retry = false; for ($y = 0; $y < $this->_entry["Height"] and !$retry; $y++) { for ($x = 0; $x < $this->_entry["Width"] and !$retry; $x++) { $blue = ord($this->_imageIconFormat[$readPosition++]); $green = ord($this->_imageIconFormat[$readPosition++]); $red = ord($this->_imageIconFormat[$readPosition++]); if ($this->_header["BitCount"] < 32) { $alpha = 0; } elseif ($ignoreAlpha) { $alpha = 0; $readPosition++; } else { $alpha = ord($this->_imageIconFormat[$readPosition++]); $alphas[$alpha] = $alpha; $alpha = 127 - round($alpha / 255 * 127); } $paletteEntry = imagecolorexactalpha($newImage, $red, $green, $blue, $alpha); if ($paletteEntry < 0) { $paletteEntry = imagecolorallocatealpha($newImage, $red, $green, $blue, $alpha); } imagesetpixel($newImage, $x, $this->_entry["Height"] - $y - 1, $paletteEntry) or die("can't set pixel"); } if ($readPosition % 4) { $readPosition += 4 - $readPosition % 4; } } if ($this->_header["BitCount"] == 32 && isset($alphas[0]) && count($alphas) == 1) { $retry = true; $readPosition = $markPosition; $ignoreAlpha = true; } } } // AND map if ($this->_header["BitCount"] < 32 || $ignoreAlpha) { // Bitcount == 32, No AND (if using alpha). $palette[-1] = imagecolorallocatealpha($newImage, 0, 0, 0, 127); imagecolortransparent($newImage, $palette[-1]); for ($y = 0; $y < $this->_entry["Height"]; $y++) { $colors = array(); for ($x = 0; $x < $this->_entry["Width"]; $x++) { $color = array_shift($colors); if (is_null($color)) { $byte = ord($this->_imageIconFormat[$readPosition++]); $tmp_color = 0; for ($t = 7; $t >= 0; $t--) { $bit_value = pow(2, $t); $bit = floor($byte / $bit_value); $byte = $byte - $bit * $bit_value; array_push($colors, $bit); } $color = array_shift($colors); } if ($color) { imagesetpixel($newImage, $x, $this->_entry["Height"] - $y - 1, $palette[-1]) or die("can't set pixel"); } } // All rows end on the 32 bit. if ($readPosition % 4) { $readPosition += 4 - $readPosition % 4; } } } if ($this->_header["BitCount"] < 24) { imagetruecolortopalette($newImage, true, pow(2, $this->_header["BitCount"])); } } $this->_imageResource = $newImage; }
/** * Allocates a color * * This function tries to allocate the requested color. If the color * already exists in the imaga it will be reused. * * @param ezcGraphColor $color * @return int Color index */ protected function allocate(ezcGraphColor $color) { $image = $this->getImage(); if ($color->alpha > 0) { $fetched = imagecolorexactalpha($image, $color->red, $color->green, $color->blue, $color->alpha / 2); if ($fetched < 0) { $fetched = imagecolorallocatealpha($image, $color->red, $color->green, $color->blue, $color->alpha / 2); } return $fetched; } else { $fetched = imagecolorexact($image, $color->red, $color->green, $color->blue); if ($fetched < 0) { $fetched = imagecolorallocate($image, $color->red, $color->green, $color->blue); } return $fetched; } }
public static function imagefilledellipseaa(&$im, $CX, $CY, $Width, $Height, $color) { $XRadius = floor($Width / 2); $YRadius = floor($Height / 2); $baseColor = self::color2rgb($color); $TwoASquare = 2 * $XRadius * $XRadius; $TwoBSquare = 2 * $YRadius * $YRadius; $X = $XRadius; $Y = 0; $XChange = $YRadius * $YRadius * (1 - 2 * $XRadius); $YChange = $XRadius * $XRadius; $EllipseError = 0; $StoppingX = $TwoBSquare * $XRadius; $StoppingY = 0; $alpha = 77; $color = imagecolorexactalpha($im, $baseColor[0], $baseColor[1], $baseColor[2], $alpha); while ($StoppingX >= $StoppingY) { // {1st set of points, y' > -1} self::imagefilledellipseaa_Plot4EllipsePoints($im, $CX, $CY, $X, $Y, $color, 0); $Y++; $StoppingY += $TwoASquare; $EllipseError += $YChange; $YChange += $TwoASquare; if (2 * $EllipseError + $XChange > 0) { $X--; $StoppingX -= $TwoBSquare; $EllipseError += $XChange; $XChange += $TwoBSquare; } // decide how much of pixel is filled. $filled = $X - sqrt($XRadius * $XRadius - $XRadius * $XRadius / ($YRadius * $YRadius) * $Y * $Y); $alpha = abs(90 * $filled + 37); imagecolordeallocate($im, $color); $color = imagecolorexactalpha($im, $baseColor[0], $baseColor[1], $baseColor[2], $alpha); } // { 1st point set is done; start the 2nd set of points } $X = 0; $Y = $YRadius; $XChange = $YRadius * $YRadius; $YChange = $XRadius * $XRadius * (1 - 2 * $YRadius); $EllipseError = 0; $StoppingX = 0; $StoppingY = $TwoASquare * $YRadius; $alpha = 77; $color = imagecolorexactalpha($im, $baseColor[0], $baseColor[1], $baseColor[2], $alpha); while ($StoppingX <= $StoppingY) { // {2nd set of points, y' < -1} self::imagefilledellipseaa_Plot4EllipsePoints($im, $CX, $CY, $X, $Y, $color, 1); $X++; $StoppingX += $TwoBSquare; $EllipseError += $XChange; $XChange += $TwoBSquare; if (2 * $EllipseError + $YChange > 0) { $Y--; $StoppingY -= $TwoASquare; $EllipseError += $YChange; $YChange += $TwoASquare; } // decide how much of pixel is filled. $filled = $Y - sqrt($YRadius * $YRadius - $YRadius * $YRadius / ($XRadius * $XRadius) * $X * $X); $alpha = abs(90 * $filled + 37); imagecolordeallocate($im, $color); $color = imagecolorexactalpha($im, $baseColor[0], $baseColor[1], $baseColor[2], $alpha); } }
/** * Rotate an image. * @param string $srcfile The source image. * @param string $dstfile The destination file. * @param float $angle Rotation angle, in degrees. The rotation angle is interpreted as the number of degrees to rotate the image anticlockwise. */ public static function Rotate($srcfile, $dstfile, $angle) { // load data from image file; on fail, return source filename list($data, $size) = self::_GetFileData($srcfile); if (is_null($data)) { return $srcfile; } // get image type $type = $size[2]; // rotate the image and save it switch ($type) { case 1: // this is needed because there's a bug in GD: // when you rotate transparent GIFs by multiples // of 90 degrees, the transparency is lost $index = imagecolortransparent($data); if ($index >= 0) { $color = imagecolorsforindex($data, $index); $index = imagecolorallocate($data, $color['red'], $color['green'], $color['blue']); $rotated = imagerotate($data, $angle, $index); $index = imagecolorexactalpha($rotated, $color['red'], $color['green'], $color['blue'], $color['alpha']); imagecolortransparent($rotated, $index); } else { $rotated = imagerotate($data, $angle, 0); } imagegif($rotated, $dstfile); break; case 3: $rotated = imagerotate($data, $angle, -1); imagealphablending($rotated, true); imagesavealpha($rotated, true); imagepng($rotated, $dstfile); break; default: $rotated = imagerotate($data, $angle, 0); imagejpeg($rotated, $dstfile, 100); break; } // return destination filename return $dstfile; }
private function createTransparentImage($width, $height) { $img = imagecreatetruecolor($width, $height); $white = imagecolorexactalpha($img, 255, 255, 255, 127); imagefill($img, 0, 0, $white); imagealphablending($img, false); $bg = imagecolorallocate($img, 0, 0, 0); imagecolortransparent($img, $bg); imagesavealpha($img, true); return $img; }
/** * Extracts icon resource * @return GD2 resource */ private static function _readIconResource($fp, $idx) { $entry = self::_readIconEntry($fp, $idx); fseek($fp, $entry['ImageOffset']); if ($entry['Reserved'] != 0) { throw new \Exception('Reserved (0) is ' . $entry['Reserved']); } if ($entry['Planes'] > 1) { throw new \Exception('odd planes: ' . $entry['Planes']); } // read icon data $data = fread($fp, $entry['BytesInRes']); //file_put_contents('dump-'.($idx+1).'.raw', $data); if (substr($data, 0, 4) == chr(0x89) . 'PNG') { $im = imagecreatefromstring($data); imagesavealpha($im, true); imagealphablending($im, false); return $im; } // read BITMAPINFOHEADER $header = unpack('VSize/VWidth/VHeight/vPlanes/vBitCount/VCompression/VImageSize/VXpixelsPerM/VYpixelsPerM/VColorsUsed/VColorsImportant', substr($data, 0, 40)); if ($header['Size'] != 40) { print_r($header); throw new \Exception('odd header size: ' . $header['Size']); } if ($header['Planes'] > 1) { throw new \Exception('odd planes: ' . $header['Planes']); } if ($header['Compression']) { throw new \Exception('compression not supported'); } if ($entry['Height'] > 1024 || $entry['Width'] > 1024) { throw new \Exception('xxx too big'); } $im = imagecreatetruecolor($entry['Width'], $entry['Height']); imagesavealpha($im, true); imagealphablending($im, false); $pos = 40; $palette = array(); if ($header['BitCount'] < 24) { // Read Palette for low bitcounts $pal_entries = $entry['ColorCount']; if (!$entry['ColorCount'] && $header['BitCount'] == 8) { $pal_entries = 256; } for ($i = 0; $i < $pal_entries; $i++) { $b = ord($data[$pos++]); $g = ord($data[$pos++]); $r = ord($data[$pos++]); $pos++; // skip empty alpha channel $col = imagecolorexactalpha($im, $r, $g, $b, 0); // echo '0x'.dechex($entry['ImageOffset'] + 40 + $pos-4).': Color '.$i.' '.dechex($r).','.dechex($g).','.dechex($b)."\n"; if ($col >= 0) { $palette[] = $col; } else { $palette[] = imagecolorallocatealpha($im, $r, $g, $b, 0); } } // XorMap (contains the icon's foreground bitmap) Each value is an index into the Palette color map for ($y = 0; $y < $entry['Height']; $y++) { $colors = array(); for ($x = 0; $x < $entry['Width']; $x++) { switch ($header['BitCount']) { case 4: // 8- and 16-color bitmap data is stored as 4 bits per pixel list($hi, $lo) = byte_split($data[$pos++]); imagesetpixel($im, $x, $entry['Height'] - $y - 1, $palette[$hi]); imagesetpixel($im, $x + 1, $entry['Height'] - $y - 1, $palette[$lo]); $x++; // echo '0x'.dechex($pos-1).': XorMap '.$hi.', '.$lo."\n"; break; case 8: if ($entry['ColorCount']) { throw new \Exception('xxx use available palette for 8bit??'); } $byte = ord($data[$pos++]); imagesetpixel($im, $x, $entry['Height'] - $y - 1, $palette[$byte]); // echo '0x'.dechex($pos-1).': XorMap 0x'.dechex($byte)."\n"; break; default: throw new \Exception('unhandled bitcount ' . $header['BitCount']); } } // All rows end on the 32 bit if ($pos % 4) { $pos += 4 - $pos % 4; } } } else { // BitCount >= 24, No Palette // marking position because some icons mark all pixels transparent when using an AND map. $mark_pos = $pos; $use_alpha = false; $alphas = array(); for ($y = 0; $y < $entry['Height']; $y++) { for ($x = 0; $x < $entry['Width']; $x++) { $b = ord($data[$pos++]); $g = ord($data[$pos++]); $r = ord($data[$pos++]); if ($header['BitCount'] < 32) { $alpha = 0; } elseif (!$use_alpha) { $alpha = 0; $pos++; } else { $alpha = ord($data[$pos++]); $alphas[$alpha] = $alpha; $alpha = 127 - round($alpha / 255 * 127); } $col = imagecolorexactalpha($im, $r, $g, $b, $alpha); if ($col < 0) { $col = imagecolorallocatealpha($im, $r, $g, $b, $alpha); } imagesetpixel($im, $x, $entry['Height'] - $y - 1, $col); } if ($pos % 4) { //$pos += 4 - ($pos % 4); throw new \Exception('eh2: ' . dechex($pos)); } } if ($header['BitCount'] == 32 && !empty($alphas) && count($alphas) == 1) { echo "USE ALPHA\n"; $pos = $mark_pos; $use_alpha = true; } } // AndMap (background bit mask) 1-bit-per-pixel mask that is the same size (in pixels) as the XorMap // Bitcount == 32, No AND (if using alpha) ????? if ($header['BitCount'] < 32 && $use_alpha) { $palette[-1] = imagecolorallocatealpha($im, 0, 0, 0, 127); imagecolortransparent($im, $palette[-1]); for ($y = 0; $y < $entry['Height']; $y++) { for ($x = 0; $x < $entry['Width']; $x += 8) { $byte = ord($data[$pos++]); $bits = byte_to_bits($byte); echo '0x' . dechex($pos - 1) . ': AndMap 0x' . dechex($byte) . "\n"; for ($i = 0; $i <= 7; $i++) { $color = array_shift($bits); if ($color) { imagesetpixel($im, $x + $i, $entry['Height'] - $y - 1, $palette[-1]); } } } // All rows end on the 32 bit if ($pos % 4) { $pos += 4 - $pos % 4; } } } if ($header['BitCount'] < 24) { imagetruecolortopalette($im, true, pow(2, $header['BitCount'])); } return $im; }
function createTransparent($x, $y) { $this->resample = false; $temp = $this->copycreatehandle($this->handle, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height); $rgb = imagecolorat($temp, $x, $y); if ($this->output == "png8") { imagecolortransparent($temp, $rgb); } else { imagecolorexactalpha($temp, $rgb["red"], $rgb["green"], $rgb["blue"], 127); } return $this->createUnique($temp); }
function acolor2color($im, $acolor, $alpha = false) { if ($alpha) { return imagecolorexactalpha($im, $acolor[0], $acolor[1], $acolor[2], $acolor[3]); } else { return imagecolorallocate($im, $acolor[0], $acolor[1], $acolor[2]); } }
/** * Allocate a color for an image. * * @param int $red Value of red component (between 0 and 255) * @param int $green Value of green component (between 0 and 255) * @param int $blue Value of blue component (between 0 and 255) * @param int|null $alpha Optional value of alpha component (between 0 and 127). 0 = opaque, 127 = transparent. * * @return Color */ public function allocateColor($red, $green, $blue, $alpha = null) { // Verify parameters before trying to allocate new Color($red, $green, $blue, $alpha); // Reuse same color if its already in index if ($alpha === null) { $index = imagecolorexact($this->resource, $red, $green, $blue); } else { $index = imagecolorexactalpha($this->resource, $red, $green, $blue, $alpha); } if ($index !== -1) { return new Color($red, $green, $blue, $alpha, $index); } // Allocate new color if ($alpha === null) { $index = imagecolorallocate($this->resource, $red, $green, $blue); } else { $index = imagecolorallocatealpha($this->resource, $red, $green, $blue, $alpha); } if ($index === false) { throw new InvalidArgumentException('Failed to create color'); } return new Color($red, $green, $blue, $alpha, $index); }
function WatermarkImage(&$obj, $Params = array()) { $file = $Params['file']; if (!$obj || empty($file) || !file_exists($file) || !is_file($file) || !function_exists("gd_info")) { return false; } $arFile = array("ext" => GetFileExtension($file)); $Params["width"] = intval(@imagesx($obj)); $Params["height"] = intval(@imagesy($obj)); $Params["coefficient"] = floatval($Params["coefficient"]); if (!isset($Params["alpha_level"])) { $Params["alpha_level"] = 100; } $Params["alpha_level"] = intval($Params["alpha_level"]) / 100; $wmWidth = round($Params["width"] * $Params["coefficient"]); $wmHeight = round($Params["height"] * $Params["coefficient"]); $arFileSizeTmp = CFile::GetImageSize($file); if (!in_array($arFileSizeTmp[2], array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_BMP))) { return false; } if ($Params["fill"] == 'resize') { $file_obj_1 = CFile::CreateImage($file, $arFileSizeTmp[2]); $arFile["width"] = intval(imagesx($file_obj_1)); $arFile["height"] = intval(imagesy($file_obj_1)); if ($arFile["width"] > $wmWidth || $arFile["height"] > $wmHeight) { $file_1 = $file . '_new.tmp'; CFile::ResizeImageFile($file, $file_1, array('width' => $wmWidth, 'height' => $wmHeight)); $file_obj = CFile::CreateImage($file_1, $arFileSizeTmp[2]); @imagedestroy($file_obj_1); } else { $file_obj = $file_obj_1; } } else { $file_obj = CFile::CreateImage($file, $arFileSizeTmp[2]); if ($Params["fill"] == 'repeat') { $Params["position"] = array('x' => 'top', 'y' => 'left'); } } if (!$file_obj) { return false; } $arFile["width"] = intval(@imagesx($file_obj)); $arFile["height"] = intval(@imagesy($file_obj)); $wm_pos = array("x" => 2, "y" => 2, "width" => $arFile["width"], "height" => $arFile["height"]); if ($Params["position"]['y'] == 'center') { $wm_pos["y"] = intval(($Params["height"] - $wm_pos["height"]) / 2); } elseif ($Params["position"]['y'] == 'bottom') { $wm_pos["y"] = intval($Params["height"] - $wm_pos["height"]); } if ($Params["position"]['x'] == 'center') { $wm_pos["x"] = intval(($Params["width"] - $wm_pos["width"]) / 2); } elseif ($Params["position"]['x'] == 'right') { $wm_pos["x"] = intval($Params["width"] - $wm_pos["width"]); } if ($wm_pos["y"] < 2) { $wm_pos["y"] = 2; } if ($wm_pos["x"] < 2) { $wm_pos["x"] = 2; } for ($y = 0; $y < $arFile["height"]; $y++) { for ($x = 0; $x < $arFile["width"]; $x++) { $watermark_y = $wm_pos["y"] + $y; while (true) { $watermark_x = $wm_pos["x"] + $x; while (true) { $return_color = NULL; $watermark_alpha = $Params["alpha_level"]; $main_rgb = imagecolorsforindex($obj, imagecolorat($obj, $watermark_x, $watermark_y)); $watermark_rbg = imagecolorsforindex($file_obj, imagecolorat($file_obj, $x, $y)); if ($watermark_rbg['alpha']) { $watermark_alpha = round((127 - $watermark_rbg['alpha']) / 127, 2); $watermark_alpha = $watermark_alpha * $Params["alpha_level"]; } $res = array(); foreach (array('red', 'green', 'blue', 'alpha') as $k) { $res[$k] = round($main_rgb[$k] * (1 - $watermark_alpha) + $watermark_rbg[$k] * $watermark_alpha); } $return_color = imagecolorexactalpha($obj, $res["red"], $res["green"], $res["blue"], $res["alpha"]); if ($return_color == -1) { $return_color = imagecolorallocatealpha($obj, $res["red"], $res["green"], $res["blue"], $res["alpha"]); if ($return_color == -1) { $return_color = imagecolorclosestalpha($obj, $res["red"], $res["green"], $res["blue"], $res["alpha"]); } } imagesetpixel($obj, $watermark_x, $watermark_y, $return_color); $watermark_x += $arFile["width"]; if ($Params["fill"] != 'repeat' || $watermark_x > $Params["width"]) { break; } } $watermark_y += $arFile["height"]; if ($Params["fill"] != 'repeat' || $watermark_y > $Params["height"]) { break; } } } } @imagedestroy($file_obj); return true; }
protected function drawElement(&$data, $from, $to, $minX, $maxX, $minY, $maxY, $drawtype, $max_color, $avg_color, $min_color, $minmax_color, $calc_fnc, $axisside) { if (!isset($data['max'][$from]) || !isset($data['max'][$to])) { return; } $oxy = $this->oxy[$axisside]; $zero = $this->zero[$axisside]; $unit2px = $this->unit2px[$axisside]; $shift_min_from = $shift_min_to = 0; $shift_max_from = $shift_max_to = 0; $shift_avg_from = $shift_avg_to = 0; if (isset($data['shift_min'][$from])) { $shift_min_from = $data['shift_min'][$from]; } if (isset($data['shift_min'][$to])) { $shift_min_to = $data['shift_min'][$to]; } if (isset($data['shift_max'][$from])) { $shift_max_from = $data['shift_max'][$from]; } if (isset($data['shift_max'][$to])) { $shift_max_to = $data['shift_max'][$to]; } if (isset($data['shift_avg'][$from])) { $shift_avg_from = $data['shift_avg'][$from]; } if (isset($data['shift_avg'][$to])) { $shift_avg_to = $data['shift_avg'][$to]; } /**/ $min_from = $data['min'][$from] + $shift_min_from; $min_to = $data['min'][$to] + $shift_min_to; $max_from = $data['max'][$from] + $shift_max_from; $max_to = $data['max'][$to] + $shift_max_to; $avg_from = $data['avg'][$from] + $shift_avg_from; $avg_to = $data['avg'][$to] + $shift_avg_to; $x1 = $from + $this->shiftXleft - 1; $x2 = $to + $this->shiftXleft; $y1min = $zero - ($min_from - $oxy) / $unit2px; $y2min = $zero - ($min_to - $oxy) / $unit2px; $y1max = $zero - ($max_from - $oxy) / $unit2px; $y2max = $zero - ($max_to - $oxy) / $unit2px; $y1avg = $zero - ($avg_from - $oxy) / $unit2px; $y2avg = $zero - ($avg_to - $oxy) / $unit2px; //*/ switch ($calc_fnc) { case CALC_FNC_MAX: $y1 = $y1max; $y2 = $y2max; $shift_from = $shift_max_from; $shift_to = $shift_max_to; break; case CALC_FNC_MIN: $y1 = $y1min; $y2 = $y2min; $shift_from = $shift_min_from; $shift_to = $shift_min_to; break; case CALC_FNC_ALL: // MAX $y1x = $y1max > $this->sizeY + $this->shiftY || $y1max < $this->shiftY; $y2x = $y2max > $this->sizeY + $this->shiftY || $y2max < $this->shiftY; if ($y1x) { $y1max = $y1max > $this->sizeY + $this->shiftY ? $this->sizeY + $this->shiftY : $this->shiftY; } if ($y2x) { $y2max = $y2max > $this->sizeY + $this->shiftY ? $this->sizeY + $this->shiftY : $this->shiftY; } //-- // MIN $y1n = $y1min > $this->sizeY + $this->shiftY || $y1min < $this->shiftY; $y2n = $y2min > $this->sizeY + $this->shiftY || $y2min < $this->shiftY; if ($y1n) { $y1min = $y1min > $this->sizeY + $this->shiftY ? $this->sizeY + $this->shiftY : $this->shiftY; } if ($y2n) { $y2min = $y2min > $this->sizeY + $this->shiftY ? $this->sizeY + $this->shiftY : $this->shiftY; } //-- $a[0] = $x1; $a[1] = $y1max; $a[2] = $x1; $a[3] = $y1min; $a[4] = $x2; $a[5] = $y2min; $a[6] = $x2; $a[7] = $y2max; // don't use break, avg must be drawed in this statement // don't use break, avg must be drawed in this statement case CALC_FNC_AVG: // don't use break, avg must be drawed in this statement // don't use break, avg must be drawed in this statement default: $y1 = $y1avg; $y2 = $y2avg; $shift_from = $shift_avg_from; $shift_to = $shift_avg_to; } $shift_from -= $shift_from != 0 ? $oxy : 0; $shift_to -= $shift_to != 0 ? $oxy : 0; $y1_shift = $zero - $shift_from / $unit2px; $y2_shift = $zero - $shift_to / $unit2px; //*/ if (!$this->limitToBounds($y1, $y2, $this->shiftY, $this->sizeY, $drawtype)) { return true; } if (!$this->limitToBounds($y1_shift, $y2_shift, $this->shiftY, $this->sizeY, $drawtype)) { return true; } // draw main line switch ($drawtype) { case GRAPH_ITEM_DRAWTYPE_BOLD_LINE: if ($calc_fnc == CALC_FNC_ALL) { imagefilledpolygon($this->im, $a, 4, $minmax_color); if (!$y1x || !$y2x) { imageline($this->im, $x1 + 1, $y1max, $x2 + 1, $y2max, $max_color); imageline($this->im, $x1, $y1max, $x2, $y2max, $max_color); } if (!$y1n || !$y2n) { imageline($this->im, $x1 - 1, $y1min, $x2 - 1, $y2min, $min_color); imageline($this->im, $x1, $y1min, $x2, $y2min, $min_color); } } imageline($this->im, $x1, $y1 + 1, $x2, $y2 + 1, $avg_color); imageline($this->im, $x1, $y1, $x2, $y2, $avg_color); break; case GRAPH_ITEM_DRAWTYPE_LINE: if ($calc_fnc == CALC_FNC_ALL) { imagefilledpolygon($this->im, $a, 4, $minmax_color); if (!$y1x || !$y2x) { imageline($this->im, $x1, $y1max, $x2, $y2max, $max_color); } if (!$y1n || !$y2n) { imageline($this->im, $x1, $y1min, $x2, $y2min, $min_color); } } imageline($this->im, $x1, $y1, $x2, $y2, $avg_color); break; case GRAPH_ITEM_DRAWTYPE_FILLED_REGION: //-- $a[0] = $x1; $a[1] = $y1; $a[2] = $x1; $a[3] = $y1_shift; $a[4] = $x2; $a[5] = $y2_shift; $a[6] = $x2; $a[7] = $y2; imagefilledpolygon($this->im, $a, 4, $avg_color); break; case GRAPH_ITEM_DRAWTYPE_DOT: imagefilledrectangle($this->im, $x1 - 1, $y1 - 1, $x1, $y1, $avg_color); break; case GRAPH_ITEM_DRAWTYPE_BOLD_DOT: imagefilledrectangle($this->im, $x2 - 1, $y2 - 1, $x2 + 1, $y2 + 1, $avg_color); break; case GRAPH_ITEM_DRAWTYPE_DASHED_LINE: if (function_exists('imagesetstyle')) { // Use imagesetstyle+imageline instead of bugged imagedashedline $style = array($avg_color, $avg_color, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT); imagesetstyle($this->im, $style); imageline($this->im, $x1, $y1, $x2, $y2, IMG_COLOR_STYLED); } else { imagedashedline($this->im, $x1, $y1, $x2, $y2, $avg_color); } break; case GRAPH_ITEM_DRAWTYPE_GRADIENT_LINE: ImageLine($this->im, $x1, $y1, $x2, $y2, $avg_color); //draw the initial line ImageLine($this->im, $x1, $y1 - 1, $x2, $y2 - 1, $avg_color); $bitmask = 255; $blue = $avg_color & $bitmask; // $blue_diff = 255 - $blue; $bitmask = $bitmask << 8; $green = ($avg_color & $bitmask) >> 8; // $green_diff = 255 - $green; $bitmask = $bitmask << 8; $red = ($avg_color & $bitmask) >> 16; // $red_diff = 255 - $red; $maxAlpha = 110; $startAlpha = 50; $alphaRatio = $maxAlpha / ($this->sizeY - $startAlpha); $diffX = $x1 - $x2; for ($i = 0; $i <= $diffX; $i++) { $Yincr = $diffX > 0 ? abs($y2 - $y1) / $diffX : 0; $gy = $y1 > $y2 ? $y2 + $Yincr * $i : $y2 - $Yincr * $i; $steps = $this->sizeY + $this->shiftY - $gy + 1; for ($j = 0; $j < $steps; $j++) { if ($gy + $j < $this->shiftY + $startAlpha) { $alpha = 0; } else { $alpha = 127 - abs(127 - $alphaRatio * ($gy + $j - $this->shiftY - $startAlpha)); } $color = imagecolorexactalpha($this->im, $red, $green, $blue, $alpha); imagesetpixel($this->im, $x2 + $i, $gy + $j, $color); } } break; } }
function drawArc($cx, $cy, $w, $h, $start, $stop, $color) { $start = deg2rad($start); $stop = deg2rad($stop); $fillColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], 0); $w /= 2; $h /= 2; $cdx = $w * cos(M_PI / 4); $cdy = $h * sin(M_PI / 4); $xstart = $w * cos($start); $ystart = $h * sin($start); $xstop = $w * cos(min(M_PI, $stop)); $ystop = $h * sin(min(M_PI, $stop)); if ($start < M_PI / 2) { $yy = 0; for ($x = 0; $x <= $xstart; $x += 1) { if ($x < $xstop) { $y1 = $x / $xstop * $ystop; } else { $y1 = $h * sqrt(1 - pow($x, 2) / pow($w, 2)); } $y2 = $x / $xstart * $ystart; $d1 = $y1 - floor($y1); $d2 = $y2 - floor($y2); $y1 = floor($y1); $y2 = floor($y2); imageline($this->im, $cx + $x, $cy - $y1, $cx + $x, $cy - $y2, $fillColor); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], 100 - $d1 * 100); imagesetpixel($this->im, $cx + $x, $cy - $y1 - 1, $diffColor); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], $d2 * 100); imagesetpixel($this->im, $cx + $x, $cy - $y2 + 1, $diffColor); for ($yy; $yy <= $y1; $yy += 1) { if ($yy < $ystart) { $x1 = $yy / $ystart * $xstart; } else { $x1 = $w * sqrt(1 - pow($yy, 2) / pow($h, 2)); } $d1 = $x1 - floor($x1); $x1 = floor($x1); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], 100 - $d1 * 100); imagesetpixel($this->im, $cx + $x1 + 1, $cy - $yy, $diffColor); if ($stop < M_PI / 2) { $x2 = $yy / $ystop * $xstop; $d2 = $x2 - floor($x2); $x2 = floor($x2); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], $d2 * 100); imagesetpixel($this->im, $cx + $x2, $cy - $yy, $diffColor); } } } } if ($start < M_PI && $stop > M_PI / 2) { $yy = 0; for ($x = 0; $x >= $xstop; $x -= 1) { if ($x > $xstart) { $y1 = $x / $xstart * $ystart; } else { $y1 = $h * sqrt(1 - pow($x, 2) / pow($w, 2)); } $y2 = $x / $xstop * $ystop; $d1 = $y1 - floor($y1); $d2 = $y2 - floor($y2); $y1 = floor($y1); $y2 = floor($y2); imageline($this->im, $cx + $x, $cy - $y1, $cx + $x, $cy - $y2, $fillColor); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], 100 - $d1 * 100); imagesetpixel($this->im, $cx + $x, $cy - $y1 - 1, $diffColor); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], $d2 * 100); imagesetpixel($this->im, $cx + $x, $cy - $y2 + 1, $diffColor); for ($yy; $yy <= $y1; $yy += 1) { if ($yy < $ystop) { $x1 = -$yy / $ystop * $xstop; } else { $x1 = $w * sqrt(1 - pow($yy, 2) / pow($h, 2)); } $d1 = $x1 - floor($x1); $x1 = floor($x1); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], 100 - $d1 * 100); imagesetpixel($this->im, $cx - $x1 - 1, $cy - $yy, $diffColor); if ($start > M_PI / 2) { $x2 = $yy / $ystart * $xstart; $d2 = $x2 - floor($x2); $x2 = floor($x2); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], $d2 * 100); imagesetpixel($this->im, $cx + $x2, $cy - $yy, $diffColor); } } } } $xstart = $w * cos(max(M_PI, $start)); $ystart = $h * sin(max(M_PI, $start)); $xstop = $w * cos($stop); $ystop = $h * sin($stop); if ($start < 3 * M_PI / 2 && $stop > M_PI) { $yy = 0; for ($x = 0; $x >= $xstart; $x -= 1) { if ($x > $xstop) { $y1 = $x / $xstop * $ystop; } else { $y1 = -$h * sqrt(1 - pow($x, 2) / pow($w, 2)); } $y2 = $x / $xstart * $ystart; $d1 = $y1 - floor($y1); $d2 = $y2 - floor($y2); $y1 = floor($y1); $y2 = floor($y2); imageline($this->im, $cx + $x, $cy - $y1, $cx + $x, $cy - $y2, $fillColor); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], $d1 * 100); imagesetpixel($this->im, $cx + $x, $cy - $y1 + 1, $diffColor); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], 100 - $d2 * 100); imagesetpixel($this->im, $cx + $x, $cy - $y2 - 1, $diffColor); for ($yy; $yy >= $y1; $yy -= 1) { if ($yy > $ystart) { $x1 = -$yy / $ystart * $xstart; } else { $x1 = $w * sqrt(1 - pow($yy, 2) / pow($h, 2)); } $d1 = $x1 - floor($x1); $x1 = floor($x1); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], 100 - $d1 * 100); imagesetpixel($this->im, $cx - $x1 - 1, $cy - $yy, $diffColor); if ($stop < 3 * M_PI / 2) { $x2 = $yy / $ystop * $xstop; $d2 = $x2 - floor($x2); $x2 = floor($x2); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], 100 - $d2 * 100); imagesetpixel($this->im, $cx + $x2 + 1, $cy - $yy, $diffColor); } } } } if ($start < 2 * M_PI && $stop > 3 * M_PI / 2) { $yy = 0; for ($x = 0; $x <= $xstop; $x += 1) { if ($x < $xstart) { $y1 = $x / $xstart * $ystart; } else { $y1 = -$h * sqrt(1 - pow($x, 2) / pow($w, 2)); } $y2 = $x / $xstop * $ystop; $d1 = $y1 - floor($y1); $d2 = $y2 - floor($y2); $y1 = floor($y1); $y2 = floor($y2); imageline($this->im, $cx + $x, $cy - $y1, $cx + $x, $cy - $y2, $fillColor); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], $d1 * 100); imagesetpixel($this->im, $cx + $x, $cy - $y1 + 1, $diffColor); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], 100 - $d2 * 100); imagesetpixel($this->im, $cx + $x, $cy - $y2 - 1, $diffColor); for ($yy; $yy >= $y1; $yy -= 1) { if ($yy > $ystop) { $x1 = $yy / $ystop * $xstop; } else { $x1 = $w * sqrt(1 - pow($yy, 2) / pow($h, 2)); } $d1 = $x1 - floor($x1); $x1 = floor($x1); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], 100 - $d1 * 100); imagesetpixel($this->im, $cx + $x1 + 1, $cy - $yy, $diffColor); if ($start > 3 * M_PI / 2) { $x2 = $yy / $ystart * $xstart; $d2 = $x2 - floor($x2); $x2 = floor($x2); $diffColor = imagecolorexactalpha($this->im, $color[0], $color[1], $color[2], $d2 * 100); imagesetpixel($this->im, $cx + $x2, $cy - $yy, $diffColor); } } } } }
/** * Resolve/Allocate palete index for the given $color. * * @param string $color Allowed syntax: * 'red' * '#f00' * '#ff0000' * 'rgb(255, 0, 0)' * 'rgba(255, 0, 0, 0.5)' * @param $gd (optional) GD resource * * @return int */ protected function colorIndex($color, $gd = null) { if ($gd === null) { $gd = $this->gd; } $color = strtolower($color); $colorNames = array('black' => '000000', 'red' => 'ff0000', 'lime' => '00ff00', 'blue' => '0000ff', 'yellow' => 'ffff00', 'aqua' => '00ffff', 'fuchsia' => 'ff00ff', 'white' => 'ffffff', 'silver' => 'c0c0c0', 'gray' => '808080', 'purple' => '800080', 'maroon' => '800000', 'green' => '008000', 'olive' => '808000', 'navy' => '000080', 'teal' => '008080'); if (isset($colorNames[$color])) { $color = '#' . $colorNames[$color]; } if (preg_match('/^#([0-9abcdef]{6})$/', $color, $match)) { // #ffffff notatie $red = hexdec(substr($match[1], 0, 2)); $green = hexdec(substr($match[1], 2, 2)); $blue = hexdec(substr($match[1], 4, 2)); } elseif (preg_match('/^#([0-9abcdef]{3})$/', $color, $match)) { // #fff notatie? $red = hexdec(substr($match[1], 0, 1)) * 16; $green = hexdec(substr($match[1], 1, 1)) * 16; $blue = hexdec(substr($match[1], 2, 1)) * 16; } elseif (preg_match('/^\\s{0,}rgb\\s{0,}\\(\\s{0,}([0-9]+)\\s{0,},\\s{0,}([0-9]+)\\s{0,},\\s{0,}([0-9]+)\\s{0,}\\)\\s{0,}$/', $color, $match)) { // rgb(255, 255, 255) notation $red = $match[1]; $green = $match[2]; $blue = $match[3]; } elseif (preg_match('/^\\s{0,}rgba\\s{0,}\\(\\s{0,}([0-9]+)\\s{0,},\\s{0,}([0-9]+)\\s{0,},\\s{0,}([0-9]+)\\s{0,},\\s{0,}(0|1|[01]{0,1}\\.[0-9]+)\\s{0,}\\)\\s{0,}$/', $color, $match)) { // rgba(255, 255, 255, 0.5) notation $red = $match[1]; $green = $match[2]; $blue = $match[3]; $alpha = ceil((1 - $match[4]) * 127); $pallete = imagecolorexactalpha($gd, $red, $green, $blue, $alpha); // Resolve color if ($pallete !== -1) { return $pallete; } return imagecolorallocatealpha($gd, $red, $green, $blue, $alpha); // Allocate color } else { notice('Unsupported color notation: "' . $color . '"'); return -1; } $pallete = imagecolorexact($gd, $red, $green, $blue); // Resolve color if ($pallete !== -1) { return $pallete; } return imagecolorallocate($gd, $red, $green, $blue); // Allocate color }
/** * Allocate a color on $im resource. This function prevents * from allocating same colors on the same pallete. Instead * if it finds that the color is already allocated, it only * returns the index to that color. * It supports alpha channel. * * @param resource $im Image resource * @param integer $red Red component * @param integer $green Green component * @param integer $blue Blue component * @param optional integer $alphpa Alpha channel * @return integer Color index */ private function __allocateColor(&$im, $red, $green, $blue, $alpha = 0) { $c = imagecolorexactalpha($im, $red, $green, $blue, $alpha); if ($c >= 0) { return $c; } return imagecolorallocatealpha($im, $red, $green, $blue, $alpha); }
/** * Reads image from a ICO file * * @param string $filename Target ico file to load * @param int $icoColorCount Icon color count (For multiple icons ico file) - 2,16,256, ICO_TRUE_COLOR, ICO_XP_COLOR or ICO_MAX_COLOR * @param int $icoSize Icon width (For multiple icons ico file) or ICO_MAX_SIZE * @param int $alphaBgR Background color R value for alpha-channel images (Default is White) * @param int $alphaBgG Background color G value for alpha-channel images (Default is White) * @param int $alphaBgB Background color B value for alpha-channel images (Default is White) * @return resource Image resource */ function imageCreateFromIco($filename, $icoColorCount = 16, $icoSize = 16, $alphaBgR = 255, $alphaBgG = 255, $alphaBgB = 255) { $Ikona = jpexs_GetIconsInfo($filename); $IconID = -1; $ColMax = -1; $SizeMax = -1; for ($p = 0; $p < count($Ikona); $p++) { $Ikona[$p]["NumberOfColors"] = pow(2, $Ikona[$p]["Info"]["BitsPerPixel"]); } for ($p = 0; $p < count($Ikona); $p++) { if ($ColMax == -1 or $Ikona[$p]["NumberOfColors"] >= $Ikona[$ColMax]["NumberOfColors"]) { if ($icoSize == $Ikona[$p]["Width"] or $icoSize == ICO_MAX_SIZE) { $ColMax = $p; } } if ($SizeMax == -1 or $Ikona[$p]["Width"] >= $Ikona[$SizeMax]["Width"]) { if ($icoColorCount == $Ikona[$p]["NumberOfColors"] or $icoColorCount == ICO_MAX_COLOR) { $SizeMax = $p; } } if ($Ikona[$p]["NumberOfColors"] == $icoColorCount) { if ($Ikona[$p]["Width"] == $icoSize) { $IconID = $p; } } } if ($icoColorCount == ICO_MAX_COLOR) { $IconID = $ColMax; } if ($icoSize == ICO_MAX_SIZE) { $IconID = $SizeMax; } $ColName = $icoColorCount; if ($icoSize == ICO_MAX_SIZE) { $icoSize = "Max"; } if ($ColName == ICO_TRUE_COLOR) { $ColName = "True"; } if ($ColName == ICO_XP_COLOR) { $ColName = "XP"; } if ($ColName == ICO_MAX_COLOR) { $ColName = "Max"; } if ($IconID == -1) { die("Icon with {$ColName} colors and {$icoSize} x {$icoSize} size doesn't exist in this file!"); } jpexs_readIcon($filename, $IconID, $Ikona); $biBitCount = $Ikona[$IconID]["Info"]["BitsPerPixel"]; if ($Ikona[$IconID]["Info"]["BitsPerPixel"] == 0) { $Ikona[$IconID]["Info"]["BitsPerPixel"] = 24; } $biBitCount = $Ikona[$IconID]["Info"]["BitsPerPixel"]; if ($biBitCount == 0) { $biBitCount = 1; } $Ikona[$IconID]["BitCount"] = $Ikona[$IconID]["Info"]["BitsPerPixel"]; if ($Ikona[$IconID]["BitCount"] >= 24) { $img = imagecreatetruecolor($Ikona[$IconID]["Width"], $Ikona[$IconID]["Height"]); if ($Ikona[$IconID]["BitCount"] == 32) { $backcolor = imagecolorallocate($img, $alphaBgR, $alphaBgG, $alphaBgB); imagefilledrectangle($img, 0, 0, $Ikona[$IconID]["Width"] - 1, $Ikona[$IconID]["Height"] - 1, $backcolor); } for ($y = 0; $y < $Ikona[$IconID]["Height"]; $y++) { for ($x = 0; $x < $Ikona[$IconID]["Width"]; $x++) { $R = $Ikona[$IconID]["Data"][$x][$y]["r"]; $G = $Ikona[$IconID]["Data"][$x][$y]["g"]; $B = $Ikona[$IconID]["Data"][$x][$y]["b"]; if ($Ikona[$IconID]["BitCount"] == 32) { $Alpha = 127 - round($Ikona[$IconID]["Data"][$x][$y]["alpha"] * 127 / 255); if ($Ikona[$IconID]["Maska"][$x][$y] == 1) { $Alpha = 127; } $color = imagecolorexactalpha($img, $R, $G, $B, $Alpha); if ($color == -1) { $color = imagecolorallocatealpha($img, $R, $G, $B, $Alpha); } } else { $color = imagecolorexact($img, $R, $G, $B); if ($color == -1) { $color = imagecolorallocate($img, $R, $G, $B); } } imagesetpixel($img, $x, $y, $color); } } } else { $img = imagecreate($Ikona[$IconID]["Width"], $Ikona[$IconID]["Height"]); for ($p = 0; $p < count($Ikona[$IconID]["Paleta"]); $p++) { $Paleta[$p] = imagecolorallocate($img, $Ikona[$IconID]["Paleta"][$p]["r"], $Ikona[$IconID]["Paleta"][$p]["g"], $Ikona[$IconID]["Paleta"][$p]["b"]); } for ($y = 0; $y < $Ikona[$IconID]["Height"]; $y++) { for ($x = 0; $x < $Ikona[$IconID]["Width"]; $x++) { imagesetpixel($img, $x, $y, $Paleta[$Ikona[$IconID]["Data"][$x][$y]]); } } } $IsTransparent = false; for ($y = 0; $y < $Ikona[$IconID]["Height"]; $y++) { for ($x = 0; $x < $Ikona[$IconID]["Width"]; $x++) { if ($Ikona[$IconID]["Maska"][$x][$y] == 1) { $IsTransparent = true; break; } } } if ($Ikona[$IconID]["BitCount"] == 32) { imagealphablending($img, false); if (function_exists("imagesavealpha")) { imagesavealpha($img, true); } } if ($IsTransparent) { if ($Ikona[$IconID]["BitCount"] >= 24 or imagecolorstotal($img) >= 256) { $img2 = imagecreatetruecolor(imagesx($img), imagesy($img)); imagecopy($img2, $img, 0, 0, 0, 0, imagesx($img), imagesy($img)); imagedestroy($img); $img = $img2; imagetruecolortopalette($img, true, 255); } $Pruhledna = imagecolorallocate($img, 0, 0, 0); for ($y = 0; $y < $Ikona[$IconID]["Height"]; $y++) { for ($x = 0; $x < $Ikona[$IconID]["Width"]; $x++) { if ($Ikona[$IconID]["Maska"][$x][$y] == 1) { imagesetpixel($img, $x, $y, $Pruhledna); } } } imagecolortransparent($img, $Pruhledna); } return $img; }
/** * Allocate color for an image. * * @param resource $image * @param string $color a hexadecimal color identifier like "1F2C33" * @param int $alpha */ function get_color($image, $color, $alpha = 0) { $red = hexdec('0x' . substr($color, 0, 2)); $green = hexdec('0x' . substr($color, 2, 2)); $blue = hexdec('0x' . substr($color, 4, 2)); if (function_exists('imagecolorexactalpha') && function_exists('imagecreatetruecolor') && @imagecreatetruecolor(1, 1)) { return imagecolorexactalpha($image, $red, $green, $blue, $alpha); } return imagecolorallocate($image, $red, $green, $blue); }
function imagefilledellipseaa(&$im, $CX, $CY, $Width, $Height, $color) { $XRadius = floor($Width / 2); $YRadius = floor($Height / 2); $baseColor = color2rgb($color); $TwoASquare = 2 * $XRadius * $XRadius; $TwoBSquare = 2 * $YRadius * $YRadius; $X = $XRadius; $Y = 0; $XChange = $YRadius * $YRadius * (1 - 2 * $XRadius); $YChange = $XRadius * $XRadius; $EllipseError = 0; $StoppingX = $TwoBSquare * $XRadius; $StoppingY = 0; $alpha = 77; $color = imagecolorexactalpha($im, $baseColor[0], $baseColor[1], $baseColor[2], $alpha); while ($StoppingX >= $StoppingY) { imagefilledellipseaa_Plot4EllipsePoints($im, $CX, $CY, $X, $Y, $color, 0); $Y++; $StoppingY += $TwoASquare; $EllipseError += $YChange; $YChange += $TwoASquare; if (2 * $EllipseError + $XChange > 0) { $X--; $StoppingX -= $TwoBSquare; $EllipseError += $XChange; $XChange += $TwoBSquare; } $filled = $X - sqrt($XRadius * $XRadius - $XRadius * $XRadius / ($YRadius * $YRadius) * $Y * $Y); $alpha = abs(90 * $filled + 37); imagecolordeallocate($im, $color); $color = imagecolorexactalpha($im, $baseColor[0], $baseColor[1], $baseColor[2], $alpha); } $X = 0; $Y = $YRadius; $XChange = $YRadius * $YRadius; $YChange = $XRadius * $XRadius * (1 - 2 * $YRadius); $EllipseError = 0; $StoppingX = 0; $StoppingY = $TwoASquare * $YRadius; $alpha = 77; $color = imagecolorexactalpha($im, $baseColor[0], $baseColor[1], $baseColor[2], $alpha); while ($StoppingX <= $StoppingY) { imagefilledellipseaa_Plot4EllipsePoints($im, $CX, $CY, $X, $Y, $color, 1); $X++; $StoppingX += $TwoBSquare; $EllipseError += $XChange; $XChange += $TwoBSquare; if (2 * $EllipseError + $YChange > 0) { $Y--; $StoppingY -= $TwoASquare; $EllipseError += $YChange; $YChange += $TwoASquare; } $filled = $Y - sqrt($YRadius * $YRadius - $YRadius * $YRadius / ($XRadius * $XRadius) * $X * $X); $alpha = abs(90 * $filled + 37); imagecolordeallocate($im, $color); $color = imagecolorexactalpha($im, $baseColor[0], $baseColor[1], $baseColor[2], $alpha); } }
/** * {@inheritdoc} */ protected function execute(array $arguments) { // PHP installations using non-bundled GD do not have imagerotate. if (!function_exists('imagerotate')) { $this->logger->notice('The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', array('%file' => $this->getToolkit()->getSource())); return FALSE; } $this->getToolkit()->setResource(imagerotate($this->getToolkit()->getResource(), 360 - $arguments['degrees'], $arguments['background_idx'])); // GIFs need to reassign the transparent color after performing the rotate, // but only do so, if the image already had transparency of its own, or the // rotate added a transparent background. if (!empty($arguments['gif_transparent_color'])) { $transparent_idx = imagecolorexactalpha($this->getToolkit()->getResource(), $arguments['gif_transparent_color']['red'], $arguments['gif_transparent_color']['green'], $arguments['gif_transparent_color']['blue'], $arguments['gif_transparent_color']['alpha']); imagecolortransparent($this->getToolkit()->getResource(), $transparent_idx); } return TRUE; }
/** * @param \DarkLuk42\Color $color * @param bool $forceAllocation * * @return int */ public function getColorIndex(Color $color, $forceAllocation = true) { if ($color->hasAlpha()) { $index = imagecolorexactalpha($this->image, $color->getRed(), $color->getGreen(), $color->getBlue(), intval($color->getAlpha() / 2)); } else { $index = imagecolorexact($this->image, $color->getRed(), $color->getGreen(), $color->getBlue()); } if ($index == -1 && $forceAllocation) { $index = $this->colorAllocate($color); } return $index; }