Ansel::getImageFromFile PHP Method

getImageFromFile() public static method

Read an image from the filesystem.
public static getImageFromFile ( string $file, array $override = [] ) : array
$file string The filename of the image.
$override array Overwrite the file array with these values.
return array The image data of the file as an array
    public static function getImageFromFile($file, $override = array())
    {
        if (!file_exists($file)) {
            throw new Horde_Exception_NotFound(sprintf(_("The file \"%s\" doesn't exist."), $file));
        }
        global $conf;
        // Get the mime type of the file (and make sure it's an image).
        $mime_type = Horde_Mime_Magic::analyzeFile($file, isset($conf['mime']['magic_db']) ? $conf['mime']['magic_db'] : null);
        if (strpos($mime_type, 'image') === false) {
            throw new Horde_Exception_NotFound(sprintf(_("Can't get unknown file type \"%s\"."), $file));
        }
        $image = array('image_filename' => basename($file), 'image_caption' => '', 'image_type' => $mime_type, 'data' => file_get_contents($file));
        // Override the array e.g., if we're changing filename to something else.
        if (count($override)) {
            $image = array_merge($image, $override);
        }
        return $image;
    }

Usage Example

Example #1
0
         if (!$gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::EDIT)) {
             $error = _("Access denied adding photos to this gallery.");
         } else {
             $error = false;
         }
     } catch (Ansel_Exception $e) {
         $error = _("There was an error accessing the gallery.");
     }
 }
 if (!$name || $error) {
     $error = _("No file specified");
 } else {
     try {
         $GLOBALS['browser']->wasFileUploaded('imagefile', _("photo"));
         try {
             $image = Ansel::getImageFromFile($file, array('image_filename' => $name));
         } catch (Ansel_Exception $e) {
             $error = $e->getMessage();
         }
         $gallery = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGallery($galleryId);
         try {
             $image_id = $gallery->addImage($image);
             $error = false;
         } catch (Ansel_Exception $e) {
             $error = _("There was a problem uploading the photo.");
         }
     } catch (Horde_Browser_Exception $e) {
         $error = $e->getMessage();
     }
 }
 if ($error) {