function changeBackground($im, $red, $green, $blue) { imagetruecolortopalette($im, false, 255); $ig = imagecolorat($im, 0, 0); imagecolorset($im, $ig, $red, $green, $blue); return $im; }
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; }
function execute() { $img =& $this->image->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); } }
function applyFilter(Canvas $canvas) { $himage = $canvas->getImage(); // If not we need to do some enumeration $total = imagecolorstotal($himage); if ($total > 0) { // This works for indexed images but not for truecolor for ($i = 0; $i < $total; $i++) { $index = imagecolorsforindex($himage, $i); $rgb = rgb($index['red'], $index['green'], $index['blue']); $hsv = hsv($rgb); $hsv->hue = $this->hue; $rgb = rgb($hsv); $red = $rgb->red; $green = $rgb->green; $blue = $rgb->blue; imagecolorset($himage, $i, $red, $green, $blue); } } else { // For truecolor we need to enum it all for ($x = 0; $x < imagesx($himage); $x++) { for ($y = 0; $y < imagesy($himage); $y++) { $index = imagecolorat($himage, $x, $y); $rgb = rgb($index['red'], $index['green'], $index['blue'], $index['alpha']); $hsv = hsv($rgb); $hsv->hue = $this->hue; $rgb = rgb($hsv); $red = $rgb->red; $green = $rgb->green; $blue = $rgb->blue; imagesetpixel($himage, $x, $y, $red << 16 | $green < 8 | $blue); } } } }
function setColorPalette($input, $output, $clut) { $gd = null; if (file_exists($input)) { $gd = imagecreatefrompng($input); } else { throw new Exception("Unable to apply color-table: {$input} does not exist."); } if (!$gd) { throw new Exception("Unable to apply color-table: {$input} is not a valid image."); } $ctable = imagecreatefrompng($clut); for ($i = 0; $i <= 255; $i++) { $rgb = imagecolorat($ctable, 0, $i); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; imagecolorset($gd, $i, $r, $g, $b); } // Enable interlacing imageinterlace($gd, true); imagepng($gd, $output); // Cleanup if ($input != $output) { unlink($input); } imagedestroy($gd); imagedestroy($ctable); }
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); }
/** * 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); } } }
function applyFilter(Canvas $canvas) { $himage = $canvas->getImage(); if (function_exists('imagefilter')) { // If gd is bundled this will work imagefilter($himage, IMG_FILTER_GRAYSCALE); } else { // If not we need to do some enumeration $total = imagecolorstotal($himage); if ($total > 0) { // This works for indexed images but not for truecolor for ($i = 0; $i < $total; $i++) { $index = imagecolorsforindex($himage, $i); $avg = ($index["red"] + $index["green"] + $index["blue"]) / 3; $red = $avg; $green = $avg; $blue = $avg; imagecolorset($himage, $i, $red, $green, $blue); } } else { // For truecolor we need to enum it all for ($x = 0; $x < imagesx($himage); $x++) { for ($y = 0; $y < imagesy($himage); $y++) { $index = imagecolorat($himage, $x, $y); $avg = (($index & 0xff) + ($index >> 8 & 0xff) + ($index >> 16 & 0xff)) / 3; imagesetpixel($himage, $x, $y, $avg | $avg << 8 | $avg << 16); } } } } }
function setColorPalette($input, $clut, $base64 = false) { $gd = null; if ($base64) { $input = base64_decode($input); } $gd = imagecreatefromstring($input); $ctable = imagecreatefrompng($clut); for ($i = 0; $i <= 255; $i++) { $rgb = imagecolorat($ctable, 0, $i); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; imagecolorset($gd, $i, $r, $g, $b); } // start buffering ob_start(); imagepng($gd, NULL); $blob = ob_get_contents(); // end capture ob_end_clean(); // be tidy; free up memory imagedestroy($gd); imagedestroy($ctable); if ($base64) { $blob = base64_encode($blob); } return $blob; }
function AdjBrightContrast($img, $bright, $contr) { $nbr = imagecolorstotal($img); for ($i = 0; $i < $nbr; ++$i) { $colarr = imagecolorsforindex($img, $i); $r = AdjRGBBrightContrast($colarr["red"], $bright, $contr); $g = AdjRGBBrightContrast($colarr["green"], $bright, $contr); $b = AdjRGBBrightContrast($colarr["blue"], $bright, $contr); imagecolorset($img, $i, $r, $g, $b); } }
public function generate() { $findColor = Image_Image::hexColorToArrayColor($this->find); $replaceColor = Image_Image::hexColorToArrayColor($this->replace); $index = imagecolorclosest($this->_owner->image, $findColor['red'], $findColor['green'], $findColor['blue']); //find imagecolorset($this->_owner->image, $index, $replaceColor['red'], $replaceColor['green'], $replaceColor['blue']); //replace unset($index); return true; }
/** * Applies the filter to the resource * * @param ImageResource $aResource */ public function applyFilter(ImageResource $aResource) { $dest = $aResource->getResource(); if (imageistruecolor($dest)) { imagetruecolortopalette($dest, false, 256); } foreach ($this->search as $search) { $searchRgb = new Color($search); $index = imagecolorclosest($aResource->getResource(), $searchRgb->getRed(), $searchRgb->getGreen(), $searchRgb->getBlue()); // get White COlor imagecolorset($aResource->getResource(), $index, $this->replace->getRed(), $this->replace->getGreen(), $this->replace->getBlue()); // SET NEW COLOR } $aResource->setResource($dest); }
/** * Tints an image. * * @param data $img * @param int $tint_r * @param int $tint_g * @param int $tint_b * @return data * * @example: * imagetint($img); // Grayscale, no tinting * imagetint($img, 304, 242, 209); // What I use for sepia * imagetint($img, 0, 0, 255); // A berry blue image * * The RGB values for tinting are normally from 0 to 255. * But, you can use values larger than 255 to lighten and "burn" the image. * The sepia example above does this a little, the below example provides * a better example of lightening the image and burning the light areas * out a little: * * imagetint($img, 400, 400, 400); // Lighten image * imagetint($img, 127, 127, 127); // Darken image */ public function imagetint(&$img, $tint_r = 255, $tint_g = 255, $tint_b = 255) { if (!$this->gd) { return; } $width = imagesx($this->gd); $height = imagesy($this->gd); $dest = imagecreate($width, $height); for ($i = 0; $i < 256; $i++) { imagecolorallocate($dest, $i, $i, $i); } imagecopyresized($dest, $this->gd, 0, 0, 0, 0, $width, $height, $width, $height); for ($i = 0; $i < 256; $i++) { imagecolorset($dest, $i, min($i * abs($tint_r) / 255, 255), min($i * abs($tint_g) / 255, 255), min($i * abs($tint_b) / 255, 255)); } $img = imagecreate($width, $height); imagecopy($this->gd, $dest, 0, 0, 0, 0, $width, $height); imagedestroy($dest); $output = clone $this; $output->setImageResource($this->gd); return $output; }
function send_thumb($image, $compression, $sizex, $sizey, $generateOnly = false) { global $bg_color_preview_R, $bg_color_preview_G, $bg_color_preview_B; set_error_handler("on_error_no_output"); ini_set("gd.jpeg_ignore_warning", 1); // since php 5.1.3 this leads that corrupt jpgs are read much better! set_error_handler("on_error"); $srcx = 0; $srcy = 0; $dimx = $sizex; $dimy = $sizey; $usethumbs = false; if (file_exists(dirname(__FILE__) . "/thumbs") && is_writable(dirname(__FILE__) . "/thumbs")) { // is a caching dir available and writeable? $cachename = dirname(__FILE__) . "/thumbs/" . sha1($image . $sizex) . ".jpg"; $usethumbs = true; } if ($usethumbs && file_exists($cachename)) { // we return the jpg! header("Content-type: image/jpg"); header("Content-Length: " . filesize($cachename)); header("Pragma: no-cache"); header("Expires: 0"); $fp = fopen($cachename, "rb"); while ($content = fread($fp, 8192)) { print $content; } fclose($fp); return true; } else { if (file_exists($image)) { $oldsize = getimagesize($image); // for broken images we try to read the exif data! if ($oldsize[0] == 0) { $oldsize = get_exif_size($image, $image); } $oldsizex = $oldsize[0]; $oldsizey = $oldsize[1]; if ($oldsizex < $sizex && $oldsizey < $sizey) { $sizex = $oldsizex; $sizey = $oldsizey; } $height = $sizey; $width = $height / $oldsizey * $oldsizex; if ($width > $sizex) { $width = $sizex; $height = $width / $oldsizex * $oldsizey; } if (isMemoryOk($oldsize, "")) { $src = get_image_src($image, $oldsize[2]); if (!$src) { // error in image! if ($sizex < 100) { // we return an empty white one ;). $src = ImageCreateTrueColor($oldsizex, $oldsizey); $back = imagecolorallocate($src, 255, 255, 255); imagefilledrectangle($src, 0, 0, $oldsizex, $oldsizex, $back); } debug($image . " is not a valid image - please check the file."); return false; } // $dst = ImageCreateTrueColor($width, $height); $dst = ImageCreateTrueColor($dimx, $dimy); if ($dimx < 100) { // white bg for small preview $back = imagecolorallocate($dst, $bg_color_preview_R, $bg_color_preview_G, $bg_color_preview_B); } else { // gray bg for big preview $back = imagecolorallocate($dst, 245, 245, 245); } imagefilledrectangle($dst, 0, 0, $dimx, $dimy, $back); if ($dimx > 100) { // border imagerectangle($dst, 0, 0, $dimx - 1, $dimy - 1, imagecolorallocate($dst, 160, 160, 160)); } $offsetx = 0; $offsetx_b = 0; if ($dimx > $width) { // we have to center! $offsetx = floor(($dimx - $width) / 2); } else { if ($dimx > 100) { $offsetx = 4; $offsetx_b = 8; } } $offsety = 0; $offsety_b = 0; if ($dimy > $height) { // we have to center! $offsety = floor(($dimy - $height) / 2); } else { if ($dimx > 100) { $offsety = 4; $offsety_b = 8; } } $trans = imagecolortransparent($src); imagecolorset($src, $trans, 255, 255, 255); imagecolortransparent($src, imagecolorallocate($src, 0, 0, 0)); imagecopyresampled($dst, $src, $offsetx, $offsety, $srcx, $srcy, $width - $offsetx_b, $height - $offsety_b, $oldsizex, $oldsizey); header("Content-type: image/jpg"); if ($usethumbs) { // we save the thumb imagejpeg($dst, $cachename, $compression); } if (!$generateOnly) { header("Pragma: no-cache"); header("Expires: 0"); ob_start(); if (imagejpeg($dst, "", $compression)) { $buffer = ob_get_contents(); header("Content-Length: " . strlen($buffer)); ob_end_clean(); echo $buffer; @imagedestroy($dst); return true; } else { ob_end_flush(); debug('cannot save: ' . $image); @imagedestroy($src); } } } } } return false; }
static function image_resize($src, $dest, $width, $height, $quality = 90) { if (!file_exists($src)) { return false; } $size = getimagesize($src); if ($size === false) { return false; } $format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1)); $w = $size[0]; $h = $size[1]; if ($width < $w && $w > $h) { $new_width = $width; $coef = $w / $width; $new_height = round($h / $coef); } elseif ($h == $w) { $new_height = min($height, $width); $new_width = $new_height; } else { $new_height = $height; $coef = $h / $height; $new_width = round($w / $coef); } if ($width > $w && $height > $h) { $new_width = $w; $new_height = $h; } if ($format == 'jpeg') { $image_src = imagecreatefromjpeg($src); } if ($format == 'png') { $image_src = imagecreatefrompng($src); } if ($format == 'gif') { $image_src = imagecreatefromgif($src); } $image_dest = imagecreatetruecolor($new_width, $new_height); $image_dest2 = imagecreatetruecolor($w, $h); imagecopyresampled($image_dest, $image_src, 0, 0, 0, 0, $new_width, $new_height, $w, $h); //imagecopyresampled($image_dest2, $image_src, 0, 0, 0, 0, $w, $h, $w, $h); imagecolorset($image_dest, 0, 255, 255, 255); if ($format == 'jpeg') { imagejpeg($image_dest, $dest, $quality); } if ($format == 'png') { imagepng($image_dest, $dest, $quality); } if ($format == 'gif') { imagegif($image_dest, $dest, $quality); } //echo imagecolorat($image_dest,1,1).'<br>src='.imagecolorat($image_dest2,1,1);exit; //imagecolortransparent($image_dest, "#000000"); imagedestroy($image_src); imagedestroy($image_dest); return true; }
function resetTransparency($img, $options) { if ($options['imageFormat'] && $options['imageFormat'] !== '') { // convert to a specific format $imageOptions = explode(':', $options['imageFormat']); if ($imageOptions[0] == WADFP_JPEG) { $transparent_index = imagecolortransparent($img); if ($transparent_index == -1) { $tmp = imagecreatetruecolor(imagesx($img), imagesy($img)); $white = imagecolorallocate($tmp, 255, 255, 255); imagefilledrectangle($tmp, 0, 0, imagesx($img), imagesy($img), $white); imagecopy($tmp, $img, 0, 0, 0, 0, imagesx($img), imagesy($img)); $img = $tmp; } if ($transparent_index >= 0) { imagetruecolortopalette($img, true, 255); $transparent_index = imagecolortransparent($img); $transparent_old = imagecolorsforindex($img, $transparent_index); imagecolorset($img, $transparent_index, 255, 255, 255); } } } return $img; }
/** * Convert original image to greyscale and/or apply noise and sephia effect * */ private function ageimage() { imagetruecolortopalette($this->im,1,256); for ($c=0;$c<256;$c++) { $col=imagecolorsforindex($this->im,$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->im,$c,max(0,min(255,$r)),max(0,min(255,$g)),max(0,min(255,$b))); } }
function giffont() { $seccode = $this->code; $seccodedir = array(); if (function_exists('imagecreatefromgif')) { $seccoderoot = $this->datapath . 'gif/'; $dirs = opendir($seccoderoot); while ($dir = readdir($dirs)) { if ($dir != '.' && $dir != '..' && file_exists($seccoderoot . $dir . '/9.gif')) { $seccodedir[] = $dir; } } } $widthtotal = 0; for ($i = 0; $i <= 3; $i++) { $this->imcodefile = $seccodedir ? $seccoderoot . $seccodedir[array_rand($seccodedir)] . '/' . strtolower($seccode[$i]) . '.gif' : ''; if (!empty($this->imcodefile) && file_exists($this->imcodefile)) { $font[$i]['file'] = $this->imcodefile; $font[$i]['data'] = getimagesize($this->imcodefile); $font[$i]['width'] = $font[$i]['data'][0] + mt_rand(0, 6) - 4; $font[$i]['height'] = $font[$i]['data'][1] + mt_rand(0, 6) - 4; if ($this->width / 5 - $font[$i]['width'] > 0) { $tt = mt_rand(0, $this->width / 5 - $font[$i]['width']); } else { $tt = mt_rand($this->width / 5 - $font[$i]['width'], 0); } $font[$i]['width'] += $tt; $widthtotal += $font[$i]['width']; } else { $font[$i]['file'] = ''; $font[$i]['width'] = 8 + mt_rand(0, $this->width / 5 - 5); $widthtotal += $font[$i]['width']; } } $x = mt_rand(1, $this->width - $widthtotal); for ($i = 0; $i <= 3; $i++) { $this->color && ($this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255))); if ($font[$i]['file']) { $this->imcode = imagecreatefromgif($font[$i]['file']); if ($this->size) { $font[$i]['width'] = mt_rand($font[$i]['width'] - $this->width / 20, $font[$i]['width'] + $this->width / 20); $font[$i]['height'] = mt_rand($font[$i]['height'] - $this->width / 20, $font[$i]['height'] + $this->width / 20); } if ($this->height - $font[$i]['height'] > 0) { $y = mt_rand(0, $this->height - $font[$i]['height']); } else { $y = mt_rand($this->height - $font[$i]['height'], 0); } if ($this->shadow) { $this->imcodeshadow = $this->imcode; imagecolorset($this->imcodeshadow, 0, 0, 0, 0); imagecopyresized($this->im, $this->imcodeshadow, $x + 1, $y + 1, 0, 0, $font[$i]['width'], $font[$i]['height'], $font[$i]['data'][0], $font[$i]['data'][1]); } imagecolorset($this->imcode, 0, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]); imagecopyresized($this->im, $this->imcode, $x, $y, 0, 0, $font[$i]['width'], $font[$i]['height'], $font[$i]['data'][0], $font[$i]['data'][1]); } else { $y = mt_rand(0, $this->height - 20); if ($this->shadow) { $text_shadowcolor = imagecolorallocate($this->im, 0, 0, 0); imagechar($this->im, 5, $x + 1, $y + 1, $seccode[$i], $text_shadowcolor); } $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]); imagechar($this->im, 5, $x, $y, $seccode[$i], $text_color); } $x += $font[$i]['width']; } }
/** * Convert a true color image to a palette image with 256 colors and preserve transparency * * @return static */ public function convertToPaletteImage() { if (!imageistruecolor($this->gdResource)) { return $this; } $width = imagesx($this->gdResource); $height = imagesy($this->gdResource); $transparentColor = null; if ($this->countColors(256) <= 256) { $paletteImage = imagecreate($width, $height); $colors = array(); $isTransparent = false; for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $color = imagecolorat($this->gdResource, $x, $y); // Check if the pixel is fully transparent if (($color >> 24 & 0x7f) === 127) { $isTransparent = true; } else { $colors[$color & 0xffffff] = true; } } } $colors = array_keys($colors); foreach ($colors as $index => $color) { imagecolorset($paletteImage, $index, $color >> 16 & 0xff, $color >> 8 & 0xff, $color & 0xff); } if ($isTransparent) { $transparentColor = imagecolorallocate($paletteImage, 0, 0, 0); imagecolortransparent($paletteImage, $transparentColor); } imagecopy($paletteImage, $this->gdResource, 0, 0, 0, 0, $width, $height); } else { $paletteImage = imagecreatetruecolor($width, $height); imagealphablending($paletteImage, false); imagesavealpha($paletteImage, true); imagecopy($paletteImage, $this->gdResource, 0, 0, 0, 0, $width, $height); // 256 minus 1 for the transparent color imagetruecolortopalette($paletteImage, false, 255); $transparentColor = imagecolorallocate($paletteImage, 0, 0, 0); imagecolortransparent($paletteImage, $transparentColor); } if ($transparentColor !== null) { // Fix fully transparent pixels for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { // Check if the pixel is fully transparent if ((imagecolorat($this->gdResource, $x, $y) >> 24 & 0x7f) === 127) { imagefilledrectangle($paletteImage, $x, $y, $x, $y, $transparentColor); } } } } imagedestroy($this->gdResource); $this->gdResource = $paletteImage; return $this; }
function seccode_giffont() { global $seccode, $seccodedata, $im, $c; $seccodedir = array(); if (function_exists('imagecreatefromgif')) { $seccoderoot = 'image/seccode/'; $dirs = opendir($seccoderoot); while ($dir = readdir($dirs)) { if ($dir != '.' && $dir != '..' && file_exists($seccoderoot . $dir . '/9.gif')) { $seccodedir[] = $dir; } } } $widthtotal = 0; for ($i = 0; $i <= 3; $i++) { $imcodefile = $seccodedir ? $seccoderoot . $seccodedir[array_rand($seccodedir)] . '/' . strtolower($seccode[$i]) . '.gif' : ''; if (!empty($imcodefile) && file_exists($imcodefile)) { $font[$i]['file'] = $imcodefile; $font[$i]['data'] = getimagesize($imcodefile); $font[$i]['width'] = $font[$i]['data'][0] + mt_rand(0, 6) - 4; $font[$i]['height'] = $font[$i]['data'][1] + mt_rand(0, 6) - 4; $font[$i]['width'] += mt_rand(0, $seccodedata['width'] / 5 - $font[$i]['width']); $widthtotal += $font[$i]['width']; } else { $font[$i]['file'] = ''; $font[$i]['width'] = 8 + mt_rand(0, $seccodedata['width'] / 5 - 5); $widthtotal += $font[$i]['width']; } } $x = mt_rand(1, $seccodedata['width'] - $widthtotal); for ($i = 0; $i <= 3; $i++) { if ($font[$i]['file']) { $imcode = imagecreatefromgif($font[$i]['file']); $y = mt_rand(0, $seccodedata['height'] - $font[$i]['height']); if ($seccodedata['shadow']) { $imcodeshadow = $imcode; imagecolorset($imcodeshadow, 0, 255 - $c[0], 255 - $c[1], 255 - $c[2]); imagecopyresized($im, $imcodeshadow, $x + 1, $y + 1, 0, 0, $font[$i]['width'], $font[$i]['height'], $font[$i]['data'][0], $font[$i]['data'][1]); } imagecolorset($imcode, 0, $c[0], $c[1], $c[2]); imagecopyresized($im, $imcode, $x, $y, 0, 0, $font[$i]['width'], $font[$i]['height'], $font[$i]['data'][0], $font[$i]['data'][1]); } else { $y = mt_rand(0, $seccodedata['height'] - 20); if ($seccodedata['shadow']) { $text_shadowcolor = imagecolorallocate($im, 255 - $c[0], 255 - $c[1], 255 - $c[2]); imagechar($im, 5, $x + 1, $y + 1, $seccode[$i], $text_shadowcolor); } $text_color = imagecolorallocate($im, $c[0], $c[1], $c[2]); imagechar($im, 5, $x, $y, $seccode[$i], $text_color); } $x += $font[$i]['width']; } }
private function _makeGrayscale($im) { if (imageistruecolor($im)) { imagetruecolortopalette($im, false, 256); } for ($c = 0; $c < imagecolorstotal($im); $c++) { $col = imagecolorsforindex($im, $c); $gray = round(0.299 * $col['red'] + 0.587 * $col['green'] + 0.114 * $col['blue']); imagecolorset($im, $c, $gray, $gray, $gray); } return $im; }
public function sepia2() { if ($this->imageResized) { $total = imagecolorstotal($this->imageResized); for ($i = 0; $i < $total; $i++) { $index = imagecolorsforindex($this->imageResized, $i); $red = ($index["red"] * 0.393 + $index["green"] * 0.769 + $index["blue"] * 0.189) / 1.351; $green = ($index["red"] * 0.349 + $index["green"] * 0.6860000000000001 + $index["blue"] * 0.168) / 1.203; $blue = ($index["red"] * 0.272 + $index["green"] * 0.534 + $index["blue"] * 0.131) / 2.14; imagecolorset($this->imageResized, $i, $red, $green, $blue); } } }
/** * Generates the actual heatmap. */ private function generate_image($data) { require_once 'gd-rg.php'; $time = microtime(1); $this->logg('Started at ' . $time); // Find the maximum value from the given data. $max_data_value = 1; foreach ($data as $row) { if (isset($row[2]) && $max_data_value < $row[2]) { $max_data_value = $row[2]; } } $this->logg('Done sorting data at ' . (microtime(1) - $time)); // Create the heatmap image. $im = imagecreatetruecolor($this->get_config('width'), $this->get_config('height')); $white = imagecolorallocate($im, 255, 255, 255); imagefill($im, 0, 0, $white); imagealphablending($im, true); imagesavealpha($im, true); // Create a separate spot image for each value to be shown, with different // amounts of black. Having 25 separate shades of colour looks like a decent // number. $spots = array(); for ($i = 0; $i < $this->config['noc']; $i++) { // The gradient lib doesn't like too small values for $alpha_end, so we use // $noc for that, which happens to work well. $alpha_end = $this->map($i, 0, $this->config['noc'] - 1, $this->config['noc'], 255); $temp = new gd_gradient_alpha($this->config['r'], $this->config['r'], 'ellipse', '#000', 0x0, $alpha_end, 0); $spot = $temp->get_image(); imagealphablending($spot, true); imagesavealpha($spot, true); $spots[$i] = $spot; } $this->logg('Created ' . count($spots) . ' spots at ' . (microtime(1) - $time)); // Go through the data, and add appropriate spot images to the heatmap // image. for ($i = 0; $i < count($data); $i++) { $value = isset($data[$i][2]) ? $data[$i][2] : 1; $value = $this->map($value, 1, $max_data_value, 0, $this->config['noc'] - 1); imagecopy($im, $spots[$value], $data[$i][0], $data[$i][1], 0, 0, $this->config['r'], $this->config['r']); } $this->logg('Copied spots to image at ' . (microtime(1) - $time)); imagetruecolortopalette($im, $this->config['dither'], $this->config['noc']); $this->logg('Flattened black at ' . (microtime(1) - $time)); // Get the gradient from an image file $gi = 'gradient-' . $this->config['noc'] . '.png'; if (!file_exists($gi)) { $this->error("Can't find gradient file " . $gi . ". Make one using gradient-source.jpg"); } $gs = imagecreatefrompng($gi); imagetruecolortopalette($gs, TRUE, $this->config['noc']); // Get a list of different gray values in the image, and order them. $grays = array(); for ($i = 0; $i < imagecolorstotal($im); $i++) { $c = imagecolorsforindex($im, $i); $grays[] = str_pad($c['red'] * 65536 + $c['green'] * 256 + $c['blue'], 8, '0', STR_PAD_LEFT) . ':' . $i; } sort($grays); $indexes = array(); foreach ($grays as $gray) { $indexes[] = substr($gray, strpos($gray, ':') + 1); } $this->logg('Created gray indexes at ' . (microtime(1) - $time)); // Replace each shade of gray with the matching rainbow colour. $i = 0; foreach ($indexes as $index) { $fill_index = imagecolorat($gs, $i, 0); $fill_color = imagecolorsforindex($gs, $fill_index); imagecolorset($im, $index, $fill_color['red'], $fill_color['green'], $fill_color['blue']); $i++; } $this->logg('Replaced black with rainbow at ' . (microtime(1) - $time)); // Finally switch from white background to transparent. $closest = imagecolorclosest($im, 255, 255, 255); imagecolortransparent($im, $closest); $this->logg('done at ' . (microtime(1) - $time)); // Debugging text if ($this->config['debug']) { $text_color = imagecolorallocate($im, 0, 0, 0); $y = 5; foreach (explode("\n", $this->debug_log) as $line) { imagestring($im, 3, 250, $y, $line, $text_color); $y = $y + 10; } } $this->im = $im; }
/** * Adds a border of constant width around an image * * @param int $border_width Width of border to add * @author Peter Bowyer * @return bool TRUE * @access public */ function addBorder($border_width, $color = '') { $this->new_x = $this->img_x + 2 * $border_width; $this->new_y = $this->img_y + 2 * $border_width; $new_img = $this->_createImage($new_x, $new_y, $this->true_color); $options = array('pencilColor', $color); $color = $this->_getColor('pencilColor', $options, array(0, 0, 0)); if ($color) { if ($this->true_color) { $c = imagecolorresolve($this->imageHandle, $color[0], $color[1], $color[2]); imagefill($new_img, 0, 0, $c); } else { imagecolorset($new_img, imagecolorat($new_img, 0, 0), $color[0], $color[1], $color[2]); } } ImageCopy($new_img, $this->imageHandle, $border_width, $border_width, 0, 0, $this->img_x, $this->img_y); $this->imageHandle = $new_img; $this->resized = true; return true; }
function imgfont(&$im) { $img = array(); if (function_exists('imagecreatefromgif')) { $imgfont = $GLOBALS['imgdir'] . '/ck/gif/'; $dirs = opendir($imgfont); while ($file = readdir($dirs)) { if ($file != '.' && $file != '..' && file_exists($imgfont . $file . '/2.gif')) { $img[] = $file; } } @closedir($dirs); } $code = $this->getCode(); $width = $this->width / $this->num; for ($i = 0; $i < $this->num; $i++) { $filepath = $img ? $imgfont . $img[array_rand($img)] . '/' . strtolower($code[$i]) . '.gif' : ''; $len = $i * $width; if ($filepath && file_exists($filepath)) { $src_im = imagecreatefromgif($filepath); list($srcW, $srcH) = getimagesize($filepath); $dstW = $this->height / 2; $dstH = $dstW * $srcH / $srcW; $x = mt_rand($len, $len + $width - $dstW); $y = mt_rand(0, $this->height - $dstH); if ($this->style & 16) { imagecolorset($src_im, 0, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); } if ($this->style & 2) { $rate = mt_rand(80, 120) / 100; $dstW *= $rate; $dstH *= $rate; } imagecopyresized($im, $src_im, $x, $y, 0, 0, $dstW, $dstH, $srcW, $srcH); } else { $color = $this->getColor($im); $x = mt_rand($len, $len + $width - 10); $y = mt_rand(10, $this->height - 10); imagechar($im, 5, $x, $y, $code[$i], $color); } } }
/** * Color a territory the color of a given countryID. This must be done * before anything else is written to the image * * @param string $terrName The name of the territory to color * @param string $countryID The name of the countryID */ public function colorTerritory($terrID, $countryID) { /* * The map files are both color coded so that each territory has its own * unique color. When coloring a territory the territory's color is * selected from the map, and imagecolorset() is used to change the * territory's unique color to the desired color */ list($x, $y) = $this->territoryPositions[$terrID]; $territoryColor = imagecolorat($this->map['image'], $x, $y); list($r, $g, $b) = $this->countryColors[$countryID]; imagecolorset($this->map['image'], $territoryColor, $r, $g, $b); }
$teststring = "{$mins}'{$secs}"; // $teststring = "00d00:00:00.01"; $image = imagecreate(64, 9); $imagenum = imagecreatefrompng("digits8.png"); $bg = imagecolorallocate($image, 255, 0, 255); $black = imagecolorallocate($image, 0, 0, 0); if ($left > 3600) { $barcol = imagecolorallocate($image, 100, 255, 100); } elseif ($left >= 1800) { $barcolr = 50 + 205 * ($left / 1800); $barcol = imagecolorallocate($image, 100, 255, $barcolr); } else { $barcolg = 0 + 255 * ($left / 1800); $barcol = imagecolorallocate($image, 0, $barcolg, 255); imagecolorset($imagenum, 2, $barcolg, $barcolg / 2 + 128, 255); imagecolorset($imagenum, 3, 0, $barcolg / 4, $barcolg / 1.5); } imageline($image, $bpad + 1, 8, $barpct + 1 + $bpad, 8, $black); imageline($image, $bpad, 7, $barpct + $bpad, 7, $barcol); $nums = str_split($teststring); $ofs = 5; foreach ($nums as $n) { $of = 6; switch ($n) { case ":": $w = 2; $p = 81; $of = 2; break; case "\"": $w = 4;
/** * Create the image. * * @throws Exceptions\DataDoesntExistsException * @throws Exceptions\VersionTooLargeException * @throws Exceptions\ImageSizeTooLargeException * @throws \OverflowException */ public function create() { $image_path = $this->image_path; $path = $this->path; $version_ul = 40; $qrcode_data_string = $this->text; //Previously from $_GET["d"]; $qrcode_error_correct = $this->error_correction; //Previously from $_GET["e"]; $qrcode_module_size = $this->module_size; //Previously from $_GET["s"]; $qrcode_version = $this->version; //Previously from $_GET["v"]; $qrcode_image_type = $this->image_type; //Previously from $_GET["t"]; $qrcode_structureappend_n = $this->structure_append_n; //Previously from $_GET["n"]; $qrcode_structureappend_m = $this->structure_append_m; //Previously from $_GET["m"]; $qrcode_structureappend_parity = $this->structure_append_parity; //Previously from $_GET["p"]; $qrcode_structureappend_originaldata = $this->structure_append_original_data; //Previously from $_GET["o"]; if ($qrcode_module_size > 0) { } else { if ($qrcode_image_type == 'jpeg') { $qrcode_module_size = 8; } else { $qrcode_module_size = 4; } } $data_length = strlen($qrcode_data_string); if ($data_length <= 0) { throw new DataDoesntExistsException('QRCode: Data does not exists.'); } $data_counter = 0; if ($qrcode_structureappend_n > 1 && $qrcode_structureappend_n <= 16 && $qrcode_structureappend_m > 0 && $qrcode_structureappend_m <= 16) { $data_value[0] = 3; $data_bits[0] = 4; $data_value[1] = $qrcode_structureappend_m - 1; $data_bits[1] = 4; $data_value[2] = $qrcode_structureappend_n - 1; $data_bits[2] = 4; $originaldata_length = strlen($qrcode_structureappend_originaldata); if ($originaldata_length > 1) { $qrcode_structureappend_parity = 0; $i = 0; while ($i < $originaldata_length) { $qrcode_structureappend_parity = $qrcode_structureappend_parity ^ ord(substr($qrcode_structureappend_originaldata, $i, 1)); ++$i; } } $data_value[3] = $qrcode_structureappend_parity; $data_bits[3] = 8; $data_counter = 4; } $data_bits[$data_counter] = 4; /* --- determine encode mode */ if (preg_match('/[^0-9]/', $qrcode_data_string) != 0) { if (preg_match("/[^0-9A-Z \$\\*\\%\\+\\.\\/\\:\\-]/", $qrcode_data_string) != 0) { /* --- 8bit byte mode */ $codeword_num_plus = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8); $data_value[$data_counter] = 4; ++$data_counter; $data_value[$data_counter] = $data_length; $data_bits[$data_counter] = 8; /* #version 1-9 */ $codeword_num_counter_value = $data_counter; ++$data_counter; $i = 0; while ($i < $data_length) { $data_value[$data_counter] = ord(substr($qrcode_data_string, $i, 1)); $data_bits[$data_counter] = 8; ++$data_counter; ++$i; } } else { /* ---- alphanumeric mode */ $codeword_num_plus = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4); $data_value[$data_counter] = 2; ++$data_counter; $data_value[$data_counter] = $data_length; $data_bits[$data_counter] = 9; /* #version 1-9 */ $codeword_num_counter_value = $data_counter; $alphanumeric_character_hash = array('0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20, 'L' => 21, 'M' => 22, 'N' => 23, 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34, 'Z' => 35, ' ' => 36, '$' => 37, '%' => 38, '*' => 39, '+' => 40, '-' => 41, '.' => 42, '/' => 43, ':' => 44); $i = 0; ++$data_counter; while ($i < $data_length) { if ($i % 2 == 0) { $data_value[$data_counter] = $alphanumeric_character_hash[substr($qrcode_data_string, $i, 1)]; $data_bits[$data_counter] = 6; } else { $data_value[$data_counter] = $data_value[$data_counter] * 45 + $alphanumeric_character_hash[substr($qrcode_data_string, $i, 1)]; $data_bits[$data_counter] = 11; ++$data_counter; } ++$i; } } } else { /* ---- numeric mode */ $codeword_num_plus = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4); $data_value[$data_counter] = 1; ++$data_counter; $data_value[$data_counter] = $data_length; $data_bits[$data_counter] = 10; /* #version 1-9 */ $codeword_num_counter_value = $data_counter; $i = 0; ++$data_counter; while ($i < $data_length) { if ($i % 3 == 0) { $data_value[$data_counter] = substr($qrcode_data_string, $i, 1); $data_bits[$data_counter] = 4; } else { $data_value[$data_counter] = $data_value[$data_counter] * 10 + substr($qrcode_data_string, $i, 1); if ($i % 3 == 1) { $data_bits[$data_counter] = 7; } else { $data_bits[$data_counter] = 10; ++$data_counter; } } ++$i; } } if (array_key_exists($data_counter, $data_bits) && $data_bits[$data_counter] > 0) { ++$data_counter; } $i = 0; $total_data_bits = 0; while ($i < $data_counter) { $total_data_bits += $data_bits[$i]; ++$i; } $ecc_character_hash = array('L' => '1', 'l' => '1', 'M' => '0', 'm' => '0', 'Q' => '3', 'q' => '3', 'H' => '2', 'h' => '2'); if (!is_numeric($qrcode_error_correct)) { $ec = @$ecc_character_hash[$qrcode_error_correct]; } else { $ec = $qrcode_error_correct; } if (!$ec) { $ec = 0; } $max_data_bits = 0; $max_data_bits_array = array(0, 128, 224, 352, 512, 688, 864, 992, 1232, 1456, 1728, 2032, 2320, 2672, 2920, 3320, 3624, 4056, 4504, 5016, 5352, 5712, 6256, 6880, 7312, 8000, 8496, 9024, 9544, 10136, 10984, 11640, 12328, 13048, 13800, 14496, 15312, 15936, 16816, 17728, 18672, 152, 272, 440, 640, 864, 1088, 1248, 1552, 1856, 2192, 2592, 2960, 3424, 3688, 4184, 4712, 5176, 5768, 6360, 6888, 7456, 8048, 8752, 9392, 10208, 10960, 11744, 12248, 13048, 13880, 14744, 15640, 16568, 17528, 18448, 19472, 20528, 21616, 22496, 23648, 72, 128, 208, 288, 368, 480, 528, 688, 800, 976, 1120, 1264, 1440, 1576, 1784, 2024, 2264, 2504, 2728, 3080, 3248, 3536, 3712, 4112, 4304, 4768, 5024, 5288, 5608, 5960, 6344, 6760, 7208, 7688, 7888, 8432, 8768, 9136, 9776, 10208, 104, 176, 272, 384, 496, 608, 704, 880, 1056, 1232, 1440, 1648, 1952, 2088, 2360, 2600, 2936, 3176, 3560, 3880, 4096, 4544, 4912, 5312, 5744, 6032, 6464, 6968, 7288, 7880, 8264, 8920, 9368, 9848, 10288, 10832, 11408, 12016, 12656, 13328); if (!is_numeric($qrcode_version)) { $qrcode_version = 0; } if (!$qrcode_version) { /* #--- auto version select */ $i = 1 + 40 * $ec; $j = $i + 39; $qrcode_version = 1; while ($i <= $j) { if ($max_data_bits_array[$i] >= $total_data_bits + $codeword_num_plus[$qrcode_version]) { $max_data_bits = $max_data_bits_array[$i]; break; } ++$i; ++$qrcode_version; } } else { $max_data_bits = $max_data_bits_array[$qrcode_version + 40 * $ec]; } if ($qrcode_version > $version_ul) { throw new VersionTooLargeException('QRCode : version too large'); } $total_data_bits += $codeword_num_plus[$qrcode_version]; $data_bits[$codeword_num_counter_value] += $codeword_num_plus[$qrcode_version]; $max_codewords_array = array(0, 26, 44, 70, 100, 134, 172, 196, 242, 292, 346, 404, 466, 532, 581, 655, 733, 815, 901, 991, 1085, 1156, 1258, 1364, 1474, 1588, 1706, 1828, 1921, 2051, 2185, 2323, 2465, 2611, 2761, 2876, 3034, 3196, 3362, 3532, 3706); $max_codewords = $max_codewords_array[$qrcode_version]; $max_modules_1side = 17 + ($qrcode_version << 2); $matrix_remain_bit = array(0, 0, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0); /* ---- read version ECC data file */ $byte_num = $matrix_remain_bit[$qrcode_version] + ($max_codewords << 3); $filename = $path . '/qrv' . $qrcode_version . '_' . $ec . '.dat'; $fp1 = fopen($filename, 'rb'); $matx = fread($fp1, $byte_num); $maty = fread($fp1, $byte_num); $masks = fread($fp1, $byte_num); $fi_x = fread($fp1, 15); $fi_y = fread($fp1, 15); $rs_ecc_codewords = ord(fread($fp1, 1)); $rso = fread($fp1, 128); fclose($fp1); $matrix_x_array = unpack('C*', $matx); $matrix_y_array = unpack('C*', $maty); $mask_array = unpack('C*', $masks); $rs_block_order = unpack('C*', $rso); $format_information_x2 = unpack('C*', $fi_x); $format_information_y2 = unpack('C*', $fi_y); $format_information_x1 = array(0, 1, 2, 3, 4, 5, 7, 8, 8, 8, 8, 8, 8, 8, 8); $format_information_y1 = array(8, 8, 8, 8, 8, 8, 8, 8, 7, 5, 4, 3, 2, 1, 0); $max_data_codewords = $max_data_bits >> 3; $filename = $path . '/rsc' . $rs_ecc_codewords . '.dat'; $fp0 = fopen($filename, 'rb'); $i = 0; $rs_cal_table_array = array(); while ($i < 256) { $rs_cal_table_array[$i] = fread($fp0, $rs_ecc_codewords); ++$i; } fclose($fp0); /* --- set terminator */ if ($total_data_bits <= $max_data_bits - 4) { $data_value[$data_counter] = 0; $data_bits[$data_counter] = 4; } else { if ($total_data_bits < $max_data_bits) { $data_value[$data_counter] = 0; $data_bits[$data_counter] = $max_data_bits - $total_data_bits; } else { if ($total_data_bits > $max_data_bits) { throw new \OverflowException('QRCode : Overflow error'); } } } /* ----divide data by 8bit */ $i = 0; $codewords_counter = 0; $codewords[0] = 0; $remaining_bits = 8; while ($i <= $data_counter) { $buffer = @$data_value[$i]; $buffer_bits = @$data_bits[$i]; $flag = 1; while ($flag) { if ($remaining_bits > $buffer_bits) { $codewords[$codewords_counter] = @$codewords[$codewords_counter] << $buffer_bits | $buffer; $remaining_bits -= $buffer_bits; $flag = 0; } else { $buffer_bits -= $remaining_bits; $codewords[$codewords_counter] = $codewords[$codewords_counter] << $remaining_bits | $buffer >> $buffer_bits; if ($buffer_bits == 0) { $flag = 0; } else { $buffer = $buffer & (1 << $buffer_bits) - 1; $flag = 1; } ++$codewords_counter; if ($codewords_counter < $max_data_codewords - 1) { $codewords[$codewords_counter] = 0; } $remaining_bits = 8; } } ++$i; } if ($remaining_bits != 8) { $codewords[$codewords_counter] = $codewords[$codewords_counter] << $remaining_bits; } else { --$codewords_counter; } /* ---- set padding character */ if ($codewords_counter < $max_data_codewords - 1) { $flag = 1; while ($codewords_counter < $max_data_codewords - 1) { ++$codewords_counter; if ($flag == 1) { $codewords[$codewords_counter] = 236; } else { $codewords[$codewords_counter] = 17; } $flag = $flag * -1; } } /* ---- RS-ECC prepare */ $i = 0; $j = 0; $rs_block_number = 0; $rs_temp[0] = ''; while ($i < $max_data_codewords) { $rs_temp[$rs_block_number] .= chr($codewords[$i]); ++$j; if ($j >= $rs_block_order[$rs_block_number + 1] - $rs_ecc_codewords) { $j = 0; ++$rs_block_number; $rs_temp[$rs_block_number] = ''; } ++$i; } /* # # RS-ECC main # */ $rs_block_number = 0; $rs_block_order_num = count($rs_block_order); while ($rs_block_number < $rs_block_order_num) { $rs_codewords = $rs_block_order[$rs_block_number + 1]; $rs_data_codewords = $rs_codewords - $rs_ecc_codewords; $rstemp = $rs_temp[$rs_block_number] . str_repeat(chr(0), $rs_ecc_codewords); $padding_data = str_repeat(chr(0), $rs_data_codewords); $j = $rs_data_codewords; while ($j > 0) { $first = ord(substr($rstemp, 0, 1)); if ($first) { $left_chr = substr($rstemp, 1); $cal = $rs_cal_table_array[$first] . $padding_data; $rstemp = $left_chr ^ $cal; } else { $rstemp = substr($rstemp, 1); } --$j; } $codewords = array_merge($codewords, unpack('C*', $rstemp)); ++$rs_block_number; } /* ---- flash matrix */ $matrix_content = array(); $i = 0; while ($i < $max_modules_1side) { $j = 0; while ($j < $max_modules_1side) { $matrix_content[$j][$i] = 0; ++$j; } ++$i; } /* --- attach data */ $i = 0; while ($i < $max_codewords) { $codeword_i = $codewords[$i]; $j = 8; while ($j >= 1) { $codeword_bits_number = ($i << 3) + $j; $matrix_content[$matrix_x_array[$codeword_bits_number]][$matrix_y_array[$codeword_bits_number]] = 255 * ($codeword_i & 1) ^ $mask_array[$codeword_bits_number]; $codeword_i = $codeword_i >> 1; --$j; } ++$i; } $matrix_remain = $matrix_remain_bit[$qrcode_version]; while ($matrix_remain) { $remain_bit_temp = $matrix_remain + ($max_codewords << 3); $matrix_content[$matrix_x_array[$remain_bit_temp]][$matrix_y_array[$remain_bit_temp]] = 255 ^ $mask_array[$remain_bit_temp]; --$matrix_remain; } #--- mask select $min_demerit_score = 0; $hor_master = ''; $ver_master = ''; $k = 0; while ($k < $max_modules_1side) { $l = 0; while ($l < $max_modules_1side) { $hor_master = $hor_master . chr($matrix_content[$l][$k]); $ver_master = $ver_master . chr($matrix_content[$k][$l]); ++$l; } ++$k; } $i = 0; $all_matrix = $max_modules_1side * $max_modules_1side; $mask_number = 0; while ($i < 8) { $demerit_n1 = 0; $ptn_temp = array(); $bit = 1 << $i; $bit_r = ~$bit & 255; $bit_mask = str_repeat(chr($bit), $all_matrix); $hor = $hor_master & $bit_mask; $ver = $ver_master & $bit_mask; $ver_shift1 = $ver . str_repeat(chr(170), $max_modules_1side); $ver_shift2 = str_repeat(chr(170), $max_modules_1side) . $ver; $ver_shift1_0 = $ver . str_repeat(chr(0), $max_modules_1side); $ver_shift2_0 = str_repeat(chr(0), $max_modules_1side) . $ver; $ver_or = chunk_split(~($ver_shift1 | $ver_shift2), $max_modules_1side, chr(170)); $ver_and = chunk_split(~($ver_shift1_0 & $ver_shift2_0), $max_modules_1side, chr(170)); $hor = chunk_split(~$hor, $max_modules_1side, chr(170)); $ver = chunk_split(~$ver, $max_modules_1side, chr(170)); $hor = $hor . chr(170) . $ver; $n1_search = '/' . str_repeat(chr(255), 5) . '+|' . str_repeat(chr($bit_r), 5) . '+/'; $n3_search = chr($bit_r) . chr(255) . chr($bit_r) . chr($bit_r) . chr($bit_r) . chr(255) . chr($bit_r); $demerit_n3 = substr_count($hor, $n3_search) * 40; $demerit_n4 = floor(abs((100 * (substr_count($ver, chr($bit_r)) / $byte_num) - 50) / 5)) * 10; $n2_search1 = '/' . chr($bit_r) . chr($bit_r) . '+/'; $n2_search2 = '/' . chr(255) . chr(255) . '+/'; $demerit_n2 = 0; preg_match_all($n2_search1, $ver_and, $ptn_temp); foreach ($ptn_temp[0] as $str_temp) { $demerit_n2 += strlen($str_temp) - 1; } $ptn_temp = array(); preg_match_all($n2_search2, $ver_or, $ptn_temp); foreach ($ptn_temp[0] as $str_temp) { $demerit_n2 += strlen($str_temp) - 1; } $demerit_n2 *= 3; $ptn_temp = array(); preg_match_all($n1_search, $hor, $ptn_temp); foreach ($ptn_temp[0] as $str_temp) { $demerit_n1 += strlen($str_temp) - 2; } $demerit_score = $demerit_n1 + $demerit_n2 + $demerit_n3 + $demerit_n4; if ($demerit_score <= $min_demerit_score || $i == 0) { $mask_number = $i; $min_demerit_score = $demerit_score; } ++$i; } $mask_content = 1 << $mask_number; # --- format information $format_information_value = $ec << 3 | $mask_number; $format_information_array = array('101010000010010', '101000100100101', '101111001111100', '101101101001011', '100010111111001', '100000011001110', '100111110010111', '100101010100000', '111011111000100', '111001011110011', '111110110101010', '111100010011101', '110011000101111', '110001100011000', '110110001000001', '110100101110110', '001011010001001', '001001110111110', '001110011100111', '001100111010000', '000011101100010', '000001001010101', '000110100001100', '000100000111011', '011010101011111', '011000001101000', '011111100110001', '011101000000110', '010010010110100', '010000110000011', '010111011011010', '010101111101101'); $i = 0; while ($i < 15) { $content = substr($format_information_array[$format_information_value], $i, 1); $matrix_content[$format_information_x1[$i]][$format_information_y1[$i]] = $content * 255; $matrix_content[$format_information_x2[$i + 1]][$format_information_y2[$i + 1]] = $content * 255; ++$i; } $mib = $max_modules_1side + 8; if ($this->size == 0) { $this->size = $mib * $qrcode_module_size; if ($this->size > 1480) { throw new ImageSizeTooLargeException('QRCode : Image size too large'); } } $image_width = $this->size + $this->padding * 2; $image_height = $this->size + $this->padding * 2; if (!empty($this->label)) { $font_box = imagettfbbox($this->label_font_size, 0, $this->label_font_path, $this->label); $label_width = (int) $font_box[2] - (int) $font_box[0]; $label_height = (int) $font_box[0] - (int) $font_box[7]; $image_height += $label_height + $this->padding; } $output_image = imagecreate($image_width, $image_height); imagecolorallocate($output_image, 255, 255, 255); if (!empty($this->label)) { $font_x = floor($image_width - $label_width) / 2; $font_y = $image_height - $this->padding; $color = imagecolorallocate($output_image, 0, 0, 0); imagettftext($output_image, $this->label_font_size, 0, $font_x, $font_y, $color, $this->label_font_path, $this->label); } $image_path = $image_path . '/qrv' . $qrcode_version . '.png'; $base_image = imagecreatefrompng($image_path); $col[1] = imagecolorallocate($base_image, 0, 0, 0); $col[0] = imagecolorallocate($base_image, 255, 255, 255); $i = 4; $mxe = 4 + $max_modules_1side; $ii = 0; while ($i < $mxe) { $j = 4; $jj = 0; while ($j < $mxe) { if ($matrix_content[$ii][$jj] & $mask_content) { imagesetpixel($base_image, $i, $j, $col[1]); } ++$j; ++$jj; } ++$i; ++$ii; } imagecopyresampled($output_image, $base_image, $this->padding, $this->padding, 4, 4, $this->size, $this->size, $mib - 8, $mib - 8); $imagecolorset_function = new ReflectionFunction('imagecolorset'); $allow_alpha = $imagecolorset_function->getNumberOfParameters() == 6; if ($this->color_background != null) { $index = imagecolorclosest($output_image, 255, 255, 255); if ($allow_alpha) { imagecolorset($output_image, $index, $this->color_background['r'], $this->color_background['g'], $this->color_background['b'], $this->color_background['a']); } else { imagecolorset($output_image, $index, $this->color_background['r'], $this->color_background['g'], $this->color_background['b']); } } if ($this->color_foreground != null) { $index = imagecolorclosest($output_image, 0, 0, 0); if ($allow_alpha) { imagecolorset($output_image, $index, $this->color_foreground['r'], $this->color_foreground['g'], $this->color_foreground['b'], $this->color_foreground['a']); } else { imagecolorset($output_image, $index, $this->color_foreground['r'], $this->color_foreground['g'], $this->color_foreground['b']); } } $this->image = $output_image; }
function _AdjBrightContrast($img, $bright, $contr = 0) { if ($bright < -1 || $bright > 1 || $contr < -1 || $contr > 1) { JpGraphError::Raise(" Parameters for brightness and Contrast out of range [-1,1]"); } $nbr = imagecolorstotal($img); for ($i = 0; $i < $nbr; ++$i) { $colarr = imagecolorsforindex($img, $i); $r = $this->AdjRGBBrightContrast($colarr["red"], $bright, $contr); $g = $this->AdjRGBBrightContrast($colarr["green"], $bright, $contr); $b = $this->AdjRGBBrightContrast($colarr["blue"], $bright, $contr); imagecolorset($img, $i, $r, $g, $b); } }
function generate($width, $height = 0) { /* First check paths */ $this->path = rtrim($this->path, '/') . '/'; $this->cache = rtrim($this->cache, '/') . '/'; $this->file = str_replace('/', '', $this->file); if (!is_dir($this->path) || $this->path === '/') { return $this->raiseError('path = "' . $this->path . '" is not a directory or is "/"'); } if (!is_dir($this->cache) || $this->cache === '/') { return $this->raiseError('cache = "' . $this->cache . '" is not a directory or is "/"'); } if (strpos($this->file, '%d') === false) { return $this->raiseError('file = "' . $this->file . '" doesn\'t include a \'%d\' for image number'); } $files = array('filenames' => array(), 'absolutes' => array()); /* Generated files list */ $this->startStep = (int) floor(($this->step - 1) / 2); $nbOfImages = 1; /* Will be modified after the first image is created */ $this->maxClicks = 1; /* Must not be zero for divisions */ $this->maxY = 0; /** * Memory consumption: * imagecreate : about 200,000 + 5 * $width * $height bytes * dots : about 6,000 + 360 * DOT_WIDTH bytes each (100 dots) * imagepng : about 4 * $width * $height bytes * So a rough idea of the memory is 10 * $width * $height + 500,000 (2 images) + 100 * (DOT_WIDTH * 360 + 6000) * */ $this->width = (int) abs($width); if ($this->width === 0) { return $this->raiseError('Width can\'t be 0'); } $height = (int) abs($height); if ($height === 0) { /* Calculating height from memory consumption, and add a 100% security margin: 10 => 20 */ $this->height = floor(($this->memory - 500000 - 100 * ($this->dot * 360 + 6000)) / (20 * $width)); /* Limit height to 1000px max, with a modulo of 10 */ $this->height = (int) max(100, min(1000, $this->height - $this->height % 10)); } else { /* Force height */ $this->height = $height; } /* Startup tasks */ if ($this->startDrawing() === false) { return false; } $files['width'] = $this->width; $files['height'] = $this->height; for ($image = 0; $image < $nbOfImages; $image++) { /* Image creation */ $this->image = imagecreatetruecolor($this->width, $this->height); if ($this->heatmap === false) { $grey = imagecolorallocate($this->image, $this->__grey, $this->__grey, $this->__grey); imagefill($this->image, 0, 0, $grey); } else { /* Image is filled in the color "0", which means 0 click */ imagefill($this->image, 0, 0, 0); } /* Draw next pixels for this image */ if ($this->drawPixels($image) === false) { return false; } if ($image === 0) { if ($this->maxY === 0) { if (defined('LANG_ERROR_DATA') === true) { return $this->raiseError(LANG_ERROR_DATA); } else { $this->maxY = 1; } } $nbOfImages = (int) ceil($this->maxY / $this->height); $files['count'] = $nbOfImages; } if ($this->heatmap === true) { imagepng($this->image, sprintf($this->cache . $this->file . '_temp', $image)); } else { /* "No clicks under this line" message */ if ($image === $nbOfImages - 1 && defined('LANG_NO_CLICK_BELOW') === true) { $black = imagecolorallocate($this->image, 0, 0, 0); imageline($this->image, 0, $this->height - 1, $this->width, $this->height - 1, $black); imagestring($this->image, 1, 1, $this->height - 9, LANG_NO_CLICK_BELOW, $black); } imagepng($this->image, sprintf($this->path . $this->file, $image)); } imagedestroy($this->image); /* Result files */ $files['filenames'][] = sprintf($this->file, $image); $files['absolutes'][] = sprintf($this->path . $this->file, $image); } /* End tasks */ if ($this->finishDrawing() === false) { return false; } if ($this->heatmap === false) { return $files; } /* Now, our image is a direct representation of the clicks on each pixel, so create some fuzzy dots to put a nice blur effect if user asked for a heatmap */ for ($i = 0; $i < 128; $i++) { $dots[$i] = imagecreatetruecolor($this->dot, $this->dot); imagealphablending($dots[$i], false); } for ($x = 0; $x < $this->dot; $x++) { for ($y = 0; $y < $this->dot; $y++) { $sinX = sin($x * pi() / $this->dot); $sinY = sin($y * pi() / $this->dot); for ($i = 0; $i < 128; $i++) { $alpha = 127 - $i * $sinX * $sinY * $sinX * $sinY; imagesetpixel($dots[$i], $x, $y, (int) $alpha * 16777216); } } } /** * Colors creation: * grey => deep blue (rgB) => light blue (rGB) => green (rGb) => yellow (RGb) => red (Rgb) * 0 $this->__colors[0] $this->__colors[1] $this->__colors[2] $this->__colors[3] 128 */ sort($this->__colors); $colors = array(); for ($i = 0; $i < 128; $i++) { /* Red */ if ($i < $this->__colors[0]) { $colors[$i][0] = $this->__grey + ($this->__low - $this->__grey) * $i / $this->__colors[0]; } elseif ($i < $this->__colors[2]) { $colors[$i][0] = $this->__low; } elseif ($i < $this->__colors[3]) { $colors[$i][0] = $this->__low + ($this->__high - $this->__low) * ($i - $this->__colors[2]) / ($this->__colors[3] - $this->__colors[2]); } else { $colors[$i][0] = $this->__high; } /* Green */ if ($i < $this->__colors[0]) { $colors[$i][1] = $this->__grey + ($this->__low - $this->__grey) * $i / $this->__colors[0]; } elseif ($i < $this->__colors[1]) { $colors[$i][1] = $this->__low + ($this->__high - $this->__low) * ($i - $this->__colors[0]) / ($this->__colors[1] - $this->__colors[0]); } elseif ($i < $this->__colors[3]) { $colors[$i][1] = $this->__high; } else { $colors[$i][1] = $this->__high - ($this->__high - $this->__low) * ($i - $this->__colors[3]) / (127 - $this->__colors[3]); } /* Blue */ if ($i < $this->__colors[0]) { $colors[$i][2] = $this->__grey + ($this->__high - $this->__grey) * $i / $this->__colors[0]; } elseif ($i < $this->__colors[1]) { $colors[$i][2] = $this->__high; } elseif ($i < $this->__colors[2]) { $colors[$i][2] = $this->__high - ($this->__high - $this->__low) * ($i - $this->__colors[1]) / ($this->__colors[2] - $this->__colors[1]); } else { $colors[$i][2] = $this->__low; } } for ($image = 0; $image < $nbOfImages; $image++) { $img = imagecreatetruecolor($this->width, $this->height); $white = imagecolorallocate($img, 255, 255, 255); /* «imagefill» doesn't work correctly on some hosts, ending on a red drawing */ imagefilledrectangle($img, 0, 0, $this->width - 1, $this->height - 1, $white); imagealphablending($img, true); $imgSrc = @imagecreatefrompng(sprintf($this->cache . $this->file . '_temp', $image)); @unlink(sprintf($this->cache . $this->file . '_temp', $image)); if ($imgSrc === false) { return $this->raiseError('::MEMORY_OVERFLOW::'); } for ($x = $this->startStep; $x < $this->width; $x += $this->step) { for ($y = $this->startStep; $y < $this->height; $y += $this->step) { $dot = (int) ceil(imagecolorat($imgSrc, $x, $y) / $this->maxClicks * 100); if ($dot !== 0) { imagecopy($img, $dots[$dot], ceil($x - $this->dot / 2), ceil($y - $this->dot / 2), 0, 0, $this->dot, $this->dot); } } } /* Destroy image source */ imagedestroy($imgSrc); /* Rainbow */ if ($image === 0 && $this->rainbow === true) { for ($i = 1; $i < 128; $i += 2) { /* Erase previous alpha channel so that clicks don't change the heatmap by combining their alpha */ imageline($img, ceil($i / 2), 0, ceil($i / 2), 10, 16777215); /* Then put our alpha */ imageline($img, ceil($i / 2), 0, ceil($i / 2), 10, (127 - $i) * 16777216); } } if ($this->alpha === 0) { /* Some version of imagetruecolortopalette() don't transform alpha value to non alpha */ if ($this->palette === true) { for ($x = 0; $x < $this->width; $x++) { for ($y = 0; $y < $this->height; $y++) { /* Get Alpha value (0->127) and transform it to red (divide color by 16777216 and multiply by 65536 * 2 (red is 0->255), so divide it by 128) */ imagesetpixel($img, $x, $y, (imagecolorat($img, $x, $y) & 0x7f000000) / 128); } } } /* Change true color image to palette then change palette colors */ imagetruecolortopalette($img, false, 127); for ($i = 0, $max = imagecolorstotal($img); $i < $max; $i++) { $color = imagecolorsforindex($img, $i); imagecolorset($img, $i, $colors[floor(127 - $color['red'] / 2)][0], $colors[floor(127 - $color['red'] / 2)][1], $colors[floor(127 - $color['red'] / 2)][2]); } } else { /* Need some transparency, really? So we have to deal with each and every pixel */ imagealphablending($img, false); imagesavealpha($img, true); for ($x = 0; $x < $this->width; $x++) { for ($y = 0; $y < $this->height; $y++) { /* Get blue value (0->255), divide it by 2, and apply transparency + colors */ $blue = floor((imagecolorat($img, $x, $y) & 0xff) / 2); imagesetpixel($img, $x, $y, floor($this->alpha * $blue / 127) * 16777216 + $colors[127 - $blue][0] * 65536 + $colors[127 - $blue][1] * 256 + $colors[127 - $blue][2]); } } } $grey = imagecolorallocate($img, $this->__grey, $this->__grey, $this->__grey); $gray = imagecolorallocate($img, ceil($this->__grey / 2), ceil($this->__grey / 2), ceil($this->__grey / 2)); $white = imagecolorallocate($img, 255, 255, 255); $black = imagecolorallocate($img, 0, 0, 0); /* maxClicks */ if ($image === 0 && $this->rainbow === true) { imagerectangle($img, 0, 0, 65, 11, $white); imagefilledrectangle($img, 0, 11, 65, 18, $white); imagestring($img, 1, 0, 11, '0', $black); $right = 66 - strlen($this->maxClicks) * 5; imagestring($img, 1, $right, 11, $this->maxClicks, $black); imagestring($img, 1, floor($right / 2) - 12, 11, 'clicks', $black); } if ($image === $nbOfImages - 1) { /* "No clicks under this line" message */ if (defined('LANG_NO_CLICK_BELOW') === true) { imageline($img, 0, $this->height - 1, $this->width, $this->height - 1, $gray); imagestring($img, 1, 1, $this->height - 9, LANG_NO_CLICK_BELOW, $gray); } /* Copyleft */ if ($this->copyleft === true) { imagestring($img, 1, $this->width - 160, $this->height - 9, 'Open source heatmap by ClickHeat', $grey); imagestring($img, 1, $this->width - 161, $this->height - 9, 'Open source heatmap by ClickHeat', $gray); } } /* Save PNG file */ imagepng($img, sprintf($this->path . $this->file, $image)); imagedestroy($img); } for ($i = 0; $i < 100; $i++) { imagedestroy($dots[$i]); } return $files; }