public function get_pixel($x, $y) { $pixel = $this->image->getImagePixelColor($x, $y); $color = $pixel->getColor(); $normalized_color = $pixel->getColor(true); return array('color' => ($color['r'] << 16) + ($color['g'] << 8) + $color['b'], 'opacity' => $normalized_color['a']); }
public function getPixel($x, $y) { $pixel = $this->image->getImagePixelColor($x, $y); $color = $pixel->getColor(); $normalizedColor = $pixel->getColor(true); $color = ($color['r'] << 16) + ($color['g'] << 8) + $color['b']; $opacity = $normalizedColor['a']; return $this->buildPixel($x, $y, $color, $opacity); }
/** * Helper assertion for testing alpha on images using Imagick * * @param string $image_path * @param array $point array(x,y) * @param int $expected */ protected function assertImageAlphaAtPointImagick($image_path, $point, $expected) { $im = new Imagick($image_path); $pixel = $im->getImagePixelColor($point[0], $point[1]); $color = $pixel->getColorValue(imagick::COLOR_ALPHA); $this->assertEquals($expected, $color); }
/** * {@inheritdoc} */ public function getColorAt(PointInterface $point) { if (!$point->in($this->getSize())) { throw new RuntimeException(sprintf('Error getting color at point [%s,%s]. The point must be inside the image of size [%s,%s]', $point->getX(), $point->getY(), $this->getSize()->getWidth(), $this->getSize()->getHeight())); } $pixel = $this->imagick->getImagePixelColor($point->getX(), $point->getY()); return new Color(array($pixel->getColorValue(\Imagick::COLOR_RED) * 255, $pixel->getColorValue(\Imagick::COLOR_GREEN) * 255, $pixel->getColorValue(\Imagick::COLOR_BLUE) * 255), (int) round($pixel->getColorValue(\Imagick::COLOR_ALPHA) * 100)); }
protected function _getPixel($x, $y) { $pixel = $this->im->getImagePixelColor($x, $y); $result = array_values($pixel->getColor(true)); if (!isset($result[3])) { $result[3] = 1; } return $result; }
/** * @param \Imagick $imagick * @return float */ private function getAverageLuminosity(\Imagick $imagick) { $colors = 0; foreach (range(1, self::IMG_SIZE) as $x) { foreach (range(1, self::IMG_SIZE) as $y) { $rgb = $imagick->getImagePixelColor($x, $y)->getColor(); $colors += ($rgb['r'] + $rgb['g'] + $rgb['b']) / 3; } } return $colors / self::IMG_SIZE ** 2; }
/** * {@inheritdoc} */ public function getColorAt(PointInterface $point) { if (!$point->in($this->getSize())) { throw new RuntimeException(sprintf('Error getting color at point [%s,%s]. The point must be inside the image of size [%s,%s]', $point->getX(), $point->getY(), $this->getSize()->getWidth(), $this->getSize()->getHeight())); } try { $pixel = $this->imagick->getImagePixelColor($point->getX(), $point->getY()); } catch (\ImagickException $e) { throw new RuntimeException('Error while getting image pixel color', $e->getCode(), $e); } return $this->pixelToColor($pixel); }
/** * Image caching * * Resize & cache image into the file system. Returns the template image if product image is not exists * * @param mixed $name * @param int $user_id * @param int $width Resizing width * @param int $height Resizing height * @param bool $watermarked * @param bool $overwrite * @param bool $best_fit * @return string Cached Image URL */ public function image($name, $user_id, $width, $height, $watermarked = false, $overwrite = false, $best_fit = false) { $storage = DIR_STORAGE . $user_id . DIR_SEPARATOR . $name . '.' . STORAGE_IMAGE_EXTENSION; $cache = DIR_IMAGE . 'cache' . DIR_SEPARATOR . $user_id . DIR_SEPARATOR . $name . '-' . (int) $best_fit . '-' . $width . '-' . $height . '.' . STORAGE_IMAGE_EXTENSION; $watermark_black = DIR_IMAGE . 'common' . DIR_SEPARATOR . 'watermark-black.png'; $watermark_white = DIR_IMAGE . 'common' . DIR_SEPARATOR . 'watermark-white.png'; $cached_url = URL_BASE . 'image' . DIR_SEPARATOR . 'cache' . DIR_SEPARATOR . $user_id . DIR_SEPARATOR . $name . '-' . (int) $best_fit . '-' . $width . '-' . $height . '.' . STORAGE_IMAGE_EXTENSION; // Force reset if ($overwrite && file_exists($overwrite)) { unlink($cache); } // If image is cached if (file_exists($cache)) { return $cached_url; // If image not cached } else { // Create directories by path if not exists $directories = explode(DIR_SEPARATOR, $cache); $path = ''; foreach ($directories as $directory) { $path .= DIR_SEPARATOR . $directory; if (!is_dir($path) && false === strpos($directory, '.')) { mkdir($path, 0755); } } // Prepare new image $image = new Image($storage); $image->resize($width, $height, 1, false, $best_fit); if ($watermarked) { $average = new Imagick($storage); $average->resizeImage(1, 1, Imagick::FILTER_POINT, 0); $pixel = $average->getImagePixelColor(1, 1); $color = $pixel->getColor(); $brightness = (0.299 * $color['r'] + 0.587 * $color['g'] + 0.114 * $color['b']) * 100 / 255; if ($brightness < 25) { $image->watermark($watermark_white); } else { $image->watermark($watermark_black); } } $image->save($cache); } return $cached_url; }
/** * @return bool */ protected function hasAlphaChannel() { if ($this->isAlphaPossible) { $width = $this->resource->getImageWidth(); // Get the width of the image $height = $this->resource->getImageHeight(); // Get the height of the image // We run the image pixel by pixel and as soon as we find a transparent pixel we stop and return true. for ($i = 0; $i < $width; $i++) { for ($j = 0; $j < $height; $j++) { $pixel = $this->resource->getImagePixelColor($i, $j); $color = $pixel->getColor(true); // get the real alpha not just 1/0 if ($color["a"] < 1) { // if there's an alpha pixel, return true return true; } } } } // If we dont find any pixel the function will return false. return false; }
public function index() { //exit; ini_set("max_execution_time", -1); $dir = new DirectoryIterator("swatch"); foreach ($dir as $fl) { if (!$fl->isDot()) { if (!$fl->isDir()) { $name = $fl->getFilename(); $image = new Imagick('swatch/' . $name); $x = rand(4, 10); $y = rand(4, 10); $pixel = $image->getImagePixelColor($x, $y); $clr = $pixel->getColor(); $hex = $this->fromRGB($clr['r'], $clr['g'], $clr['b']); $tn = explode("_", $name); $tn = explode(".", $tn[1]); $tn = $tn[0]; rename('swatch/' . $name, 'swatch/' . $tn . "-" . $hex . ".png"); } } } exit; }
/** * * @ticket 30596 */ public function test_image_preserves_alpha_on_rotate() { $file = DIR_TESTDATA . '/images/transparent.png'; $pre_rotate_editor = new Imagick($file); $pre_rotate_pixel = $pre_rotate_editor->getImagePixelColor(0, 0); $pre_rotate_alpha = $pre_rotate_pixel->getColorValue(imagick::COLOR_ALPHA); $save_to_file = tempnam(get_temp_dir(), '') . '.png'; $pre_rotate_editor->writeImage($save_to_file); $pre_rotate_editor->destroy(); $image_editor = new WP_Image_Editor_Imagick($save_to_file); $image_editor->load(); $this->assertNotInstanceOf('WP_Error', $image_editor); $image_editor->rotate(180); $image_editor->save($save_to_file); $this->assertImageAlphaAtPointImagick($save_to_file, array(0, 0), $pre_rotate_alpha); unlink($save_to_file); }
<?php $im = new Imagick("magick:logo"); $im->setImageBackgroundColor("pink"); $im->thumbnailImage(200, 200, true, true); $color = $im->getImagePixelColor(5, 5); if ($color->isPixelSimilar("pink", 0)) { echo "Similar" . PHP_EOL; } else { var_dump($color->getColorAsString()); } $color = $im->getImagePixelColor(199, 5); if ($color->isPixelSimilar("pink", 0)) { echo "Similar" . PHP_EOL; } else { var_dump($color->getColorAsString()); }
/** * @Given /^the pixel at coordinate "([^"]*)" should have a color of "#([^"]*)"$/ */ public function assertImagePixelColor($coordinates, $expectedColor) { $coordinates = array_map('trim', explode(',', $coordinates)); $coordinates = array_map('intval', $coordinates); $expectedColor = strtolower($expectedColor); $imagick = new \Imagick(); $imagick->readImageBlob((string) $this->getLastResponse()->getBody()); $pixel = $imagick->getImagePixelColor($coordinates[0], $coordinates[1]); $color = $pixel->getColor(); $toHex = function ($col) { return str_pad(dechex($col), 2, '0', STR_PAD_LEFT); }; $hexColor = $toHex($color['r']) . $toHex($color['g']) . $toHex($color['b']); assertSame($expectedColor, $hexColor, 'Incorrect color at coordinate ' . implode(', ', $coordinates) . ', expected ' . $expectedColor . ', got ' . $hexColor); }
/** * Fills an image area * * @param Imagick $image * @param int $x * @param int $y * @param color $color * @return bool */ function zp_imageFill($image, $x, $y, $color) { $target = $image->getImagePixelColor($x, $y); return $image->floodFillPaintImage($color, 1, $target, $x, $y, false); }
/** * Applies the image effect on an Imagick object. This function should be overridden by the effect. * @param \Imagick The image object to work with * @return \Imagick The new image object to use */ protected function executeFilterImagick(\Imagick $imageData) { $imagePixel = $imageData->getImagePixelColor($this->pixelX, $this->pixelY); $imageData->paintTransparentImage($imagePixel, 0, $this->fuzzyNess); return $imageData; }
function kingBar($a) { $line_1 = $a['line_1']; $line_2 = $a['line_2']; $line_3 = $a['line_3']; $line_4 = $a['line_4']; $line_1_font = $a['line_1_font']; $line_2_font = $a['line_2_font']; $line_3_font = $a['line_3_font']; $line_4_font = $a['line_4_font']; $line_1_size = $a['line_1_size']; $line_2_size = $a['line_2_size']; $line_3_size = $a['line_3_size']; $line_4_size = $a['line_4_size']; $line_1_x = $a['line_1_x']; $line_2_x = $a['line_2_x']; $line_3_x = $a['line_3_x']; $line_4_x = $a['line_4_x']; $line_1_y = $a['line_1_y']; $line_2_y = $a['line_2_y']; $line_3_y = $a['line_3_y']; $line_4_y = $a['line_4_y']; $base = new \Imagick('/var/www/mch' . $a['base']); $draw = new \ImagickDraw(); $fillColor = $base->getImagePixelColor(0, 0); $textColor = "rgb(126, 64, 17)"; if (isset($a['layer_top']) && $a['layer_top'] != "") { $layout = new \Imagick(); $layout_x = $a['layer_top_x']; $layout_y = $a['layer_top_y']; if ($a['image_uploaded'] != true) { $img = '/var/www/mch/' . $a['layer_top']; $blob = base64_decode(convert($img)); $layout->readImageBlob($blob); $layout->setImageFormat('png'); } else { $layout->readImage($img); } } if ($line_1 != "" || $line_2 != "" || $line_3 != "" || $line_4 != "") { // $line_1_x = $line_1_x + 25; $line_2_x = $line_2_x + 25; $line_3_x = $line_3_x + 25; $line_4_x = $line_4_x + 25; $line_1_x = $line_1_x < 70 ? 70 : $line_1_x; $line_1_x = $line_1_x > 196 ? 197 + 20 : $line_1_x; $line_2_x = $line_2_x < 70 ? 70 : $line_2_x; $line_2_x = $line_2_x > 196 ? 197 + 20 : $line_2_x; $line_3_x = $line_3_x < 70 ? 70 : $line_3_x; $line_3_x = $line_3_x > 196 ? 197 + 20 : $line_3_x; $line_4_x = $line_4_x < 70 ? 70 : $line_4_x; $line_4_x = $line_4_x > 196 ? 197 + 20 : $line_4_x; $size = getimagesize('/var/www/mch' . $a['base']); $w = $size[0]; $h = $size[1]; $tras = new Imagick(); $mask = new Imagick(); $tras->newImage($w, $h, new ImagickPixel('grey30')); $mask->newImage($w, $h, new ImagickPixel('black')); // $tras->paintTransparentImage(new ImagickPixel('black'), 0.4, 0); $draw->setFont($line_1_font); $draw->setFontSize($line_1_size); $draw->setFontWeight(700); $draw->setFillColor($textColor); $tras->annotateImage($draw, $line_1_x - 1, $line_1_y - 1, 0, $line_1); $draw->setFillColor('white'); $mask->annotateImage($draw, $line_1_x, $line_1_y, 0, $line_1); $mask->annotateImage($draw, $line_1_x - 1, $line_1_y - 1, 0, $line_1); $draw->setFillColor('black'); $mask->annotateImage($draw, $line_1_x - 2, $line_1_y - 2, 0, $line_1); $draw->setFont($line_2_font); $draw->setFontSize($line_2_size); $draw->setFontWeight(700); $draw->setFillColor($textColor); $tras->annotateImage($draw, $line_2_x - 1, $line_2_y - 1, 0, $line_2); $draw->setFillColor('white'); $mask->annotateImage($draw, $line_2_x, $line_2_y, 0, $line_2); $mask->annotateImage($draw, $line_2_x - 1, $line_2_y - 1, 0, $line_2); $draw->setFillColor('black'); $mask->annotateImage($draw, $line_2_x - 2, $line_2_y - 2, 0, $line_2); $draw->setFont($line_3_font); $draw->setFontSize($line_3_size); $draw->setFontWeight(700); $draw->setFillColor($textColor); $tras->annotateImage($draw, $line_3_x - 1, $line_3_y - 1, 0, $line_3); $draw->setFillColor('white'); $mask->annotateImage($draw, $line_3_x, $line_3_y, 0, $line_3); $mask->annotateImage($draw, $line_3_x - 1, $line_3_y - 1, 0, $line_3); $draw->setFillColor('black'); $mask->annotateImage($draw, $line_3_x - 2, $line_3_y - 2, 0, $line_3); $draw->setFont($line_4_font); $draw->setFontSize($line_4_size); $draw->setFontWeight(700); $draw->setFillColor($textColor); $tras->annotateImage($draw, $line_4_x - 1, $line_4_y - 1, 0, $line_4); $draw->setFillColor('white'); $mask->annotateImage($draw, $line_4_x, $line_4_y, 0, $line_4); $mask->annotateImage($draw, $line_4_x - 1, $line_4_y - 1, 0, $line_4); $draw->setFillColor('black'); $mask->annotateImage($draw, $line_4_x - 2, $line_4_y - 2, 0, $line_4); $mask->setImageMatte(false); $tras->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0); $base->compositeImage($tras, Imagick::COMPOSITE_DISSOLVE, 0, 0); } echo "line 1x : " . $line_1_x . " line 2x : " . $line_2_x . " line 3x : " . $line_3_x . " line 4x: " . $line_4_x; // $img = '/var/www/mch'.$a['layout']; // $size = getimagesize($a['img']); // $h = $size[1]; // $w = $size[0]; $h = 100; $w = 100; $x = $layout_x; $y = $layout_y; // echo $x." "; // echo $y; if ($x < 134) { $x = 134; } if ($x > 186) { $x = 186; } if ($y < 27) { $y = 27; } if ($y > 84) { $y = 84; } $date = date('Y_m_d_H_i_s'); if (isset($a['layer_top']) && $a['layer_top'] != "") { $layout->resizeImage($w, $h, Imagick::FILTER_LANCZOS, 1); $base->compositeImage($layout, Imagick::COMPOSITE_OVERLAY, $x, $y); } $bar = $base->writeImage('/var/www/mch/public/test_bar.png'); $base->destroy(); if ($bar) { $barpath = "test_bar.png"; // echo "x-axis : ".$x." y-axis : ".$y; echo "<img src='{$barpath}'>"; return $bar; } else { return false; } }
/** * Test the image created with WP_Image_Editor_Imagick preserves alpha with no resizing etc * * @ticket 24871 */ public function test_image_preserves_alpha() { $file = DIR_TESTDATA . '/images/transparent.png'; $editor = wp_get_image_editor( $file ); $this->assertNotInstanceOf( 'WP_Error', $editor ); $editor->load(); $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; $editor->save( $save_to_file ); $im = new Imagick( $save_to_file ); $pixel = $im->getImagePixelColor( 0, 0 ); $expected = $pixel->getColorValue( imagick::COLOR_ALPHA ); $this->assertImageAlphaAtPointImagick( $save_to_file, array( 0,0 ), $expected ); unlink( $save_to_file ); }
private function _isLightImage($imageData) { $image = new Imagick(); $image->readImageBlob($imageData['contents']); $max = $image->getQuantumRange(); $max = $max["quantumRangeLong"]; $image->setImageType(Imagick::IMGTYPE_GRAYSCALEMATTE); $float = 0.5; $image->thresholdImage($float * $max, 255); $image->setBackgroundColor('white'); $black = 0; $white = 0; for ($x = 0; $x < $image->getImageWidth(); $x++) { for ($y = 0; $y < $image->getImageHeight(); $y++) { $pixel = $image->getImagePixelColor($x, $y); $value = $pixel->getColor(); if ($value['r'] == 0 && $value['g'] == 0 && $value['b'] == 0) { $black++; } else { $white++; } } } return $white > $black; }
public function generateImage($dataString, $ecc = 'M', $version = 0, $structure_n = NULL, $structure_m = NULL, $parity = NULL, $originaldata = NULL) { $dataString = rawurldecode($dataString); $data_length = strlen($dataString); if ($data_length <= 0) { trigger_error("QRcode : Empty data string.", E_USER_ERROR); exit; } $data_counter = 0; if ($structure_n > 1 && $structure_n <= 16 && $structure_m > 0 && $structure_m <= 16) { $data_value[0] = 3; $data_bits[0] = 4; $data_value[1] = $structure_m - 1; $data_bits[1] = 4; $data_value[2] = $structure_n - 1; $data_bits[2] = 4; $originaldata_length = strlen($originaldata); if ($originaldata_length > 1) { $parity = 0; $i = 0; while ($i < $originaldata_length) { $parity = $parity ^ ord(substr($originaldata, $i, 1)); $i++; } } $data_value[3] = $parity; $data_bits[3] = 8; $data_counter = 4; } $data_bits[$data_counter] = 4; /* --- determine encode mode */ if (preg_match('/[^0-9]/', dataString)) { if (preg_match('#[^0-9A-Z $*%+\\-./:]#', $dataString)) { /* --- 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($dataString, $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($dataString, $i, 1)]; $data_bits[$data_counter] = 6; } else { $data_value[$data_counter] = $data_value[$data_counter] * 45 + $alphanumeric_character_hash[substr($dataString, $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($dataString, $i, 1); $data_bits[$data_counter] = 4; } else { $data_value[$data_counter] = $data_value[$data_counter] * 10 + substr($dataString, $i, 1); if ($i % 3 == 1) { $data_bits[$data_counter] = 7; } else { $data_bits[$data_counter] = 10; $data_counter++; } } $i++; } } if (@$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"); $ec = @$ecc_character_hash[$ecc]; if (!$ec) { $ec = 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); /* #--- auto version select */ if (!$version) { $i = 1 + 40 * $ec; $j = $i + 39; $version = 1; while ($i <= $j) { if ($max_data_bits_array[$i] >= $total_data_bits + $codeword_num_plus[$version]) { $max_data_bits = $max_data_bits_array[$i]; break; } $i++; $version++; } } else { $max_data_bits = $max_data_bits_array[$version + 40 * $ec]; } if ($version > $this->versionUpperLimit) { trigger_error("QRcode : too large version.", E_USER_ERROR); } $total_data_bits += $codeword_num_plus[$version]; $data_bits[$codeword_num_counter_value] += $codeword_num_plus[$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[$version]; $max_modules_1side = 17 + ($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[$version] + ($max_codewords << 3); $filename = $this->dataPath . "/qrv" . $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 = $this->dataPath . "/rsc" . $rs_ecc_codewords . ".dat"; $fp0 = fopen($filename, "rb"); $i = 0; 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) { if ($version < $this->versionUpperLimit) { return $this->generateImage($dataString, $ecc, $version + 1, $structure_n, $structure_m, $parity, $originaldata); } else { trigger_error("QRcode : Overflow error", E_USER_ERROR); exit; } } } } /* ----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 */ $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[$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; 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; $this->imagePath = $this->imagePath . "/qrv" . $version . ".png"; $image = new \Imagick($this->imagePath); $draw = new \ImagickDraw(); if ($this->fillColor != "black") { $clut = new \Imagick(); $clut->newImage(1, 1, new \ImagickPixel($this->fillColor)); $image->clutImage($clut); $clut->destroy(); unset($clut); } $draw->setFillColor(new \ImagickPixel($this->fillColor)); $draw->setStrokeColor(new \ImagickPixel($this->strokeColor)); $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) { $draw->point($i, $j); } $j++; $jj++; } $i++; $ii++; } $image->drawImage($draw); if ($this->blockImage) { $blockSize = $this->blockImage->getImageWidth(); $image->resizeImage($mib * $blockSize, $mib * $blockSize, \Imagick::FILTER_POINT, 1); $testPixel = new \ImagickPixel($this->fillColor); for ($x = 0; $x < $image->getImageWidth(); $x += $blockSize) { for ($y = 0; $y < $image->getImageHeight(); $y += $blockSize) { if ($testPixel->isSimilar($image->getImagePixelColor($x, $y), 1)) { $image->compositeImage($this->blockImage, $this->overlayBlockImage ? \Imagick::COMPOSITE_OVER : \Imagick::COMPOSITE_REPLACE, $x, $y); } } } } $geo = $image->getImageGeometry(); if (!empty($this->width) && $geo['width'] <= $this->width) { if (!$this->width) { $this->width = 0; } else { if ($this->width < $image->getImageWidth()) { $this->width = $image->getImageWidth(); } } $remaining_pixels = $this->width % $geo['width']; $fit_width = $this->width - $remaining_pixels; $image->resizeImage($fit_width, $fit_width, \Imagick::FILTER_POINT, 1); //FILTER_POINT $tmp = new \Imagick(); $tmp->newImage($this->width, $this->width, 'none', 'png'); $tmp->compositeImage($image, \Imagick::COMPOSITE_OVER, floor($remaining_pixels / 2), floor($remaining_pixels / 2)); $image->destroy(); $image =& $tmp; } return $image; }
/** * Get the pixel info for given coordinates, from the image returned in the previous response * * @param string $coordinates * @return array */ private function getImagePixelInfo($coordinates) { $coordinates = array_map('trim', explode(',', $coordinates)); $coordinates = array_map('intval', $coordinates); $imagick = new \Imagick(); $imagick->readImageBlob((string) $this->getLastResponse()->getBody()); $pixel = $imagick->getImagePixelColor($coordinates[0], $coordinates[1]); $color = $pixel->getColor(); $toHex = function ($col) { return str_pad(dechex($col), 2, '0', STR_PAD_LEFT); }; $hexColor = $toHex($color['r']) . $toHex($color['g']) . $toHex($color['b']); return ['color' => $hexColor, 'alpha' => (double) $pixel->getColorValue(\Imagick::COLOR_ALPHA)]; }
public function brightness($n, $s_x = 0, $e_x = 0, $s_y = 0, $e_y = 0) { $info = $this->_handle->getImageGeometry(); $w = $info["width"]; $h = $info["height"]; $format = $this->_handle->getImageFormat(); if ($s_x == 0 && $s_y == 0 && $e_x == 0 && $e_y == 0) { $e_x = $w; $e_y = $h; } $image = new \Imagick(); $image->newImage($w, $h, "transparent"); $draw = new \ImagickDraw(); for ($x = 0; $x < $w; $x++) { for ($y = 0; $y < $h; $y++) { $p = $image->getImagePixelColor($x, $y); $rgb = $p->getColor(); if ($x >= $s_x && $x < $e_x && $y >= $s_y && $y < $e_y) { $rgb["r"] = $rgb["r"] + $rgb["r"] * $n; $rgb["g"] = $rgb["g"] + $rgb["g"] * $n; $rgb["b"] = $rgb["b"] + $rgb["b"] * $n; $rgb["r"] = min(255, $rgb["r"]); $rgb["r"] = max(0, $rgb["r"]); $rgb["g"] = min(255, $rgb["g"]); $rgb["g"] = max(0, $rgb["g"]); $rgb["b"] = min(255, $rgb["b"]); $rgb["b"] = max(0, $rgb["b"]); } $p->setColor("rgb({$rgb["r"]},{$rgb["g"]},{$rgb["b"]})"); $draw->setFillColor($p); $draw->point($x, $y); } } $image->drawImage($draw); $image->setImageFormat($format); $this->_handle = $image; $draw->destroy(); }
/** * Fills an image area * * @internal Imagick::floodFillPaintImage() requires Imagick 2.3.0b1+ compiled against ImageMagick 6.3.8+ * * @param Imagick $image * @param int $x * @param int $y * @param color $color * @return bool */ function zp_imageFill($image, $x, $y, $color) { global $_use_imagick_deprecated; if ($_use_imagick_deprecated) { return $image->paintFloodfillImage($color, 1, $color, $x, $y); } $target = $image->getImagePixelColor($x, $y); return $image->floodFillPaintImage($color, 1, $target, $x, $y, false); }
/** * Load actual image pixels from Imagick object * * @param Imagick $im Image to load from */ public function readImageFromImagick(Imagick $im) { /* Threshold */ $im->setImageType(Imagick::IMGTYPE_TRUECOLOR); // Remove transparency (good for PDF's) $max = $im->getQuantumRange(); $max = $max["quantumRangeLong"]; $im->thresholdImage(0.5 * $max); /* Make a string of 1's and 0's */ $geometry = $im->getimagegeometry(); $this->imgHeight = $im->getimageheight(); $this->imgWidth = $im->getimagewidth(); $this->imgData = str_repeat("", $this->imgHeight * $this->imgWidth); for ($y = 0; $y < $this->imgHeight; $y++) { for ($x = 0; $x < $this->imgWidth; $x++) { /* Faster to average channels, blend alpha and negate the image here than via filters (tested!) */ $cols = $im->getImagePixelColor($x, $y); $cols = $cols->getcolor(); $greyness = (int) (($cols['r'] + $cols['g'] + $cols['b']) / 3) >> 7; // 1 for white, 0 for black $this->imgData[$y * $this->imgWidth + $x] = 1 - $greyness; // 1 for black, 0 for white } } }
/** * @medium */ public function testPdfEncodeColorCorrection() { $fn = __DIR__ . '/../Resources/sample_pdf2.pdf'; // Define a pixel within the image which has colour white when alpha // channel ignored. Test ignores alpha channel - See below. $x_px = $y_px = 10; // Retrieve PDF background color before encoding $imagick_pdf = new \Imagick($fn); $before_bg_color = $imagick_pdf->getImagePixelColor($x_px, $y_px)->getColor(); $im = new ImageManager(new Filesystem(new LocalAdapter(static::$tmp_dir . 'remote'))); $imagick_encoder = new ImagickEncoder(); $im->addEncoder($imagick_encoder); $source = $im->loadFromFile($fn, self::TEST_KEY . '_pdf'); // Create and render the variation $var = $im->createVariation($source, ImageFormat::JPEG(), 90, new ImageDimensions(1200)); $fn_jpeg = self::$tmp_dir . 'local/sample_pdf2.jpg'; $im->save($var, $fn_jpeg); // Check jpeg bg color $imagick_jpeg = new \Imagick($fn_jpeg); $after_bg_color = $imagick_jpeg->getImagePixelColor($x_px, $y_px)->getColor(); // Unset alpha channel unset($before_bg_color['a']); unset($after_bg_color['a']); $this->assertEquals($before_bg_color, $after_bg_color); }