File Utility

Handle files and their references.

class Jar\Utilities\Utilities\FileUtility

Jar\Utilities\Utilities\FileUtility::getFileReferenceByUid($uid)

Loads \TYPO3\CMS\Core\Resource\FileReference object from sys_file_reference table via UID.

Parameters:
  • $uid (int) – UID of the sys_file_reference record.
Returns:

TYPO3\CMS\Core\Resource\FileReference or null if resource doesn’t exist or file is missing.

Example:

FileUtility::getFileReferenceByUid(123);

returns

// TYPO3\CMS\Core\Resource\FileReference
{
   'propertiesOfFileReference' => /* ... */,
   'name' => /* ... */,
   'originalFile' => /* ... */,
   'mergedProperties' => /* ... */
}

Jar\Utilities\Utilities\FileUtility::buildFileArrayBySysFileReferenceUid($uid, $configuration)

Shorthand for FileUtility::buildFileArrayBySysFileReference(FileUtility::getFileReferenceByUid($uid)), accepts the UID of an FileReference instead of using the FileReference object directly.

Parameters:
Returns:

File-information array or null if resource doesn’t exist or file is missing.

Example:

FileUtility::buildFileArrayBySysFileReferenceUid(123);

returns

[
   'uid' => 123
   'url' => 'fileadmin/user_upload/my-image-original.jpg',
   'alt' => 'some text',
   'title' => 'some text',
   'description' => 'some text',
   'link' => NULL,
]

Jar\Utilities\Utilities\FileUtility::buildFileArrayBySysFileReference($fileReference, $configuration)

Preparation of files and images in a simple array structure. Very helpful in image preparation and cropping.

Parameters:
  • TYPO3CMSCoreResourceFileReference – $fileReference
  • $configuration (array) –

    (optional). The configuration is based on three settings:

    1. showDetailedInformations (bool): For all kind of files. If active, list more informations about the file (name, extension, type, mimetype, size, basename).

      Warning

      This has an noticeable impact on performance when a huge amount of files is processed.

    2. tcaCropVariants (array): Just for images. List of cropVariants, which are applied to the images.

      Tip

      Instead of putting this together yourself, it’s easier to use the Reflection Service or Reflection Processor. These automatically read the tcaCropVariants from the corresponding TCA configurations of the images.

    3. processingConfigurationForCrop (array): Just for images. Configuration how the image should be proceed in the different tcaCropVariants. Configurations like maxWidth, minWidth, width (applies also for heigth) are possible.
Returns:

File-information array or null if resource doesn’t exist or file is missing.

Example:

FileUtility::buildFileArrayBySysFileReference(
   $aFileReference,
   [
      // show informations about file
      'showDetailedInformations' => true,

      // set cropping variants
      'tcaCropVariants' => [
         'desktop' => [
            /* .. */
            'cropArea' => [
               'x' => 0.0,
               'y' => 0.0,
               'width' => 1.0,
               'height' => 1.0,
            ]
         ],
         'mobile' => [
            /* .. */
            'cropArea' => [
               'x' => 0.0,
               'y' => 0.0,
               'width' => 1.0,
               'height' => 1.0,
            ]
         ]
      ],

      // set image rendering instructions for the different cropping variants
      'processingConfigurationForCrop' => [
         'desktop' => [
            'maxWidth' => 3000
         ],
         // will be ignored, because we have no tcaCropVariants['medium'] informations
         'medium' => [
            'maxWidth' => 1920
         ],
         // will be ignored, because we have no tcaCropVariants['tablet'] informations
         'tablet' => [
            'maxWidth' => 1024
         ],
         'mobile' => [
            'maxWidth' => 920
         ]
      ]
   ]
);

returns

[
   // base informations
   'uid' => 1
   'url' => 'fileadmin/user_upload/my-image-original.jpg',
   'alt' => 'some text',
   'title' => 'some text',
   'description' => 'some text',
   'link' => NULL,

   // detailed informations
   'name' => 'my-image-original.jpg',
   'extension' => 'jpg',
   'type' => 2,
   'mimetype' => 'image/jpeg',
   'size' => 2313,
   'basename' => 'my-image-original',

   // 'cropped' is just available for image-files
   'cropped' => [
      'desktop' => 'fileadmin/_processed_/1/my-image-original-max-width-3000.jpg',
      'mobile' => 'fileadmin/_processed_/1/my-image-original-max-width-920.jpg'
   ]
]

Jar\Utilities\Utilities\FileUtility::humanFilesize($bytes, $decimals = 2)

Converts filesizes in a human readable format.

Parameters:
  • $bytes (int) – Size of file in bytes.
  • $decimals (int) – (optional) Length of decimals.
Returns:

Filesize in human readable format.

Example:

FileUtility::humanFilesize(123456);    // 120.56 kB
FileUtility::humanFilesize(123456, 0); // 121 kB