/** * Get image hash * @param array $image * @return array */ private function imagehex($image) { $size = getimagesize($image['path']); $func = 'imagecreatefrom'.$image['type']; $imageres = $func($image['path']); $zone = imagecreate(20, 20); imagecopyresized($zone, $imageres, 0, 0, 0, 0, 20, 20, $size[0], $size[1]); $colormap = array(); $average = 0; $result = array(); for($x=0; $x < 20; $x++) { for($y=0; $y < 20; $y++) { $color = imagecolorat($zone, $x, $y); $color = imagecolorsforindex($zone, $color); $colormap[$x][$y]= 0.212671 * $color['red'] + 0.715160 * $color['green'] + 0.072169 * $color['blue']; $average += $colormap[$x][$y]; } } $average /= 400; for($x=0; $x < 20; $x++) { for($y=0; $y < 20; $y++) { $result[]=($x < 10 ? $x : chr($x+97)) . ($y < 10 ? $y : chr($y+97)) . ($colormap[$x][$y]==0 ? '0' : round(2*($colormap[$x][$y] > $average ? $colormap[$x][$y]/$average:-1*$average/$colormap[$x][$y]))); } } return $result; }
/** * Retourne la couleur du pixel en bas a gauche de l'image **/ function colorImageBottom($imageName) { // recuperer le type et la taille de l'image // Pb sur certains JPG qui retourne '' list($imgW, $imgH, $imgTyp) = getimagesize($imageName); switch ($imgTyp) { case 1: $im = imagecreatefromgif($imageName); break; case 2: case ' ': $im = imagecreatefromjpeg($imageName); break; case 3: $im = imagecreatefrompng($imageName); break; default: $app = JFactory::getApplication(); $app->enqueueMessage(JTEXT::_('IMGNAME_ERROR') . '[name=' . $imageName . '] [ type=' . $imgTyp . '] [ format= ' . $imgW . 'x' . $imgH, 'error'); var_dump(gd_info()); return ""; } $rgb = imagecolorat($im, 2, $imgH - 2); $hex = sprintf("%06X", $rgb); return $hex; }
/** * Method to apply a filter to an image resource. * * @param array $options An array of options for the filter. * * @return void * * @throws InvalidArgumentException * @throws RuntimeException */ public function execute(array $options = array()) { // Verify that image filter support for PHP is available. if (!function_exists('imagefilter')) { throw new RuntimeException('The imagefilter function for PHP is not available.'); } if (empty($options)) { throw new InvalidArgumentException('No valid amount was given. Expected float.'); } $value = (int) array_shift($options); if ($value == 0) { $value = 128; } $width = imagesx($this->handle); $height = imagesy($this->handle); for ($x = 0; $x < $width; ++$x) { for ($y = 0; $y < $height; ++$y) { $index = imagecolorat($this->handle, $x, $y); $rgb = imagecolorsforindex($this->handle, $index); $r = $rgb['red']; $g = $rgb['green']; $b = $rgb['blue']; $a = $rgb['alpha']; $v = round(($r + $g + $b) / 3) >= $value ? 255 : 0; $color = imagecolorallocatealpha($this->handle, $v, $v, $v, $a); if ($color === false) { $color = imagecolorclosestalpha($this->handle, $v, $v, $v, $a); } imagesetpixel($this->handle, $x, $y, $color); } } }
protected function getAverageColor() { $im = $this->image; //Begin getting average $width = imagesx($im); $height = imagesy($im); $total = $r = $g = $b = $a = 0; for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { //get rgba array at index $index = imagecolorat($im, $x, $y); $rgba = imagecolorsforindex($im, $index); //add total for each color $r += $rgba['red']; $g += $rgba['green']; $b += $rgba['blue']; $a += $rgba['alpha']; $total++; unset($index); unset($rgba); } // end for $y } // end for $x unset($im); $avg = array('red' => round($r / $total), 'green' => round($g / $total), 'blue' => round($b / $total), 'alpha' => round($a / $total)); $rgb = new Rgba($avg['red'], $avg['green'], $avg['blue'], $avg['alpha'], $this->url); unset($r); unset($g); unset($b); unset($a); $this->rgba = $rgb; return $this; }
function GetPartialImage($url) { $W = 150; $H = 130; $F = 80; $STEP = 1.0 / $F; $im = imagecreatefromjpeg($url); $dest = imagecreatetruecolor($W, $H); imagecopy($dest, $im, 0, 0, 35, 40, $W, $H); $a = 1; for( $y = $H - $F; $y < $H; $y++ ) { for ( $x = 0; $x < $W; $x++ ) { $i = imagecolorat($dest, $x, $y); $c = imagecolorsforindex($dest, $i); $c = imagecolorallocate($dest, a($c['red'], $a), a($c['green'], $a), a($c['blue'], $a) ); imagesetpixel($dest, $x, $y, $c); } $a -= $STEP; } header('Content-type: image/png'); imagepng($dest); imagedestroy($dest); imagedestroy($im); }
/** * Applies the sepia filter to an image resource * * @param ImageResource $aResource */ public function applyFilter(ImageResource $aResource) { if ($this->offset === 0) { return; } $resource = $aResource->getResource(); $imagex = imagesx($resource); $imagey = imagesy($resource); for ($x = 0; $x < $imagex; ++$x) { for ($y = 0; $y < $imagey; ++$y) { $distx = rand($this->offset * -1, $this->offset); $disty = rand($this->offset * -1, $this->offset); if ($x + $distx >= $imagex) { continue; } if ($x + $distx < 0) { continue; } if ($y + $disty >= $imagey) { continue; } if ($y + $disty < 0) { continue; } $oldcol = imagecolorat($resource, $x, $y); $newcol = imagecolorat($resource, $x + $distx, $y + $disty); imagesetpixel($resource, $x, $y, $newcol); imagesetpixel($resource, $x + $distx, $y + $disty, $oldcol); } } }
/** * Extracts the colour palette of the set image * * @return array * @throws Exception */ public function extractPalette() { if (is_null($this->image)) { throw new Exception('An image must be set before its palette can be extracted.'); } if (($size = getimagesize($this->image)) === false) { throw new Exception("Unable to get image size data"); } if (($img = imagecreatefromstring(file_get_contents($this->image))) === false) { throw new Exception("Unable to open image file"); } $colors = array(); for ($x = 0; $x < $size[0]; $x += $this->granularity) { for ($y = 0; $y < $size[1]; $y += $this->granularity) { $rgb = imagecolorsforindex($img, imagecolorat($img, $x, $y)); $red = round(round($rgb['red'] / 0x33) * 0x33); $green = round(round($rgb['green'] / 0x33) * 0x33); $blue = round(round($rgb['blue'] / 0x33) * 0x33); $thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue); if (array_key_exists($thisRGB, $colors)) { $colors[$thisRGB]++; } else { $colors[$thisRGB] = 1; } } } arsort($colors); return array_slice(array_keys($colors), 0, $this->totalColors); }
public function getHec() { $res = imagecreatefromjpeg($this->ImagePath); $size = getimagesize($this->ImagePath); $data = array(); for ($i = 0; $i < $size[1]; ++$i) { for ($j = 0; $j < $size[0]; ++$j) { $rgb = imagecolorat($res, $j, $i); $rgbarray = imagecolorsforindex($res, $rgb); // ========================================================= // 任何验证码的数字和字母部分为了和验证码图片背景有所区别 // 都必须对文字和背景图片的RGB进行区分,下面的值是我根据 // 验证码的图片进行区分的,您可以分析您的图片,找到如下规律 // ========================================================= if ($rgbarray['red'] < 125 || $rgbarray['green'] < 125 || $rgbarray['blue'] < 125) { $data[$i][$j] = 1; } else { $data[$i][$j] = 0; } } } // 首列1 for ($j = 0; $j < $size[1]; ++$j) { $data[$j][0] = 0; } $this->DataArray = $data; $this->ImageSize = $size; }
function process_pixels() { $this->min = 255; $this->max = 0; for ($x = 0; $x < $this->resize_width; $x++) { for ($y = 0; $y < $this->resize_height; $y++) { $rgb = imagecolorat($this->resized, $x, $y); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; $this->pixels[$x][$y] = array($r, $g, $b); $grey = round(($r + $g + $b) / 3); if ($grey > $this->max) { $this->max = $grey; } if ($grey < $this->min) { $this->min = $grey; } $this->pixels_processed_grey[$x][$y] = $grey; } } $this->normalize_pixels(); //$this->median_pixels(); $this->further_process(); }
/** * Copies a rectangular portion of the source image to another rectangle in the destination image * * This function calls imagecopyresampled() if it is available and GD version is 2 at least. * Otherwise it reimplements the same behaviour. See the PHP manual page for more info. * * @link http://php.net/manual/en/function.imagecopyresampled.php * @param resource $dst_img the destination GD image resource * @param resource $src_img the source GD image resource * @param int $dst_x vthe X coordinate of the upper left corner in the destination image * @param int $dst_y the Y coordinate of the upper left corner in the destination image * @param int $src_x the X coordinate of the upper left corner in the source image * @param int $src_y the Y coordinate of the upper left corner in the source image * @param int $dst_w the width of the destination rectangle * @param int $dst_h the height of the destination rectangle * @param int $src_w the width of the source rectangle * @param int $src_h the height of the source rectangle * @return bool tru on success, false otherwise */ function imagecopybicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { global $CFG; if (function_exists('imagecopyresampled') and $CFG->gdversion >= 2) { return imagecopyresampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } $totalcolors = imagecolorstotal($src_img); for ($i = 0; $i < $totalcolors; $i++) { if ($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 = $scalex / 2.0; $scaley2 = $scaley / 2.0; for ($j = 0; $j < $dst_h; $j++) { $sy = $j * $scaley; for ($i = 0; $i < $dst_w; $i++) { $sx = $i * $scalex; $c1 = imagecolorsforindex($src_img, imagecolorat($src_img, (int) $sx, (int) $sy + $scaley2)); $c2 = imagecolorsforindex($src_img, imagecolorat($src_img, (int) $sx, (int) $sy)); $c3 = imagecolorsforindex($src_img, imagecolorat($src_img, (int) $sx + $scalex2, (int) $sy + $scaley2)); $c4 = imagecolorsforindex($src_img, imagecolorat($src_img, (int) $sx + $scalex2, (int) $sy)); $red = (int) (($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4); $green = (int) (($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4); $blue = (int) (($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4); $color = imagecolorclosest($dst_img, $red, $green, $blue); imagesetpixel($dst_img, $i + $dst_x, $j + $dst_y, $color); } } }
public function execute() { $this->media->asImage(); $gdimage = $this->media->getImage(); $w = $this->media->getWidth(); $h = $this->media->getHeight(); $src_x = ceil($w); $src_y = ceil($h); $dst_x = $src_x; $dst_y = $src_y; $dst_im = imagecreatetruecolor($dst_x, $dst_y); imagecopyresampled($dst_im, $gdimage, 0, 0, 0, 0, $dst_x, $dst_y, $src_x, $src_y); for ($c = 0; $c < 256; ++$c) { $palette[$c] = imagecolorallocate($dst_im, $c, $c, $c); } for ($y = 0; $y < $src_y; ++$y) { for ($x = 0; $x < $src_x; ++$x) { $rgb = imagecolorat($dst_im, $x, $y); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; $gs = $r * 0.299 + $g * 0.587 + $b * 0.114; imagesetpixel($dst_im, $x, $y, $palette[$gs]); } } $this->media->setImage($dst_im); }
function imagecharx($img, $char, $x0, $y0, $ylist) { global $bk_color, $fg_color; $da = @imagecreate(10, 20) or die("Cannot Initialize new GD image stream"); $background_color = imagecolorallocate($da, $bk_color[0], $bk_color[1], $bk_color[2]); $text_color = imagecolorallocate($da, $fg_color[0], $fg_color[1], $fg_color[2]); $color = imagecolorallocate($img, $fg_color[0], $fg_color[1], $fg_color[2]); $arg = rand(0, 18) / 100.0 * pi(); imagestring($da, 18, 0, 0, $char, $text_color); for ($i = 0; $i < 200; $i++) { $y = @floor($i / 10); $x = $i % 10; $point_color = imagecolorat($da, $x, $y); if ($point_color == $text_color) { for ($j = 0; $j < 12; $j++) { $dx = 0; $dy = 0; $p = 6; for ($s = 0; $s < $p; $s++) { $dx += rand(0, 1000 / $p) / 100; $dy += rand(0, 1000 / $p) / 100; } $xx = $x * 5 + $dx - 25; $yy = $y * 5 + $dy - 50; $x1 = cos($arg) * $xx - sin($arg) * $yy + 25; $y1 = sin($arg) * $xx + cos($arg) * $yy + 50; imagesetpixel($img, $x0 + $x1, $y0 + $y1, $color); } } } imagedestroy($da); }
/** * {@inheritDoc} */ public function hash($resource) { // Resize the image. $resized = imagecreatetruecolor(static::SIZE, static::SIZE); imagecopyresampled($resized, $resource, 0, 0, 0, 0, static::SIZE, static::SIZE, imagesx($resource), imagesy($resource)); // Create an array of greyscale pixel values. $pixels = []; for ($y = 0; $y < static::SIZE; $y++) { for ($x = 0; $x < static::SIZE; $x++) { $rgb = imagecolorsforindex($resized, imagecolorat($resized, $x, $y)); $pixels[] = floor(($rgb['red'] + $rgb['green'] + $rgb['blue']) / 3); } } // Free up memory. imagedestroy($resized); // Get the average pixel value. $average = floor(array_sum($pixels) / count($pixels)); // Each hash bit is set based on whether the current pixels value is above or below the average. $hash = 0; $one = 1; foreach ($pixels as $pixel) { if ($pixel > $average) { $hash |= $one; } $one = $one << 1; } return $hash; }
function execute() { $gdimage =& $this->image->getImage(); $w = $this->image->getWidth(); $h = $this->image->getHeight(); $im = $gdimage; $width = imagesx($im); $height = imagesy($im); $output_image_resource = imagecreatetruecolor($width, $height); // --------------- Flip X if ($this->params['flip'] == "X") { $y = 0; $x = 1; while ($x <= $width) { for ($i = 0; $i < $height; $i++) { imagesetpixel($output_image_resource, $x, $i, imagecolorat($im, $width - $x, $i)); } $x++; } $gdimage = $output_image_resource; } // --------------- Flip Y if ($this->params['flip'] == "Y") { $y = 1; $x = 0; while ($y < $height) { for ($i = 0; $i < $width; $i++) { imagesetpixel($output_image_resource, $i, $y, imagecolorat($im, $i, $height - $y)); } $y++; } $gdimage = $output_image_resource; } }
function check_box($r, $g, $b, $error = 0) { $cwd = dirname(__FILE__); $im2 = imagecreatefromgif($cwd . '/test_gif.gif'); $c = imagecolorsforindex($im2, imagecolorat($im2, 8, 8)); if ($error > 0) { $r_min = $r - $error; $r_max = $r + $error; $g_min = $g - $error; $g_max = $g + $error; $b_min = $b - $error; $b_max = $b + $error; if (($c['red'] >= $r_min || $c['red'] <= $r_max) && ($c['green'] >= $g_min || $c['green'] <= $g_max) && ($c['blue'] >= $b_min || $c['blue'] <= $b_max)) { return true; } else { return false; } } else { if ($c['red'] == $r && $c['green'] == $g && $c['blue'] == $b) { return true; } else { return false; } } }
public function run($file) { $res = $this->open_image($file); if ($res != TRUE) { return FALSE; } $this->image_progressive = isset($this->settings['field_settings']['progressive_jpeg']) === TRUE && $this->settings['field_settings']['progressive_jpeg'] == 'yes' ? TRUE : FALSE; if (function_exists('imagefilter') === TRUE) { @imagefilter($this->EE->channel_images->image, IMG_FILTER_GRAYSCALE); } else { $img_width = imageSX($this->EE->channel_images->image); $img_height = imageSY($this->EE->channel_images->image); // convert to grayscale $palette = array(); for ($c = 0; $c < 256; $c++) { $palette[$c] = imagecolorallocate($this->EE->channel_images->image, $c, $c, $c); } for ($y = 0; $y < $img_height; $y++) { for ($x = 0; $x < $img_width; $x++) { $rgb = imagecolorat($this->EE->channel_images->image, $x, $y); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; $gs = $r * 0.299 + $g * 0.587 + $b * 0.114; imagesetpixel($this->EE->channel_images->image, $x, $y, $palette[$gs]); } } } $this->save_image($file); return TRUE; }
/** * Apply the transform to the sfImage object. * * @param sfImage * @return sfImage */ protected function transform(sfImage $image) { $resource = $image->getAdapter()->getHolder(); $resourcex = imagesx($resource); $resourcey = imagesy($resource); for ($x = 0; $x < $resourcex; ++$x) { for ($y = 0; $y < $resourcey; ++$y) { $distx = rand(-$this->scatter_factor, $this->scatter_factor); $disty = rand(-$this->scatter_factor, $this->scatter_factor); // keep inside the image boundaries if ($x + $distx >= $resourcex) { continue; } if ($x + $distx < 0) { continue; } if ($y + $disty >= $resourcey) { continue; } if ($y + $disty < 0) { continue; } $oldcol = imagecolorat($resource, $x, $y); $newcol = imagecolorat($resource, $x + $distx, $y + $disty); imagesetpixel($resource, $x, $y, $newcol); imagesetpixel($resource, $x + $distx, $y + $disty, $oldcol); } } return $image; }
protected function CopyTextCanvasIntoCaptcha($text3dHeight, $rotationMatrix) { $color = $this->AllocateColorByRgbArray($this->image, $this->_conf['color']); $textCanvasHeight = imagesy($this->textCanvas); $textCanvasWidth = imagesx($this->textCanvas); for ($y = 0; $y < $textCanvasHeight; $y++) { for ($x = 0; $x < $textCanvasWidth; $x++) { $pixel = imagecolorat($this->textCanvas, $x, $y); $pixelColor = ($pixel >> 16 & 0xff) + ($pixel >> 8 & 0xff) + ($pixel & 0xff); // calculate new (stertched) values $newX = ($x / $textCanvasWidth - 0.5) * $this->_conf['width']; $newY = ($y / $textCanvasHeight - 0.5) * $this->_conf['height']; $newZ = $pixelColor * $text3dHeight; $grid[$x][$y] = array($newX, $newY, $newZ); $grid[$x][$y] = self::MultiplyMatrices($grid[$x][$y], $rotationMatrix); // fix position $grid[$x][$y][0] += $this->_conf['width'] / 2; $grid[$x][$y][1] += $this->_conf['height'] / 2; // draw vertical line if ($y > 0) { imageline($this->image, $grid[$x][$y - 1][0], $grid[$x][$y - 1][1], $grid[$x][$y][0], $grid[$x][$y][1], $color); } // draw horizontal lines if ($x > 0) { imageline($this->image, $grid[$x - 1][$y][0], $grid[$x - 1][$y][1], $grid[$x][$y][0], $grid[$x][$y][1], $color); } } } }
/** * Returns the colors of the image in an array, ordered in descending order, where the keys are the colors, and the values are the count of the color. * * @return array */ function Get_Color() { if (isset($this->image)) { $im = $this->image; $imgWidth = imagesx($im); $imgHeight = imagesy($im); for ($y = 0; $y < $imgHeight; $y++) { for ($x = 0; $x < $imgWidth; $x++) { $index = imagecolorat($im, $x, $y); $Colors = imagecolorsforindex($im, $index); $Colors['red'] = intval(($Colors['red'] + 15) / 32) * 32; //ROUND THE COLORS, TO REDUCE THE NUMBER OF COLORS, SO THE WON'T BE ANY NEARLY DUPLICATE COLORS! $Colors['green'] = intval(($Colors['green'] + 15) / 32) * 32; $Colors['blue'] = intval(($Colors['blue'] + 15) / 32) * 32; if ($Colors['red'] >= 256) { $Colors['red'] = 240; } if ($Colors['green'] >= 256) { $Colors['green'] = 240; } if ($Colors['blue'] >= 256) { $Colors['blue'] = 240; } $hexarray[] = substr("0" . dechex($Colors['red']), -2) . substr("0" . dechex($Colors['green']), -2) . substr("0" . dechex($Colors['blue']), -2); } } $hexarray = array_count_values($hexarray); natsort($hexarray); $hexarray = array_reverse($hexarray, true); return $hexarray; } else { die("You must enter a filename! (\$image parameter)"); } }
/** * Applies filter effects to the given image * * @param Image\Image $image The image to filter. * * @return Image\Image The filtered image. * * @throws FilterException if the image filter algorithm fails. */ public function applyFilter(Image\Image $image) { if ($this->level <= 0) { $gd = $image->getCore(); $width = imagesx($gd); $height = imagesy($gd); for ($x = 0; $x < $width; ++$x) { for ($y = 0; $y < $height; ++$y) { $rgba = imagecolorsforindex($gd, imagecolorat($gd, $x, $y)); $r = $rgba['red']; $g = $rgba['green']; $b = $rgba['blue']; $a = $rgba['alpha']; $level = $this->level * -1; $max = max($r, $g, $b); $avg = ($r + $g + $b) / 3; $amt = abs($max - $avg) * 2 / 255 * $level / 100; if ($r !== $max) { $r += ($max - $r) * $amt; } if ($g !== $max) { $g += ($max - $g) * $amt; } if ($b !== $max) { $b += ($max - $b) * $amt; } imagesetpixel($gd, $x, $y, imagecolorallocatealpha($gd, $r, $g, $b, $a)); } } $image->setCore($gd); } else { $image->filter(new SaturateFilter($this->level)); } return $image; }
function execute() { $gdimage =& $this->image->getImage(); $w = $this->image->getWidth(); $h = $this->image->getHeight(); $src_x = ceil($w); $src_y = ceil($h); $dst_x = $src_x; $dst_y = $src_y; // http://php.about.com/od/gdlibrary/ss/grayscale_gd.htm function yiq($r, $g, $b) { return $r * 0.299 + $g * 0.587 + $b * 0.114; } $dst_im = ImageCreateTrueColor($dst_x, $dst_y); ImageCopyResampled($dst_im, $gdimage, 0, 0, 0, 0, $dst_x, $dst_y, $src_x, $src_y); for ($c = 0; $c < 256; $c++) { $palette[$c] = imagecolorallocate($dst_im, $c, $c, $c); } for ($y = 0; $y < $src_y; $y++) { for ($x = 0; $x < $src_x; $x++) { $rgb = imagecolorat($dst_im, $x, $y); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; $gs = yiq($r, $g, $b); imagesetpixel($dst_im, $x, $y, $palette[$gs]); } } $gdimage = $dst_im; }
function imagemergealpha($i) { //create a new image $s = imagecreatetruecolor(imagesx($i[0]), imagesy($i[1])); $back_color = imagecolorallocate($s, 0xa9, 0xb1, 0xd3); //merge all images imagealphablending($s, true); $z = $i; while ($d = each($z)) { imagecopy($s, $d[1], 0, 0, 0, 0, imagesx($d[1]), imagesy($d[1])); } //restore the transparency imagealphablending($s, false); $w = imagesx($s); $h = imagesy($s); for ($x = 0; $x < $w; $x++) { for ($y = 0; $y < $h; $y++) { $c = imagecolorat($s, $x, $y); $c = imagecolorsforindex($s, $c); $z = $i; $t = 0; while ($d = each($z)) { $ta = imagecolorat($d[1], $x, $y); $ta = imagecolorsforindex($d[1], $ta); $t += 127 - $ta['alpha']; } $t = $t > 127 ? 127 : $t; $t = 127 - $t; $c = imagecolorallocatealpha($s, $c['red'], $c['green'], $c['blue'], $t); imagesetpixel($s, $x, $y, $c); } } imagesavealpha($s, true); return $s; }
public function getHashValue($img) { $width = imagesx($img); $height = imagesy($img); $total = 0; $array = array(); for ($y = 0; $y < $height; $y++) { for ($x = 0; $x < $width; $x++) { $gray = imagecolorat($img, $x, $y) >> 8 & 0xff; if (!is_array($array[$y])) { $array[$y] = array(); } $array[$y][$x] = $gray; $total += $gray; } } $average = intval($total / (64 * $this->rate * $this->rate)); $result = ''; for ($y = 0; $y < $height; $y++) { for ($x = 0; $x < $width; $x++) { if ($array[$y][$x] >= $average) { $result .= '1'; } else { $result .= '0'; } } } return $result; }
public function getColorScheme($number_of_colors = null) { $colors = array(); if ($image = @imagecreatefromjpeg($this->_tmp_file)) { $imgage_width = $this->Image->Transform->new_x; $imgage_height = $this->Image->Transform->new_y; $inverted_colors = array(); for ($y = 0; $y < $imgage_height; $y++) { for ($x = 0; $x < $imgage_width; $x++) { $index = imagecolorat($image, $x, $y); $image_colors = imagecolorsforindex($image, $index); $hex = ''; foreach ($image_colors as $color => $value) { $image_colors[$color] = intval(($image_colors[$color] + 15) / 32) * 32; $image_colors[$color] = $image_colors[$color] >= 256 ? 240 : $image_colors[$color]; $hex .= substr('0' . dechex($image_colors[$color]), -2); } $hex = substr($hex, 0, 6); if (strlen($hex) == 6) { $colors[$hex] = empty($colors[$hex]) ? 1 : $colors[$hex] + 1; $this->_addToFrequentColors($hex); if ($this->calculate_negatives && $colors[$hex] > $this->minimum_hits_for_negative) { $negative = $this->_getNegativeAsHex($image_colors['red'], $image_colors['green'], $image_colors['blue']); $colors[$negative] = empty($colors[$negative]) ? 1 : $colors[$negative] + 1; $this->_addToFrequentColors($negative); } } } } } return $this->_getColorsFromCounterColorArray($colors, $number_of_colors); }
protected function createMask(sfImage $image, $w, $h) { // Create a mask png image of the area you want in the circle/ellipse (a 'magicpink' image with a black shape on it, with black set to the colour of alpha transparency) - $mask $mask = $image->getAdapter()->getTransparentImage($w, $h); // Set the masking colours if (false === $this->getColor() || 'image/png' == $image->getMIMEType()) { $mask_black = imagecolorallocate($mask, 0, 0, 0); } else { $mask_black = $image->getAdapter()->getColorByHex($mask, $this->getColor()); } // Cannot use white as transparent mask if color is set to white if ($this->getColor() === '#FFFFFF' || $this->getColor() === false) { $mask_transparent = imagecolorallocate($mask, 255, 0, 0); } else { $mask_color = imagecolorsforindex($mask, imagecolorat($image->getAdapter()->getHolder(), 0, 0)); $mask_transparent = imagecolorallocate($mask, $mask_color['red'], $mask_color['green'], $mask_color['blue']); } imagecolortransparent($mask, $mask_transparent); imagefill($mask, 0, 0, $mask_black); // Draw the rounded rectangle for the mask $this->imagefillroundedrect($mask, 0, 0, $w, $h, $this->getRadius(), $mask_transparent); $mask_image = clone $image; $mask_image->getAdapter()->setHolder($mask); return $mask_image; }
function getAuthImage($text) { $this->setpin($text); $im_x = 160; $im_y = 40; $im = imagecreatetruecolor($im_x, $im_y); $text_c = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); $tmpC0 = mt_rand(100, 255); $tmpC1 = mt_rand(100, 255); $tmpC2 = mt_rand(100, 255); $buttum_c = ImageColorAllocate($im, $tmpC0, $tmpC1, $tmpC2); imagefill($im, 16, 13, $buttum_c); $font = PATH_SYS_PUBLIC . 'font-awesome/fonts/verdana.ttf'; for ($i = 0; $i < strlen($text); $i++) { $tmp = substr($text, $i, 1); $array = array(-1, 1); $p = array_rand($array); $an = $array[$p] * mt_rand(1, 10); //角度 $size = 28; imagettftext($im, $size, $an, 15 + $i * $size, 35, $text_c, $font, $tmp); } $distortion_im = imagecreatetruecolor($im_x, $im_y); imagefill($distortion_im, 16, 13, $buttum_c); for ($i = 0; $i < $im_x; $i++) { for ($j = 0; $j < $im_y; $j++) { $rgb = imagecolorat($im, $i, $j); if ((int) ($i + 20 + sin($j / $im_y * 2 * M_PI) * 10) <= imagesx($distortion_im) && (int) ($i + 20 + sin($j / $im_y * 2 * M_PI) * 10) >= 0) { imagesetpixel($distortion_im, (int) ($i + 10 + sin($j / $im_y * 2 * M_PI - M_PI * 0.1) * 4), $j, $rgb); } } } //加入干扰象素; $count = 160; //干扰像素的数量 for ($i = 0; $i < $count; $i++) { $randcolor = ImageColorallocate($distortion_im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagesetpixel($distortion_im, mt_rand() % $im_x, mt_rand() % $im_y, $randcolor); } $rand = mt_rand(5, 30); $rand1 = mt_rand(15, 25); $rand2 = mt_rand(5, 10); for ($yy = $rand; $yy <= +$rand + 2; $yy++) { for ($px = -80; $px <= 80; $px = $px + 0.1) { $x = $px / $rand1; if ($x != 0) { $y = sin($x); } $py = $y * $rand2; imagesetpixel($distortion_im, $px + 80, $py + $yy, $text_c); } } //设置文件头; Header("Content-type: image/JPEG"); //以PNG格式将图像输出到浏览器或文件; ImagePNG($distortion_im); //销毁一图像,释放与image关联的内存; ImageDestroy($distortion_im); ImageDestroy($im); }
/** * Find pixels coords and draw these on the current image * * @param integer $image Number of the image (to be used with $this->height) * @return boolean Success **/ function drawPixels($image) { $limit = 0; do { /** Select with limit */ $result = mysql_query(sprintf($this->query, $image * $this->height, ($image + 1) * $this->height - 1) . ' LIMIT ' . $limit . ',' . $this->limit); if ($result === false) { return $this->raiseError('Query failed: ' . mysql_error()); } $count = mysql_num_rows($result); while ($click = mysql_fetch_row($result)) { $x = (int) $click[0]; $y = (int) ($click[1] - $image * $this->height); if ($x < 0 || $x >= $this->width) { continue; } /** Apply a calculus for the step, with increases the speed of rendering : step = 3, then pixel is drawn at x = 2 (center of a 3x3 square) */ $x -= $x % $this->step - $this->startStep; $y -= $y % $this->step - $this->startStep; /** Add 1 to the current color of this pixel (color which represents the sum of clicks on this pixel) */ $color = imagecolorat($this->image, $x, $y) + 1; imagesetpixel($this->image, $x, $y, $color); $this->maxClicks = max($this->maxClicks, $color); if ($image === 0) { /** Looking for the maximum height of click */ $this->maxY = max($y, $this->maxY); } } /** Free resultset */ mysql_free_result($result); $limit += $this->limit; } while ($count === $this->limit); return true; }
/** * 二值化 */ function toBinary() { //$this->size = getimagesize($this->filename); $this->size[0] = imagesx($this->img); $this->size[1] = imagesy($this->img); $white = imagecolorallocate($this->img, 255, 255, 255); $black = imagecolorallocate($this->img, 0, 0, 0); //一行一行地扫描 for ($i = 0; $i < $this->size[0]; ++$i) { for ($j = 0; $j < $this->size[1]; ++$j) { $rgb = imagecolorat($this->img, $i, $j); $rgbarray = imagecolorsforindex($this->img, $rgb); // ========================================================= // 任何验证码的数字和字母部分为了和验证码图片背景有所区别 // 都必须对文字和背景图片的RGB进行区分,下面的值是我根据 // 验证码的图片进行区分的,您可以分析您的图片,找到如下规律 // ========================================================= if ($rgbarray['red'] < 125 || $rgbarray['green'] < 125 || $rgbarray['blue'] < 125) { $this->data[$i][$j] = 1; defined('TRAINNING') && TRAINNING && imagesetpixel($this->img, $i, $j, $black); } else { $this->data[$i][$j] = 0; defined('TRAINNING') && TRAINNING && imagesetpixel($this->img, $i, $j, $white); } } } }
function image_to_csv() { $img_path = 'uploads/' . $_FILES['upl']['name']; $file_path = "images/jackie.txt"; list($width, $height, $type, $attr) = getimagesize($img_path); $im = imagecreatefromjpeg($img_path); $file = $file_path; $stack = array(); for ($w = 0; $w < $width; $w++) { for ($h = 0; $h < $height; $h++) { $rgb = imagecolorat($im, $w, $h); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; $bw = round(0.3 * $r + 0.59 * $g + 0.11 * $b); $s .= $bw; if ($h < $height - 1) { $s .= ","; } else { $s .= "\n"; } } } file_put_contents($file, $s); }
public function getDomiColor($url) { \application\resonance2\debug\console::FunctionTrace(__FUNCTION__); $i = imagecreatefromjpeg($url); $rTotal = ''; $bTotal = ''; $gTotal = ''; $total = ''; for ($x = 0; $x < imagesx($i); $x++) { for ($y = 0; $y < imagesy($i); $y++) { $rgb = imagecolorat($i, $x, $y); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; $rTotal += $r; $gTotal += $g; $bTotal += $b; $total++; } } $r = round($rTotal / $total); $g = round($gTotal / $total); $b = round($bTotal / $total); $rgb = array($r, $g, $b); return $rgb; }