Problem
The error tells it all. It means your Image
object tries to instanciate an SplFileObject
under the hood.
Since the file you given does not exists, SplFileObject::__construct()
throws an exception which is the one you're getting.
Fix
Relative paths can easily be a nightmare, CLI SAPI et al. can mess everything. The simple fix is to use an ABSOLUTE path. I see two ways to handle it.
Absolute path relative to the current file
In order to get the current absolute path, you can use the __DIR__
magic constant. Here's an example of usage
// apb/appassBundle/Controller/BarController::fooAction
$iconPath = sprintf("%s/../Resources/Images/icon.png", __DIR__);
$icon = new Image($iconPath, "icon");
$iconPath
now contains the absolute path to your icon. E.g:
/path/to/project/src/apb/appassBundle/Controller/../Resources/Images/icon.png
Which is correct, and the file actually exists.
Absolute path relative to the bundle
Another way is to get the path stored in the bundle. BundleInterface
has a getPath()
method which returns the absolute path of the bundle. (Root directory of the bundle)
// apb/appassBundle/Controller/BarController::fooAction
$bundle = $this->get('kernel')->getBundle('apbappassBundle');
$iconPath = sprintf("%s/Resources/Images/icon.png", $bundle->getPath());
$icon = new Image($iconPath, "icon");
$iconPath
now contains something like
/path/to/project/src/apb/appassBundle/Resources/Images/icon.png
Which is also correct
Edit 17/07/2014
As mentionned by gilden, an alternative would be to use the method KernelInterface::locateResource
You can use it like this
$iconPath = $this->get('kernel')->locateResource('@apbappassBundle/Resources/Images/icon.png');
$icon = new Image($iconPath, "icon");