Example #1
0
 /**
  * Make a mosaic that looks like the specified guide image using the
  * source images found in the specified folder.
  * 
  * @param string $pathToGuideImage The path to the guide image.
  * @param array $pathsToSourceImagesFolders An array of the path(s) to the
  *     folder(s) that contain the sources images.
  * @return string The path to the newly created mosaic image.
  */
 public static function makeMosaic($pathToGuideImage, $pathsToSourceImagesFolders, $verboseOutput = false)
 {
     $guideImage = new GuideImage($pathToGuideImage);
     $sourceImageFiles = self::listImageFilesInFolders($pathsToSourceImagesFolders);
     $sourceImages = array();
     $numSourceImageFiles = count($sourceImageFiles);
     $numProcessedSourceImages = 0;
     $progressMeter = new ProgressMeter();
     foreach ($sourceImageFiles as $sourceImageFile) {
         try {
             $sourceImages[] = new SourceImage($sourceImageFile, 4 / 3, 120);
         } catch (\Exception $e) {
             if ($verboseOutput) {
                 echo sprintf('Skipping "%s": %s%s', $sourceImageFile, $e->getMessage(), PHP_EOL);
             }
         }
         $numProcessedSourceImages += 1;
         $progressMeter->showProgress('Loading source images (1/3)', $numProcessedSourceImages / $numSourceImageFiles);
     }
     // Create a mosaic from those slices/images.
     $mosaic = new Mosaic($guideImage, $sourceImages);
     // Generate a filename for the new mosaic image.
     $mosaicFilename = 'Mosaic_' . microtime(true) . '.jpg';
     $filePathToMosaic = dirname($pathToGuideImage) . '/' . $mosaicFilename;
     // Steps D & E: (Lazily generate and) save the mosaic image.
     $mosaic->saveAs($filePathToMosaic);
     return $filePathToMosaic;
 }
Example #2
0
 /**
  * Assemble the final mosaic Image from the list of Matches.
  * 
  * @param Match[] $matches
  * @return Image
  * @throws \Exception
  */
 protected function assembleImageFromMatches($matches)
 {
     // Define how much bigger (than the guide image) to make the mosaic.
     $multiplier = 2;
     $imageResource = imagecreatetruecolor($this->guideImage->getWidth() * $multiplier, $this->guideImage->getHeight() * $multiplier);
     $tempCounter = 0;
     $progressMeter = new ProgressMeter();
     $numMatches = count($matches);
     foreach ($matches as $match) {
         /* @var $match Match */
         $guideImageSlice = $match->getSlice();
         $imageToInsert = $match->getSourceImage();
         $success = imagecopyresampled($imageResource, $imageToInsert->getImageResource(), $guideImageSlice->xOffsetInParent * $multiplier, $guideImageSlice->yOffsetInParent * $multiplier, 0, 0, $guideImageSlice->getWidth() * $multiplier, $guideImageSlice->getHeight() * $multiplier, $imageToInsert->getWidth(), $imageToInsert->getHeight());
         // Stop if something went wrong.
         if (!$success) {
             throw new \Exception('Failed to insert a source image into the final mosaic.', 1435207513);
         }
         $progressMeter->showProgress('Assembling mosaic     (3/3)', ++$tempCounter / $numMatches);
         //$tempImage = new Image();
         //$tempImage->setImageResource($imageResource);
         //$tempImage->saveAsJpg('Step-' . ++$tempCounter . '.jpg');
     }
     $image = new Image();
     $image->setImageResource($imageResource);
     return $image;
 }