/** * Resize an image to 1px x 1px then pick the colour of that pixel * @param string $fullPathToFileOnDisk e.g. /tmp/foo.png * @return string e.g. "SeaGreen" */ public function calculateMainColourInImage($fullPathToFileOnDisk) { $colourName = null; $fileExtension = \BoxUK\Describr\Helper\FileHelper::getFileExtension($fullPathToFileOnDisk); $extForFn = str_ireplace('jpg', 'jpeg', $fileExtension); $fn = "imagecreatefrom{$extForFn}"; if (function_exists($fn)) { $im = $fn($fullPathToFileOnDisk); // resize the image to 1px $onePixelVersionOfImage = imagecreatetruecolor(1, 1); imagecopyresized($onePixelVersionOfImage, $im, 0, 0, 0, 0, 1, 1, imagesx($im), imagesy($im)); // Extract the colour information from the 1pixel version of this file $colour = imagecolorat($onePixelVersionOfImage, 0, 0); $colours = imagecolorsforindex($onePixelVersionOfImage, $colour); $colourName = $this->rgbColourToString($colours['red'], $colours['green'], $colours['blue']); if ($colourName) { // Switch "DarkOrange2" to "DarkOrange" etc $colourName = str_replace(array('1', '2', '3', '4', '5', '6', '7', '8', '9', '0'), '', $colourName); } // clean up resources unset($im); unset($onePixelVersionOfImage); } return $colourName; }
/** * Describe a file using all the relevant plugins that report they match * the mime type of the file at $fullPathToFileOnDisk * * @param string $fullPathToFileOnDisk e.g. /tmp/foo.png * * @return MediaFileAttributes The information found out about the file at $fullPathToFileOnDisk * * @throws \BoxUK\Describr\FileNotFoundException If the file at $fullPathToFileOnDisk is not * readable */ public function describeFile($fullPathToFileOnDisk) { if (!is_file($fullPathToFileOnDisk)) { throw new FileNotFoundException("File '{$fullPathToFileOnDisk}' not found or not accessible"); } // ascertain MIME type so we can check what plugins to use $mimeType = \BoxUK\Describr\Helper\FileHelper::getMimeType($fullPathToFileOnDisk); $extension = \BoxUK\Describr\Helper\FileHelper::getFileExtension($fullPathToFileOnDisk); $mediaFileAttributes = new MediaFileAttributes(); foreach ($this->availablePlugins as $pluginName) { /* @var $plugin plugins\Plugin */ $plugin = new $pluginName(); if ($plugin->supportsFile($mimeType, $extension)) { $plugin->setFile($fullPathToFileOnDisk); $mediaFileAttributes->setPluginResults($pluginName, $plugin->getAttributes()); } } return $mediaFileAttributes; }
/** * Returns a media file's file extension from its filename * * @return string e.g. "png" */ protected function getFileExtension() { return \BoxUK\Describr\Helper\FileHelper::getFileExtension($this->fullPathToFileOnDisk); }