Esempio n. 1
0
 /**
  * @param \Imagick $imagick
  * @param int $graphWidth
  * @param int $graphHeight
  */
 public static function analyzeImage(\Imagick $imagick, $graphWidth = 255, $graphHeight = 127)
 {
     $sampleHeight = 20;
     $border = 2;
     $imagick->transposeImage();
     $imagick->scaleImage($graphWidth, $sampleHeight);
     $imageIterator = new \ImagickPixelIterator($imagick);
     $luminosityArray = [];
     foreach ($imageIterator as $row => $pixels) {
         /* Loop through pixel rows */
         foreach ($pixels as $column => $pixel) {
             /* Loop through the pixels in the row (columns) */
             /** @var $pixel \ImagickPixel */
             if (false) {
                 $color = $pixel->getColor();
                 $luminosityArray[] = $color['r'];
             } else {
                 $hsl = $pixel->getHSL();
                 $luminosityArray[] = $hsl['luminosity'];
             }
         }
         /* Sync the iterator, this is important to do on each iteration */
         $imageIterator->syncIterator();
         break;
     }
     $draw = new \ImagickDraw();
     $strokeColor = new \ImagickPixel('red');
     $fillColor = new \ImagickPixel('red');
     $draw->setStrokeColor($strokeColor);
     $draw->setFillColor($fillColor);
     $draw->setStrokeWidth(0);
     $draw->setFontSize(72);
     $draw->setStrokeAntiAlias(true);
     $previous = false;
     $x = 0;
     foreach ($luminosityArray as $luminosity) {
         $pos = $graphHeight - 1 - $luminosity * ($graphHeight - 1);
         if ($previous !== false) {
             /** @var $previous int */
             //printf ( "%d, %d, %d, %d <br/>\n" , $x - 1, $previous, $x, $pos);
             $draw->line($x - 1, $previous, $x, $pos);
         }
         $x += 1;
         $previous = $pos;
     }
     $plot = new \Imagick();
     $plot->newImage($graphWidth, $graphHeight, 'white');
     $plot->drawImage($draw);
     $outputImage = new \Imagick();
     $outputImage->newImage($graphWidth, $graphHeight + $sampleHeight, 'white');
     $outputImage->compositeimage($plot, \Imagick::COMPOSITE_ATOP, 0, 0);
     $outputImage->compositeimage($imagick, \Imagick::COMPOSITE_ATOP, 0, $graphHeight);
     $outputImage->borderimage('black', $border, $border);
     $outputImage->setImageFormat("png");
     App::cachingHeader("Content-Type: image/png");
     echo $outputImage;
 }
Esempio n. 2
0
function fxAnalyzeImage(\Imagick $imagick)
{
    $graphWidth = $imagick->getImageWidth();
    $sampleHeight = 20;
    $graphHeight = 128;
    $border = 2;
    $imageIterator = new \ImagickPixelIterator($imagick);
    $reds = [];
    foreach ($imageIterator as $pixels) {
        /* Loop through pixel rows */
        foreach ($pixels as $pixel) {
            /* Loop through the pixels in the row (columns) */
            /** @var $pixel \ImagickPixel */
            $color = $pixel->getColor();
            $reds[] = $color['r'];
        }
        $imageIterator->syncIterator();
        /* Sync the iterator, this is important to do on each iteration */
    }
    $draw = new \ImagickDraw();
    $strokeColor = new \ImagickPixel('red');
    $fillColor = new \ImagickPixel('none');
    $draw->setStrokeColor($strokeColor);
    $draw->setFillColor($fillColor);
    $draw->setStrokeWidth(1);
    $draw->setFontSize(72);
    $draw->setStrokeAntiAlias(true);
    $x = 0;
    $points = [];
    foreach ($reds as $red) {
        $pos = $graphHeight - $red * $graphHeight / 256;
        $points[] = ['x' => $x, 'y' => $pos];
        $x += 1;
    }
    $draw->polyline($points);
    $plot = new \Imagick();
    $plot->newImage($graphWidth, $graphHeight, 'white');
    $plot->drawImage($draw);
    $outputImage = new \Imagick();
    $outputImage->newImage($graphWidth, $graphHeight + $sampleHeight, 'white');
    $outputImage->compositeimage($plot, \Imagick::COMPOSITE_ATOP, 0, 0);
    $imagick->resizeimage($imagick->getImageWidth(), $sampleHeight, \Imagick::FILTER_LANCZOS, 1);
    $outputImage->compositeimage($imagick, \Imagick::COMPOSITE_ATOP, 0, $graphHeight);
    $outputImage->borderimage('black', $border, $border);
    $outputImage->setImageFormat("png");
    header("Content-Type: image/png");
    echo $outputImage;
}
Esempio n. 3
0
function construct($imagePath)
{
    $imagick = new \Imagick(realpath($imagePath));
    $imageIterator = new \ImagickPixelIterator($imagick);
    /* Loop through pixel rows */
    foreach ($imageIterator as $pixels) {
        /* Loop through the pixels in the row (columns) */
        foreach ($pixels as $column => $pixel) {
            /** @var $pixel \ImagickPixel */
            if ($column % 2) {
                /* Paint every second pixel black*/
                $pixel->setColor("rgba(0, 0, 0, 0)");
            }
        }
        /* Sync the iterator, this is important to do on each iteration */
        $imageIterator->syncIterator();
    }
    header("Content-Type: image/jpg");
    echo $imagick;
}
Esempio n. 4
0
function weight($imageRegion)
{
    // Outros efectos para mellorar
    $imageRegion->negateImage(true);
    $imageRegion->fxImage('intensity');
    // Contar pixels
    $it = new \ImagickPixelIterator($imageRegion);
    $whitePixel = new \ImagickPixel('#000');
    $totalPixels = 0;
    $dirtyPixels = 0;
    foreach ($it as $pixels) {
        foreach ($pixels as $column => $pixel) {
            if (!$pixel->isSimilar($whitePixel, 0.1)) {
                $dirtyPixels++;
            }
            $totalPixels++;
        }
        $it->syncIterator();
    }
    return $dirtyPixels * 100 / $totalPixels;
}
Esempio n. 5
0
 private function weight($imageRegion)
 {
     // Outros efectos para mellorar
     //$imageRegion->negateImage(true);
     //$imageRegion->fxImage('intensity');
     //$imageRegion->contrastImage(1);
     $imageRegion->whiteThresholdImage('#EEE');
     $imageRegion->blackThresholdImage('#EEE');
     /*$basename = $this->basedir . '/%02d.png';
       $imageRegion->writeImage( sprintf($basename, $this->i++) );*/
     // Contar pixels
     $it = new \ImagickPixelIterator($imageRegion);
     //$whitePixel = new \ImagickPixel('#000');
     $whitePixel = new \ImagickPixel('#FFF');
     $totalPixels = 0;
     $dirtyPixels = 0;
     foreach ($it as $pixels) {
         foreach ($pixels as $column => $pixel) {
             if (!$pixel->isSimilar($whitePixel, 0.1)) {
                 $dirtyPixels++;
             }
             $totalPixels++;
         }
         $it->syncIterator();
     }
     $percent = $dirtyPixels * 100 / $totalPixels;
     //dump($percent);
     $this->logger->warning('%: ' . $percent);
     return $percent;
 }