/**
  * Creates an image for every code character.
  */
 protected function createCharImages()
 {
     $counter = 0;
     for ($i = 0, $j = strlen($this->codeWord); $i < $j; $i++) {
         $char = $this->codeWord[$i];
         $tempImageWidth = $this->fontSize * 2;
         $tempImageHeight = $this->fontSize + 40;
         // create image
         $tempImage = imageCreate($tempImageWidth, $tempImageHeight);
         $tempColor = imageColorAllocate($tempImage, $this->color2['red'], $this->color2['green'], $this->color2['blue']);
         imageColorTransparent($tempImage, $tempColor);
         // set font color
         $fontColor = imageColorAllocate($tempImage, $this->fontColor['red'], $this->fontColor['green'], $this->fontColor['blue']);
         // write text
         imageTtfText($tempImage, $this->fontSize, 0, 10, $this->fontSize + mt_rand(25, 30), $fontColor, $this->fontFace, $char);
         // morph text
         if (CAPTCHA_FONT_MORPH) {
             $tempImageHeight = 120;
             $tempImage2 = imageCreate($tempImageWidth, 120);
             $tempColor = imageColorAllocate($tempImage2, $this->color2['red'], $this->color2['green'], $this->color2['blue']);
             imageColorTransparent($tempImage2, $tempColor);
             $divisor = mt_rand(6, 7);
             $quotient = mt_rand(4, 6);
             $method = mt_rand(0, 1);
             // morph text on x-axis
             if ($method == 0) {
                 for ($y = 1; $y <= $tempImageHeight; $y++) {
                     $posX = sin($y / $divisor) * $quotient;
                     imageCopyMerge($tempImage2, $tempImage, $posX, $y, 0, $y, $tempImageWidth, 1, 100);
                 }
             }
             // morph text on y-axis
             if ($method == 1) {
                 for ($x = 1; $x <= $tempImageWidth; $x++) {
                     $posY = sin($x / $divisor) * $quotient;
                     imageCopyMerge($tempImage2, $tempImage, $x, $posY, $x, 0, 1, $tempImageHeight, 100);
                 }
             }
             $image = $tempImage2;
         } else {
             $image = $tempImage;
         }
         // get text width and height
         $positionX = 0;
         for ($x = $tempImageWidth - 1; $x > 0; $x--) {
             for ($y = $tempImageHeight - 1; $y > 0; $y--) {
                 $color = imageColorAt($image, $x, $y);
                 $colorArray = imageColorsForIndex($image, $color);
                 if ($colorArray['red'] == $this->fontColor['red'] && $colorArray['green'] == $this->fontColor['green'] && $colorArray['blue'] == $this->fontColor['blue']) {
                     $positionX = $x;
                     $x = 0;
                     $y = 0;
                     break;
                 }
             }
         }
         $width = $positionX + 10;
         $height = 100;
         // create final char image
         $this->chars[$counter] = imageCreate($width, $height);
         $color2 = imageColorAllocate($this->chars[$counter], $this->color2['red'], $this->color2['green'], $this->color2['blue']);
         imageColorTransparent($this->chars[$counter], $color2);
         imageCopyMerge($this->chars[$counter], $image, 5, 5, 0, 0, $width, $tempImageHeight, 100);
         $this->charWidth[$counter] = $width;
         // destroy temp images
         imageDestroy($tempImage);
         if (CAPTCHA_FONT_MORPH) {
             imageDestroy($tempImage2);
         }
         $counter++;
     }
 }
	/**
	* Make the image greyscale
	*
	* @param Asido_TMP &$tmp
	* @return boolean
	* @access protected
	*/
	function __grayscale(&$tmp) {

		// the longer path: do it pixel by pixel
		// 
		if (parent::__grayscale(&$tmp)) {
			return true;
			}

		// create 256 color palette
		//
		$palette = array();
		for ($c=0; $c<256; $c++) {
			$palette[$c] = imageColorAllocate($tmp->target, $c, $c, $c);
			}

		// read origonal colors pixel by pixel
		//
		for ($y=0; $y<$tmp->image_height; $y++) {
			for ($x=0; $x<$tmp->image_width; $x++) {

				$rgb = imageColorAt($tmp->target, $x, $y);

				$r = ($rgb >> 16) & 0xFF;
				$g = ($rgb >> 8) & 0xFF;
				$b = $rgb & 0xFF;

				$gs = (($r*0.299)+($g*0.587)+($b*0.114));
				imageSetPixel($tmp->target, $x, $y, $palette[$gs]);
				}
			}

		return true;
		}
Example #3
0
 function DoNoise($image, $G0, $C0)
 {
     $W = imageSX($image);
     $H = imageSY($image);
     for ($i = 0; $i < 768; $i++) {
         $arrLUT[$i] = $i < 512 ? $i < 255 ? 0 : $i - 256 : 255;
     }
     $G1 = $G0 / 2;
     $C1 = $C0 / 2;
     for ($y = 0; $y < $H; $y++) {
         for ($x = 0; $x < $W; $x++) {
             $P = imageColorAt($image, $x, $y);
             $R = $P >> 16 & 0xff;
             $G = $P >> 8 & 0xff;
             $B = $P >> 0 & 0xff;
             $N = rand(0, $G0) - $G1;
             $R += 255 + $N + mt_rand(0, $C0) - $C1;
             $G += 255 + $N + mt_rand(0, $C0) - $C1;
             $B += 255 + $N + mt_rand(0, $C0) - $C1;
             imageSetPixel($image, $x, $y, $arrLUT[$R] << 16 | $arrLUT[$G] << 8 | $arrLUT[$B]);
         }
     }
 }
Example #4
0
/**
 * 获取指定坐标像素颜色
 * @param type $x
 * @param type $y
 * @return type
 */
function getColorAt($img, $x, $y)
{
    $rgb = imageColorAt($img, $x, $y);
    $r = $rgb >> 16 & 0xff;
    $g = $rgb >> 8 & 0xff;
    $b = $rgb & 0xff;
    return array('red' => $r, 'green' => $g, 'blue' => $b);
}
 function GetMaxContrast($x, $y)
 {
     $c1;
     $c2;
     $error = 0;
     $max = 0;
     $c1 = imageColorAt($this->i0, $x - (int) $this->diffspace, $y);
     $c2 = imageColorAt($this->i0, $x + (int) $this->diffspace, $y);
     $error = Cartoonfy::gmerror($c1, $c2);
     if ($error > $max) {
         $max = $error;
     }
     $c1 = imageColorAt($this->i0, $x, $y - (int) $this->diffspace);
     $c2 = imageColorAt($this->i0, $x, $y + (int) $this->diffspace);
     $error = Cartoonfy::gmerror($c1, $c2);
     if ($error > $max) {
         $max = $error;
     }
     $c1 = imageColorAt($this->i0, $x - (int) $this->diffspace, $y - (int) $this->diffspace);
     $c2 = imageColorAt($this->i0, $x + (int) $this->diffspace, $y + (int) $this->diffspace);
     $error = Cartoonfy::gmerror($c1, $c2);
     if ($error > $max) {
         $max = $error;
     }
     $c1 = imageColorAt($this->i0, $x + (int) $this->diffspace, $y - (int) $this->diffspace);
     $c2 = imageColorAt($this->i0, $x - (int) $this->diffspace, $y + (int) $this->diffspace);
     $error = Cartoonfy::gmerror($c1, $c2);
     if ($error > $max) {
         $max = $error;
     }
     return $max;
 }
Example #6
0
    imageString($map, 5, 200, 245, "NO SUCH USER", imagecolorallocate($map, 255, 0, 0));
} else {
    $width = imageFontWidth(5);
    $height = imageFontHeight(5);
    if ($x + (strlen($user) + 1) * $width > 500) {
        $stringx = $x - (strlen($user) + 1) * $width - 12;
    }
    if ($y + $height > 500) {
        $stringy = $y - $height / 2 - 2;
    }
    $magenta = imageColorAllocate($map, 255, 0, 255);
    imageColorTransparent($map, $magenta);
    $brown = imagecolorallocate($map, 102, 51, 0);
    $parchment = imagecolorallocate($map, 255, 255, 204);
    // Avoid drawing a brown dot on a brown area
    $rgb = imageColorAt($map, $x, $y);
    if ($rgb > 0) {
        // $rgb is 0 on our parchment-colored areas
        $temp = $brown;
        $brown = $parchment;
        $parchment = $temp;
    }
    // YOU ARE HERE
    imageFilledEllipse($map, $x, $y, 6, 6, $brown);
    // background for text
    imageFilledRectangle($map, $stringx + 6, $stringy - $height / 2, $stringx + 6 + $width * (strlen($user) + 1), $stringy + $height / 2, $brown);
    // text itself
    imageString($map, 5, $stringx + 7 + $width / 2, $stringy - $height / 2 - 1, $user, $parchment);
}
header("Content-type: image/png");
imagePNG($map);
Example #7
0
 /**
  * Get the RGB values for a pixel in the loaded image.
  *
  * @param int $x Horizontal coordinate
  * @param int $y Vertical coordinate
  * @return array Red, green and blue values, from 0 to 255
  */
 public function colorAt($x, $y)
 {
     if (!$this->resource) {
         throw new \Exception("Image not loaded");
     }
     try {
         $rgb = imageColorAt($this->resource, $x, $y);
     } catch (\Exception $e) {
         throw new \ErrorException($e->getMessage());
     }
     $r = $rgb >> 16 & 0xff;
     $g = $rgb >> 8 & 0xff;
     $b = $rgb & 0xff;
     return [$r, $g, $b];
 }
Example #8
0
    // orange
    if (empty($mobid)) {
        $sql = "SELECT * FROM characters WHERE charId = '{$playerid}'";
        $result = mysql_query($sql, $conn) or die(mysql_error());
        while ($newArray = mysql_fetch_array($result)) {
            $currentx = $newArray['x'];
            $currenty = $newArray['y'];
            createloc();
            $mapposition = imagecreatefrompng("images/map/x.png");
            imageColorTransparent($mapposition, imageColorAt($mapposition, 0, 0));
            imagecopymerge($im, $mapposition, $resultx, $resulty, 0, 0, 9, 9, 100);
        }
    }
    if (empty($playerid)) {
        $sql = "SELECT * FROM spawnlist WHERE npc_templateid = '{$mobid}'";
        $result = mysql_query($sql, $conn) or die(mysql_error());
        while ($newArray = mysql_fetch_array($result)) {
            $currentx = $newArray['locx'];
            $currenty = $newArray['locy'];
            createloc();
            $mapposition = imagecreatefrompng("images/map/x2.png");
            imageColorTransparent($mapposition, imageColorAt($mapposition, 0, 0));
            imagecopymerge($im, $mapposition, $resultx, $resulty, 0, 0, 9, 9, 100);
        }
    }
    header("Content-type: image/gif");
    imagegif($im);
    // outputs image to browser
    imagedestroy($im);
}
dbclose();
Example #9
0
 protected function create_images($text, $hash)
 {
     // Check parameters.
     if (trim($text) === '') {
         return array();
     }
     if (!mb_check_encoding($text, 'UTF-8')) {
         throw new IMGTextException('String is not valid UTF-8');
     }
     $font_filename = $this->font_dir . '/' . $this->font_name . '.' . $this->font_ext;
     if (!file_exists($font_filename)) {
         throw new IMGTextException('Font not found: ' . $font);
     }
     $font_size = (int) $this->font_size;
     if ($font_size <= 0) {
         throw new IMGTextException('Invalid font size: ' . $size);
     }
     if (!preg_match('/^#?(?:[0-9a-f]{3})(?:[0-9a-f]{3})?$/', $this->color)) {
         throw new IMGTextException('Invalid text color: ' . $this->color);
     }
     if ($this->background_color !== false && !preg_match('/^#?(?:[0-9a-f]{3})(?:[0-9a-f]{3})?$/', $this->background_color)) {
         throw new IMGTextException('Invalid background color: ' . $this->background_color);
     }
     if (!is_array($this->padding) || count($this->padding) != 4) {
         throw new IMGTextException('Invalid padding. Please use array with 4 members.');
     }
     if (!is_array($this->shadow_offset) || count($this->shadow_offset) != 2) {
         throw new IMGTextException('Invalid shadow offset. Please use array with 2 members.');
     }
     if ($this->shadow_opacity < 0 || $this->shadow_opacity > 127) {
         throw new IMGTextException('Invalid shadow opacity. Please use a value between 0 (opaque) and 127 (transparent).');
     }
     // Split the text into words.
     $words = preg_split('/\\s+/u', $text);
     // Parse the padding amount.
     $padding_top = intval($this->padding[0]);
     $padding_right = intval($this->padding[1]);
     $padding_bottom = intval($this->padding[2]);
     $padding_left = intval($this->padding[3]);
     // Get size information for each word. We do this first, in order to find out the maximum height.
     $fragments = array();
     $max_height = 0;
     $max_top = 0;
     foreach ($words as $w) {
         $w = trim($w);
         if ($w === '') {
             continue;
         }
         // Get the bounding box size.
         $bounds = imageTTFBBox($font_size, 0, $font_filename, $w);
         // Get more useful information from GD's return values.
         $width = $bounds[2] - $bounds[0];
         $height = $bounds[3] - $bounds[5];
         $left = -$bounds[6] - 1;
         $top = -$bounds[7] - 1;
         // Update the max height/top values if necessary.
         if ($height > $max_height) {
             $max_height = $height;
         }
         if ($top > $max_top) {
             $max_top = $top;
         }
         $fragments[] = array($w, $width, $height, $left, $top);
     }
     // Create images for each word.
     $count = 1;
     $return = array();
     foreach ($fragments as $f) {
         list($w, $width, $height, $left, $top) = $f;
         $img_width = $width + $padding_left + $padding_right;
         $img_height = $this->image_height ? $this->image_height : $max_height + $padding_top + $padding_bottom;
         $text_left = $left + $padding_left;
         $text_top = $max_top + $padding_top;
         // Adjust image size and text location if there's a shadow.
         if ($this->shadow) {
             if ($this->shadow_offset[0] < 0) {
                 $shadow_space_left = $this->shadow_blur - $this->shadow_offset[0];
                 $shadow_space_right = max(0, $this->shadow_blur + abs($this->shadow_offset[0]));
                 $shadow_left = $text_left + $shadow_space_left;
                 $text_left = $text_left + $shadow_space_left - $this->shadow_offset[0];
             } else {
                 $shadow_space_left = max(0, $this->shadow_blur - $this->shadow_offset[0]);
                 $shadow_space_right = $this->shadow_blur + $this->shadow_offset[0];
                 $shadow_left = $text_left + $shadow_space_left + $this->shadow_offset[0];
                 $text_left = $text_left + $shadow_space_left;
             }
             if ($this->shadow_offset[1] < 0) {
                 $shadow_space_top = $this->shadow_blur - $this->shadow_offset[1];
                 $shadow_space_bottom = max(0, $this->shadow_blur + abs($this->shadow_offset[1]));
                 $shadow_top = $text_top + $shadow_space_top;
                 $text_top = $text_top + $shadow_space_top - $this->shadow_offset[1];
             } else {
                 $shadow_space_top = max(0, $this->shadow_blur - $this->shadow_offset[1]);
                 $shadow_space_bottom = $this->shadow_blur + $this->shadow_offset[1];
                 $shadow_top = $text_top + $shadow_space_top + $this->shadow_offset[1];
                 $text_top = $text_top + $shadow_space_top;
             }
             $img_width += $shadow_space_left + $shadow_space_right;
             $img_height += $shadow_space_top + $shadow_space_bottom;
         }
         // Initialize the image and draw the background.
         $img = imageCreateTrueColor($img_width, $img_height);
         if ($this->background_color === false) {
             imageSaveAlpha($img, true);
             imageAlphaBlending($img, false);
             $img_background_color = imageColorAllocateAlpha($img, 255, 255, 255, 127);
             imageFilledRectangle($img, 0, 0, $img_width, $img_height, $img_background_color);
             imageAlphaBlending($img, true);
         } else {
             $img_background_colors = $this->hex2rgb($this->background_color);
             $img_background_color = imageColorAllocate($img, $img_background_colors[0], $img_background_colors[1], $img_background_colors[2]);
             imageFilledRectangle($img, 0, 0, $img_width, $img_height, $img_background_color);
         }
         // Draw the shadow.
         if ($this->shadow) {
             // Blurred shadow on a transparent background needs special treatment because of GD's limitations.
             if ($this->shadow_blur && $this->background_color === false) {
                 // Create a temporary image for the shadow.
                 $temp = imageCreateTrueColor($img_width, $img_height);
                 imageSaveAlpha($temp, true);
                 imageFilledRectangle($temp, 0, 0, $img_width, $img_height, imageColorAllocate($temp, 127, 127, 127));
                 // Draw the shadow text on the temporary image, and blur it.
                 $temp_text_color = imageColorAllocate($temp, $this->shadow_opacity, $this->shadow_opacity, $this->shadow_opacity);
                 imageTTFText($temp, $font_size, 0, $shadow_left, $shadow_top, $temp_text_color, $font_filename, $w);
                 for ($i = 0; $i < $this->shadow_blur; $i++) {
                     imageFilter($temp, IMG_FILTER_GAUSSIAN_BLUR);
                 }
                 // Use the blurred shadow as an alpha mask on the original image.
                 $shadow_colors = $this->hex2rgb($this->shadow_color);
                 for ($x = 0; $x < $img_width; $x++) {
                     for ($y = 0; $y < $img_height; $y++) {
                         $alpha = imageColorAt($temp, $x, $y) & 0xff;
                         imageSetPixel($img, $x, $y, imageColorAllocateAlpha($img, $shadow_colors[0], $shadow_colors[1], $shadow_colors[2], $alpha));
                     }
                 }
                 imageDestroy($temp);
             } else {
                 $shadow_colors = $this->hex2rgb($this->shadow_color);
                 $shadow_color = imageColorAllocateAlpha($img, $shadow_colors[0], $shadow_colors[1], $shadow_colors[2], $this->shadow_opacity);
                 imageTTFText($img, $font_size, 0, $shadow_left, $shadow_top, $shadow_color, $font_filename, $w);
                 for ($i = 0; $i < $this->shadow_blur; $i++) {
                     imageFilter($img, IMG_FILTER_GAUSSIAN_BLUR);
                 }
             }
         }
         // Draw the word.
         $text_colors = $this->hex2rgb($this->color);
         $text_color = imageColorAllocate($img, $text_colors[0], $text_colors[1], $text_colors[2]);
         imageTTFText($img, $font_size, 0, $text_left, $text_top, $text_color, $font_filename, $w);
         // Save to a PNG file.
         $filename = '/imgtext.' . $hash . '.word-' . str_pad($count, 3, '0', STR_PAD_LEFT) . '.png';
         imagePNG($img, $this->cache_local_dir . $filename);
         imageDestroy($img);
         // Add information about this word to the return array.
         $return[] = array('word' => $w, 'path' => $this->cache_url_prefix . $filename);
         $count++;
     }
     // Returns a list of dictionaries, each containing a word and the corresponding image URL.
     return $return;
 }
 function _555()
 {
     $b1 = imageTTFBbox($this->_112, 0, $this->_111, $this->_113);
     $b2 = $b1[4];
     $b3 = abs($b1[5]);
     $im = imageCreateTrueColor($b1[4], abs($b1[5]) + 2);
     imageFill($im, 0, 0, imageColorAllocate($im, 255, 255, 255));
     imagettftext($im, $this->_112, 0, 0, $b3, imageColorAllocate($im, 0, 0, 0), $this->_111, $this->_113);
     $this->_114 = $x = imageSX($im);
     $this->_115 = $y = imageSY($im);
     $c = 0;
     $q = 1;
     $this->_057[0] = 0;
     for ($i = 0; $i < $x; $i++) {
         for ($j = 0; $j < $y; $j++) {
             $p = imageColorsForIndex($im, imageColorAt($im, $i, $j));
             if ($p['red'] < 128 && $p['green'] < 128 && $p['blue'] < 128) {
                 $this->_057[$q] = $i;
                 $this->_057[$q + 1] = $j;
                 $q += 2;
                 $c += 2;
             }
         }
     }
     $this->_057[0] = $c;
     $this->_435($this->_116);
 }
Example #11
0
 /**
  * @param $resource
  * @param $color
  * @return bool
  */
 public function set_part_color(&$resource, $color)
 {
     $replace_color = $this->HEX2RGB($color);
     if (LITE_RECOLOR_FUNCTION) {
         imageFilter($resource, IMG_FILTER_COLORIZE, $replace_color[0] - 255, $replace_color[1] - 255, $replace_color[2] - 255);
     } else {
         $width = imageSX($resource);
         $height = imageSY($resource);
         for ($y = 0; $y < $height; $y++) {
             for ($x = 0; $x < $width; $x++) {
                 $rgb = imageColorsForIndex($resource, imageColorAt($resource, $x, $y));
                 $nr = max(round($rgb['red'] * $replace_color[0] / 255), 0);
                 $ng = max(round($rgb['green'] * $replace_color[1] / 255), 0);
                 $nb = max(round($rgb['blue'] * $replace_color[2] / 255), 0);
                 imageSetPixel($resource, $x, $y, imageColorAllocateAlpha($resource, $nr, $ng, $nb, $rgb['alpha']));
             }
         }
     }
     return true;
 }
Example #12
0
 /**
  * Get pixel colors 
  * @param resource $img
  * @param int $x
  * @param intr $y
  * @return array
  */
 private static function getPixelColor($img, $x, $y)
 {
     return imageColorsForIndex($img, imageColorAt($img, $x, $y));
 }