function create_histogram($filename) { $img = imagecreatefromfile($filename); $size = getimagesize($filename); $width = $size[0]; $height = $size[1]; $thumbnail_gd_image = imagecreatetruecolor(100, 100); imagecopyresampled($thumbnail_gd_image, $img, 0, 0, 0, 0, 100, 100, $width, $height); $img = $thumbnail_gd_image; $histogram = array(); for ($x = 0; $x < 100; $x++) { for ($y = 0; $y < 100; $y++) { $rgb = imagecolorat($img, $x, $y); $colors = imagecolorsforindex($img, $rgb); $r = str_pad(dechex($colors['red']), 2, "0", STR_PAD_LEFT); $g = str_pad(dechex($colors['green']), 2, "0", STR_PAD_LEFT); $b = str_pad(dechex($colors['blue']), 2, "0", STR_PAD_LEFT); $hex_color = $r . $g . $b . ''; if (!array_key_exists($hex_color, $histogram)) { $histogram[$hex_color] = 0; } $histogram[$hex_color]++; } } return $histogram; }
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; $this->Ageimage = array(1, 0, 60); imagetruecolortopalette($this->EE->channel_images->image, 1, 256); for ($c = 0; $c < 256; $c++) { $col = imagecolorsforindex($this->EE->channel_images->image, $c); $new_col = floor($col['red'] * 0.2125 + $col['green'] * 0.7154 + $col['blue'] * 0.0721); $noise = rand(-$this->Ageimage[1], $this->Ageimage[1]); if ($this->Ageimage[2] > 0) { $r = $new_col + $this->Ageimage[2] + $noise; $g = floor($new_col + $this->Ageimage[2] / 1.86 + $noise); $b = floor($new_col + $this->Ageimage[2] / -3.48 + $noise); } else { $r = $new_col + $noise; $g = $new_col + $noise; $b = $new_col + $noise; } imagecolorset($this->EE->channel_images->image, $c, max(0, min(255, $r)), max(0, min(255, $g)), max(0, min(255, $b))); } $this->save_image($file); return TRUE; }
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; }
/** * 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); }
/** * Wrapper function for 'imagecopyresampled' * * @param Image $image * @param integer $dst_x * @param integer $dst_y * @param integer $src_x * @param integer $src_y * @param integer $dst_w * @param integer $dst_h * @param integer $src_w * @param integer $src_h * @return boolean */ protected function modify($image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { foreach ($image as $frame) { // create new image $modified = imagecreatetruecolor($dst_w, $dst_h); // get current image $resource = $frame->getCore(); // preserve transparency $transIndex = imagecolortransparent($resource); if ($transIndex != -1) { $rgba = imagecolorsforindex($modified, $transIndex); $transColor = imagecolorallocatealpha($modified, $rgba['red'], $rgba['green'], $rgba['blue'], 127); imagefill($modified, 0, 0, $transColor); imagecolortransparent($modified, $transColor); } else { imagealphablending($modified, false); imagesavealpha($modified, true); } // copy content from resource imagecopyresampled($modified, $resource, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); // free memory of old core imagedestroy($resource); // set new content as recource $frame->setCore($modified); } 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 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; } } }
private static function generateArray($image_path) { $image_info = self::getImageInfo($image_path); $func = 'imagecreatefrom' . $image_info['type']; if (function_exists($func)) { $main_img = $func($image_path); $tmp_img = imagecreatetruecolor(self::$matrix, self::$matrix); imagecopyresampled($tmp_img, $main_img, 0, 0, 0, 0, self::$matrix, self::$matrix, $image_info['width'], $image_info['height']); $pixelmap = array(); $average_pixel = 0; for ($x = 0; $x < self::$matrix; $x++) { for ($y = 0; $y < self::$matrix; $y++) { $color = imagecolorat($tmp_img, $x, $y); $color = imagecolorsforindex($tmp_img, $color); $pixelmap[$x][$y] = 0.299 * $color['red'] + 0.587 * $color['green'] + 0.114 * $color['blue']; $average_pixel += $pixelmap[$x][$y]; } } $average_pixel = $average_pixel / (self::$matrix * self::$matrix); imagedestroy($main_img); imagedestroy($tmp_img); for ($x = 0; $x < self::$matrix; $x++) { for ($y = 0; $y < self::$matrix; $y++) { $row = $pixelmap[$x][$y] == 0 ? 0 : round(2 * ($pixelmap[$x][$y] > $average_pixel ? $pixelmap[$x][$y] / $average_pixel : $average_pixel / $pixelmap[$x][$y] * -1)); $row = $x + 10 . ($y + 10) . (255 + $row); $result[] = intval($row); } } return $result; } else { //raise exception throw new Exception('File type not supported!'); } }
/** * 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(); $img = $this->media->getImage(); if (!($t = imagecolorstotal($img))) { $t = 256; imagetruecolortopalette($img, true, $t); } $total = imagecolorstotal($img); for ($i = 0; $i < $total; ++$i) { $index = imagecolorsforindex($img, $i); $red = $index['red'] * 0.393 + $index['green'] * 0.769 + $index['blue'] * 0.189; $green = $index['red'] * 0.349 + $index['green'] * 0.6860000000000001 + $index['blue'] * 0.168; $blue = $index['red'] * 0.272 + $index['green'] * 0.534 + $index['blue'] * 0.131; if ($red > 255) { $red = 255; } if ($green > 255) { $green = 255; } if ($blue > 255) { $blue = 255; } imagecolorset($img, $i, $red, $green, $blue); } $this->media->setImage($img); }
public function colorize() { $image_name = "{$this->_id}.png"; $this->_extension = "png"; $cache = $this->_cache_dir; if (!file_exists($cache . $image_name) or !$this->getResources()) { try { list($width, $height) = getimagesize($this->_path); $img = imagecreatefromstring(file_get_contents($this->_path)); if ($this->_color) { $rgb = $this->_toRgb($this->_color); for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $colors = imagecolorat($img, $x, $y); $current_rgb = imagecolorsforindex($img, $colors); $color = imagecolorallocatealpha($img, $rgb['red'], $rgb['green'], $rgb['blue'], $current_rgb['alpha']); imagesetpixel($img, $x, $y, $color); } } } imagesavealpha($img, true); imagepng($img, $cache . $image_name); $this->_resources = $img; } catch (Exception $e) { throw new $e(); } } return $this; }
public function getHec() { //得到像素大小,RGB值 $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); //计算灰度,并作出计算 $gray = $rgbarray['red']*0.3 + $rgbarray['green'] * 0.59 + $rgbarray['blue'] * 0.11; if($gray < 129) { $data[$i][$j]=1; }else{ $data[$i][$j]=0; } } } $this->DataArray = $data; $this->ImageSize = $size; //去除噪点 $this->filterInfo(); //返回二值化后数据 return $this->DataArray; }
public function build() { $im = imagecreatefrompng($this->image_file); if ($im === false) { throw new Exception($this->image_file . ' is not png file.'); } $is_alpha = false; $width = imagesx($im); $height = imagesy($im); $bitmap_data = array(); for ($y = 0; $y < $height; ++$y) { for ($x = 0; $x < $width; ++$x) { $rgb = imagecolorsforindex($im, imagecolorat($im, $x, $y)); if ($rgb['alpha'] > 0) { $is_alpha = true; } $alpha = (127 - $rgb['alpha']) / 127 * 255; if ($alpha == 255) { $bitmap_data[] = array('Alpha' => 255, 'Red' => $rgb['red'], 'Green' => $rgb['green'], 'Blue' => $rgb['blue']); } else { $bitmap_data[] = array('Alpha' => round($alpha), 'Red' => round($rgb['red'] * $alpha / 255), 'Green' => round($rgb['green'] * $alpha / 255), 'Blue' => round($rgb['blue'] * $alpha / 255)); } } } $bitmap = ''; if ($is_alpha) { foreach ($bitmap_data as $rgb) { if ($rgb['Alpha'] == 0) { $bitmap .= chr(0); $bitmap .= chr(0); $bitmap .= chr(0); $bitmap .= chr(0); } else { $bitmap .= chr($rgb['Alpha']); $bitmap .= chr($rgb['Red']); $bitmap .= chr($rgb['Green']); $bitmap .= chr($rgb['Blue']); } } } else { foreach ($bitmap_data as $rgb) { $bitmap .= chr(0); $bitmap .= chr($rgb['Red']); $bitmap .= chr($rgb['Green']); $bitmap .= chr($rgb['Blue']); } } $format = 5; // palette format $content = chr($format) . pack('v', $width) . pack('v', $height); $content .= gzcompress($bitmap); if ($is_alpha) { $this->code = 36; // DefineBitsLossless2 } else { $this->code = 20; // DefineBitsLossless } $this->content = $content; }
/** * 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. * @param $filePath the path to the local image file * @return an array keyed by hex color value, mapping to the frequency of use in the images */ public static function GenerateFromLocalImage(ImageFile $file) { $pImage = \PMVC\plug('image'); // resize the image for a reasonable amount of colors $previewSize = new ImageSize(150, 150); $srcSize = $file->getSize(); $scale = 1; if ($srcSize->w > 0) { $scale = min($previewSize->w / $srcSize->w, $previewSize->h / $srcSize->h); } if ($scale < 1) { $destSize = new ImageSize(floor($scale * $srcSize->w), floor($scale * $srcSize->h)); } else { $destSize = new ImageSize($srcSize->w, $srcSize->h); } $image_resized = $pImage->create($destSize); $image_orig = $pImage->create($file); //WE NEED NEAREST NEIGHBOR RESIZING, BECAUSE IT DOESN'T ALTER THE COLORS imagecopyresampled($image_resized, $image_orig, 0, 0, 0, 0, $destSize->w, $destSize->h, $srcSize->w, $srcSize->h); // walk the image counting colors $hexarray = []; $pImage->process($destSize, [$image_resized], function ($point, $im) use(&$hexarray) { $index = imagecolorat($im, $point->x, $point->y); $Colors = imagecolorsforindex($im, $index); $hexarray[] = (new BaseColor($Colors['red'], $Colors['green'], $Colors['blue']))->toHex(); }); $hexarray = array_count_values($hexarray); natsort($hexarray); $hexarray = array_slice($hexarray, -10, 10); $hexarray = array_reverse($hexarray, true); return $hexarray; }
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; }
/** * Load actual image pixels from GD resource. * * @param resource $im GD resource to use * @throws Exception Where the image can't be read. */ public function readImageFromGdResource($im) { if (!is_resource($im)) { throw new Exception("Failed to load image."); } elseif (!EscposImage::isGdLoaded()) { throw new Exception(__FUNCTION__ . " requires 'gd' extension."); } /* Make a string of 1's and 0's */ $imgHeight = imagesy($im); $imgWidth = imagesx($im); $imgData = str_repeat("", $imgHeight * $imgWidth); for ($y = 0; $y < $imgHeight; $y++) { for ($x = 0; $x < $imgWidth; $x++) { /* Faster to average channels, blend alpha and negate the image here than via filters (tested!) */ $cols = imagecolorsforindex($im, imagecolorat($im, $x, $y)); // 1 for white, 0 for black, ignoring transparency $greyness = (int) (($cols['red'] + $cols['green'] + $cols['blue']) / 3) >> 7; // 1 for black, 0 for white, taking into account transparency $black = 1 - $greyness >> ($cols['alpha'] >> 6); $imgData[$y * $imgWidth + $x] = $black; } } $this->setImgWidth($imgWidth); $this->setImgHeight($imgHeight); $this->setImgData($imgData); }
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); }
function isItWatter($lat, $lng) { $GMAPStaticUrl = "https://maps.googleapis.com/maps/api/staticmap?center=" . $lat . "," . $lng . "&size=40x40&maptype=roadmap&sensor=false&zoom=12&key=AIzaSyAbYNYyPVWQ78bvZIHHR_djLt-FMEfy2wY"; //echo $GMAPStaticUrl; $chuid = curl_init(); curl_setopt($chuid, CURLOPT_URL, $GMAPStaticUrl); curl_setopt($chuid, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($chuid, CURLOPT_SSL_VERIFYPEER, FALSE); $data = trim(curl_exec($chuid)); curl_close($chuid); $image = imagecreatefromstring($data); ob_start(); imagepng($image); $contents = ob_get_contents(); ob_end_clean(); echo "<img src='data:image/png;base64," . base64_encode($contents) . "' />"; $hexaColor = imagecolorat($image, 0, 0); $color_tran = imagecolorsforindex($image, $hexaColor); $hexaColor2 = imagecolorat($image, 0, 1); $color_tran2 = imagecolorsforindex($image, $hexaColor2); $hexaColor3 = imagecolorat($image, 0, 2); $color_tran3 = imagecolorsforindex($image, $hexaColor3); $red = $color_tran['red'] + $color_tran2['red'] + $color_tran3['red']; $green = $color_tran['green'] + $color_tran2['green'] + $color_tran3['green']; $blue = $color_tran['blue'] + $color_tran2['blue'] + $color_tran3['blue']; imagedestroy($image); //var_dump($red,$green,$blue); //int(492) int(570) int(660) if ($red == 492 && $green == 570 && $blue == 660) { return 1; } else { return 0; } }
/** * Use GD Library Create Image * @author D.C * version 1.0 * @return resource */ private function CreateImage() { $this->image = imagecreatetruecolor($this->width, $this->height); $color1 = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255)); $color2 = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255)); $color1 = imagecolorsforindex($this->image, $color1); $color2 = imagecolorsforindex($this->image, $color2); $steps = $this->width; $r1 = ($color1['red'] - $color2['red']) / $steps; $g1 = ($color1['green'] - $color2['green']) / $steps; $b1 = ($color1['blue'] - $color2['blue']) / $steps; $x1 = 0; $y1 =& $i; $x2 = $this->width; $y2 =& $i; for ($i = 0; $i <= $steps; $i++) { $r2 = $color1['red'] - floor($i * $r1); $g2 = $color1['green'] - floor($i * $g1); $b2 = $color1['blue'] - floor($i * $b1); $color = imagecolorallocate($this->image, $r2, $g2, $b2); imageline($this->image, $x1, $y1, $x2, $y2, $color); } for ($i = 0, $count = mt_rand(10, 20); $i < $count; $i++) { $color = imagecolorallocatealpha($this->image, mt_rand(20, 255), mt_rand(20, 255), mt_rand(100, 255), mt_rand(80, 120)); imageline($this->image, mt_rand(0, $this->width), 0, mt_rand(0, $this->width), $this->height, $color); } return $this->image; }
/** * Generates svg from raster * * @param GDImageIdentifier $img Raster image to convert to svg * @return string SVG xml */ function generateSVG($img) { $w = imagesx($img); // image width $h = imagesy($img); // image height $n = 1; //number of consecutive pixels $svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" shape-rendering=\"crispEdges\">"; for ($x = 0; $x < $w; $x++) { for ($y = 0; $y < $h; $y = $y + $n) { $col = imagecolorat($img, $x, $y); $n = 1; while ($y + $n < $h && $col == imagecolorat($img, $x, $y + $n)) { $n++; } $rgb = imagecolorsforindex($img, $col); $color = "rgb({$rgb['red']},{$rgb['green']},{$rgb['blue']})"; if ($rgb["alpha"] && $rgb["alpha"] < 128) { $alpha = (128 - $rgb["alpha"]) / 128; $color .= "\" fill-opacity=\"{$alpha}"; } $svg .= "<rect width=\"1\" x=\"{$x}\" height=\"{$n}\" y=\"{$y}\" fill=\"{$color}\"/>\n"; } } $svg .= '</svg>'; return $svg; }
/** * 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)"); } }
/** * 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; }
/** * 创建图像 * * @return resource */ protected function _createImage() { if (!is_resource($this->_image)) { extract($this->_options); //解出参数到变量 $this->_image = imagecreatetruecolor($width, $height); $color1 = imagecolorallocate($this->_image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255)); $color2 = imagecolorallocate($this->_image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255)); $color1 = imagecolorsforindex($this->_image, $color1); $color2 = imagecolorsforindex($this->_image, $color2); $steps = $width; $r1 = ($color1['red'] - $color2['red']) / $steps; $g1 = ($color1['green'] - $color2['green']) / $steps; $b1 = ($color1['blue'] - $color2['blue']) / $steps; $x1 = 0; $y1 =& $i; $x2 = $width; $y2 =& $i; for ($i = 0; $i <= $steps; $i++) { $r2 = $color1['red'] - floor($i * $r1); $g2 = $color1['green'] - floor($i * $g1); $b2 = $color1['blue'] - floor($i * $b1); $color = imagecolorallocate($this->_image, $r2, $g2, $b2); imageline($this->_image, $x1, $y1, $x2, $y2, $color); } for ($i = 0, $count = mt_rand(10, 20); $i < $count; $i++) { $color = imagecolorallocatealpha($this->_image, mt_rand(20, 255), mt_rand(20, 255), mt_rand(100, 255), mt_rand(80, 120)); imageline($this->_image, mt_rand(0, $width), 0, mt_rand(0, $width), $height, $color); } } return $this->_image; }
/** * {@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; }
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; }
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 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 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; }
/** * 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); } } }
/** * Convert an image. * * @param img.Image image * @return bool * @throws img.ImagingException */ public function convert($image) { // Create temporary variable as local variable access is faster // than member variable access. $handle = $image->handle; if (imageistruecolor($handle)) { $l = []; $h = $image->getHeight(); $w = $image->getWidth(); for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { $rgb = imagecolorat($handle, $x, $y); if (!isset($l[$rgb])) { $g = 0.299 * ($rgb >> 16 & 0xff) + 0.587 * ($rgb >> 8 & 0xff) + 0.114 * ($rgb & 0xff); $l[$rgb] = imagecolorallocate($handle, $g, $g, $g); } imagesetpixel($handle, $x, $y, $l[$rgb]); } } unset($l); } else { for ($i = 0, $t = imagecolorstotal($handle); $i < $t; $i++) { $c = imagecolorsforindex($handle, $i); $g = 0.299 * $c['red'] + 0.587 * $c['green'] + 0.114 * $c['blue']; imagecolorset($handle, $i, $g, $g, $g); } } }