Inheritance: implements Grafika\EditorInterface
Example #1
0
 /**
  * Generate and get the average hash of the image.
  *
  * @param Image $image
  *
  * @param Editor $editor
  *
  * @return string
  */
 public function hash($image, $editor)
 {
     // Resize the image.
     $width = 8;
     $height = 8;
     $image = clone $image;
     // Make sure we are working on the clone if Image is passed
     $editor->resizeExact($image, $width, $height);
     // Resize to exactly 8x8
     $gd = $image->getCore();
     // Create an array of greyscale pixel values.
     $pixels = array();
     for ($y = 0; $y < $height; $y++) {
         for ($x = 0; $x < $width; $x++) {
             $rgba = imagecolorat($gd, $x, $y);
             $r = $rgba >> 16 & 0xff;
             $g = $rgba >> 8 & 0xff;
             $b = $rgba & 0xff;
             $pixels[] = floor(($r + $g + $b) / 3);
             // Gray
         }
     }
     // Get the average pixel value.
     $average = floor(array_sum($pixels) / count($pixels));
     // Each hash bit is set based on whether the current pixels value is above or below the average.
     $hash = '';
     foreach ($pixels as $pixel) {
         if ($pixel > $average) {
             $hash .= '1';
         } else {
             $hash .= '0';
         }
     }
     return $hash;
 }
Example #2
0
 /**
  * Generate and get the difference hash of image.
  *
  * @param Image $image
  *
  * @param Editor $editor
  *
  * @return string
  */
 public function hash($image, $editor)
 {
     $width = 9;
     $height = 8;
     $image = clone $image;
     // Make sure we are working on the clone if Image is passed
     $editor->resizeExact($image, $width, $height);
     // Resize to exactly 9x8
     $gd = $image->getCore();
     // Build hash
     $hash = '';
     for ($y = 0; $y < $height; $y++) {
         // Get the pixel value for the leftmost pixel.
         $rgba = imagecolorat($gd, 0, $y);
         $r = $rgba >> 16 & 0xff;
         $g = $rgba >> 8 & 0xff;
         $b = $rgba & 0xff;
         $left = floor(($r + $g + $b) / 3);
         for ($x = 1; $x < $width; $x++) {
             // Get the pixel value for each pixel starting from position 1.
             $rgba = imagecolorat($gd, $x, $y);
             $r = $rgba >> 16 & 0xff;
             $g = $rgba >> 8 & 0xff;
             $b = $rgba & 0xff;
             $right = floor(($r + $g + $b) / 3);
             // Each hash bit is set based on whether the left pixel is brighter than the right pixel.
             if ($left > $right) {
                 $hash .= '1';
             } else {
                 $hash .= '0';
             }
             // Prepare the next loop.
             $left = $right;
         }
     }
     $editor->free($image);
     return $hash;
 }
Example #3
0
 /**
  * TODO: Anti-aliased curves
  * @param ImageInterface $image
  * @return ImageInterface
  */
 public function draw($image)
 {
     list($x, $y) = $this->pos;
     $left = $x + $this->width / 2;
     $top = $y + $this->height / 2;
     if (null !== $this->fillColor) {
         list($r, $g, $b, $alpha) = $this->fillColor->getRgba();
         $fillColorResource = imagecolorallocatealpha($image->getCore(), $r, $g, $b, Editor::gdAlpha($alpha));
         imagefilledellipse($image->getCore(), $left, $top, $this->width, $this->height, $fillColorResource);
     }
     // Create borders. It will be placed on top of the filled ellipse (if present)
     if (0 < $this->getBorderSize() and null !== $this->borderColor) {
         // With border > 0 AND borderColor !== null
         list($r, $g, $b, $alpha) = $this->borderColor->getRgba();
         $borderColorResource = imagecolorallocatealpha($image->getCore(), $r, $g, $b, Editor::gdAlpha($alpha));
         imageellipse($image->getCore(), $left, $top, $this->width, $this->height, $borderColorResource);
     }
     return $image;
 }
Example #4
0
 public function draw($image)
 {
     $x1 = $this->pos[0];
     $x2 = $x1 + $this->getWidth();
     $y1 = $this->pos[1];
     $y2 = $y1 + $this->getHeight();
     if (null !== $this->fillColor) {
         list($r, $g, $b, $alpha) = $this->fillColor->getRgba();
         $fillColorResource = imagecolorallocatealpha($image->getCore(), $r, $g, $b, Editor::gdAlpha($alpha));
         imagefilledrectangle($image->getCore(), $x1, $y1, $x2, $y2, $fillColorResource);
     }
     // Create borders. It will be placed on top of the filled rectangle (if present)
     if (0 < $this->getBorderSize() and null !== $this->borderColor) {
         // With border > 0 AND borderColor !== null
         list($r, $g, $b, $alpha) = $this->borderColor->getRgba();
         $borderColorResource = imagecolorallocatealpha($image->getCore(), $r, $g, $b, Editor::gdAlpha($alpha));
         imagerectangle($image->getCore(), $x1, $y1, $x2, $y2, $borderColorResource);
     }
     return $image;
 }
Example #5
0
 public function draw($image)
 {
     if (function_exists('imageantialias')) {
         imageantialias($image->getCore(), true);
     }
     $points = $this->points();
     $count = count($this->points);
     // Create filled polygon
     if (null !== $this->fillColor) {
         list($r, $g, $b, $alpha) = $this->getFillColor()->getRgba();
         $fillColorResource = imagecolorallocatealpha($image->getCore(), $r, $g, $b, Editor::gdAlpha($alpha));
         imagefilledpolygon($image->getCore(), $points, $count, $fillColorResource);
     }
     // Create polygon borders. It will be placed on top of the filled polygon (if present)
     if (0 < $this->getBorderSize() and null !== $this->borderColor) {
         // With border > 0 AND borderColor !== null
         list($r, $g, $b, $alpha) = $this->getBorderColor()->getRgba();
         $borderColorResource = imagecolorallocatealpha($image->getCore(), $r, $g, $b, Editor::gdAlpha($alpha));
         imagepolygon($image->getCore(), $points, $count, $borderColorResource);
     }
     return $image;
 }
Example #6
0
 /**
  * Detects and return the name of the first supported editor which can either be "Imagick" or "Gd".
  *
  * @param array $editorList Array of editor list names. Use this to change the order of evaluation for editors for this function call only. Default order of evaluation is Imagick then GD.
  *
  * @return string Name of available editor.
  * @throws \Exception Throws exception if there are no supported editors.
  */
 public static function detectAvailableEditor($editorList = null)
 {
     if (null === $editorList) {
         $editorList = self::$editorList;
     }
     /* Get first supported editor instance. Order of editorList matter. */
     foreach ($editorList as $editorName) {
         if ('Imagick' === $editorName) {
             $editorInstance = new ImagickEditor();
         } else {
             $editorInstance = new GdEditor();
         }
         /** @var EditorInterface $editorInstance */
         if (true === $editorInstance->isAvailable()) {
             return $editorName;
         }
     }
     throw new \Exception('No supported editor.');
 }