/** * Returns the colors of the image in an array, ordered in descending order, where * the keys are the colors, and the values are the count of the color. * @param $filePath the path to the local image file * @return an array keyed by hex color value, mapping to the frequency of use in the images */ public static function GenerateFromLocalImage(ImageFile $file) { $pImage = \PMVC\plug('image'); // resize the image for a reasonable amount of colors $previewSize = new ImageSize(150, 150); $srcSize = $file->getSize(); $scale = 1; if ($srcSize->w > 0) { $scale = min($previewSize->w / $srcSize->w, $previewSize->h / $srcSize->h); } if ($scale < 1) { $destSize = new ImageSize(floor($scale * $srcSize->w), floor($scale * $srcSize->h)); } else { $destSize = new ImageSize($srcSize->w, $srcSize->h); } $image_resized = $pImage->create($destSize); $image_orig = $pImage->create($file); //WE NEED NEAREST NEIGHBOR RESIZING, BECAUSE IT DOESN'T ALTER THE COLORS imagecopyresampled($image_resized, $image_orig, 0, 0, 0, 0, $destSize->w, $destSize->h, $srcSize->w, $srcSize->h); // walk the image counting colors $hexarray = []; $pImage->process($destSize, [$image_resized], function ($point, $im) use(&$hexarray) { $index = imagecolorat($im, $point->x, $point->y); $Colors = imagecolorsforindex($im, $index); $hexarray[] = (new BaseColor($Colors['red'], $Colors['green'], $Colors['blue']))->toHex(); }); $hexarray = array_count_values($hexarray); natsort($hexarray); $hexarray = array_slice($hexarray, -10, 10); $hexarray = array_reverse($hexarray, true); return $hexarray; }
function testImageOutput() { $pImage = \PMVC\plug('image'); $in = $pImage->create(new ImageSize('100', '100')); $out = (new ImageOutput($in))->save(); $imageFile = new ImageFile($out); $this->assertEquals('png', $imageFile->getExt()); }
function create(ImageFile $fileIn, ImageSize $canvasSize, ImageSize $dstSize, Coord2D $dstLoc) { $imOut = \PMVC\plug('image')->create($canvasSize); $caller = $this->caller; $pColor = \PMVC\plug('color'); $pColor->fill($imOut, $pColor->hexToRgb($caller['color'])); $srcSize = $fileIn->getSize(); imagecopyresized($imOut, $fileIn->toGd(), $dstLoc->x, $dstLoc->y, 0, 0, $dstSize->w, $dstSize->h, $srcSize->w, $srcSize->h); return $imOut; }