2

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?

toesslab
  • 5,092
  • 8
  • 43
  • 62
user1448031
  • 2,172
  • 11
  • 44
  • 89

1 Answers1

1

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)
toesslab
  • 5,092
  • 8
  • 43
  • 62
  • I'm now using this on a different context and I'm trying to delete the file but it's not working like this: `$returnFile->delete();`. Isn't this supposed to work? – user1448031 Sep 29 '16 at 02:13
  • I'm not getting any errors. This what I'm trying to do: if($fv = $fi->import('application/files/test.pdf')) { $returnFile = \Concrete\Core\File\File::getByID($fv->getFileID()); }$mh->addAttachment($returnFile); $returnFile->delete(); – user1448031 Sep 29 '16 at 04:42
  • Do some debug messages betweenbthe steps – toesslab Sep 29 '16 at 07:23