本文整理汇总了PHP中imagecolorsforindex函数的典型用法代码示例。如果您正苦于以下问题:PHP imagecolorsforindex函数的具体用法?PHP imagecolorsforindex怎么用?PHP imagecolorsforindex使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了imagecolorsforindex函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create_histogram
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;
}
示例2: run
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;
}
示例3: createMask
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;
}
示例4: extractPalette
/**
* 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);
}
示例5: modify
/**
* 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;
}
示例6: toBinary
/**
* 二值化
*/
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);
}
}
}
}
示例7: check_box
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;
}
}
}
示例8: generateArray
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!');
}
}
示例9: imagecopybicubic
/**
* 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);
}
}
}
示例10: execute
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);
}
示例11: colorize
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;
}
示例12: getHec
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;
}
示例13: build
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;
}
示例14: GenerateFromLocalImage
/**
* 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;
}
示例15: imagemergealpha
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;
}