Search code examples
concrete5concrete5-5.7

How to get file object from file path in Concrete5?


I want to get thumbnail from the file inside the themes directory but getThumbnail() function requires me to pass a file object.

This obviously doesn't work:

$v = View::getInstance();
$themePath = $v->getThemePath();    
$thumbnail = $imageHelper->getThumbnail($themePath.'/images/abc.jpg', 100, 100, true);

So is it possible to get file object from the file path?


Solution

  • If the file exists only in the folder structure but not as concrete5 File Object, you need the FileImporter first:

    use Concrete\Core\File\Importer;
    $fi = new Importer();
    if($fv = $fi->importIncomingFile($themePath . '/' . $filename)){
        $returnFile = \Concrete\Core\File\File::getByID($fv->getFileID());
    }
    

    Then you may pass that file object to the getThumbNail() function. The getThumbNail() doesn't take a path but an image object as first parameter:

    $imageHelper = Core::make('helper/image');    
    $thumbnail = $imageHelper->getThumbnail($returnFile, 300, 9999, false);
    

    Here are all params that are taken (from the API):

    /**
     * Returns a path to the specified item, resized and/or cropped to meet max width and height. $obj can either be
     * a string (path) or a file object.
     * Returns an object with the following properties: src, width, height
     * @param mixed $obj
     * @param int $maxWidth
     * @param int $maxHeight
     * @param bool $crop
     */
    public function getThumbnail($obj, $maxWidth, $maxHeight, $crop = false)