I'm using LiipImagineBundle in my Symfony2 project and everything is working great. I've defined a thumbnail filter:
<img src="{{ 'bundles/app/images/home/warning.jpg'|imagine_filter('thumb_home') }}" alt="Warning" />
And it works perfectly, but some of my images don't need any filter, so I've tried to remove the filter:
<img src="{{ 'bundles/app/images/home/warning.jpg'|imagine_filter('') }}" alt="Warning" />
<img src="{{ 'bundles/app/images/home/warning.jpg'|imagine_filter() }}" alt="Warning" />
**edited to correct the example as malcolm points out
But it doesn't work, so I need to use Assetic for these images:
{% image '#AppBundle/Resources/public/images/example.jpg' %}
<img src="{{ asset_url }}" alt="Example" />
{% endimage %}
I can't find in the documentation of the bundle any option which allows me to use the Bundle without filters. Is that true? Of course, I can use the LiipImagineBundle way to include images when they need a filter and the Assetic way when they don't, but switching between two different ways of achieving almost the same thing is pretty annoying.
Silly answer to your question - create an original size filter then :). Something like this:
class OriginalSizeFilter implements LoaderInterface
{
public function load(ImageInterface $image, array $options = [])
{
$size = $image->getSize();
$origWidth = $size->getWidth();
$origHeight = $size->getHeight();
$filter = new Thumbnail(new Box($origWidth, $origHeight));
$image = $filter->apply($image);
return $image;
}
}
service:
original_size_filter:
class: ...\OriginalSizeFilter
tags:
- { name: liip_imagine.filter.loader, loader: original_size_filter }
config.yml:
filter_sets:
original:
filters:
original_size_filter: ~
Usage:
<img src="{{ 'bundles/app/images/home/warning.jpg'|imagine_filter('original') }}" alt="Warning" />
But, why not use a asset() function if you don't want any filters to be applied?
Related
I've got a Blade file like this:
<a href="{{ $url }}" class="{{ $wrapperClass }}">
<p class="{{ $paraClass }}">{{ $title }}</p>
</a>
And call it like this:
<x-mycomponent
url="{{ $book->url }}"
title={{ $book->title }}
wrapper-class=""
para-class = ""
/>
Works great, but whenever I want to add a variable to the component, I need to add this to each file where I call this component.
How can I write it without having to specify all variables and without having to use isset() or {{ $myvar ?? '' }} in Blade? like this:
<x-mycomponent
url="{{ $book->url }}"
title={{ $book->title }}
/>
You can assign default values for your properties like the below's peace of code:
#props([
"product" => null
])
{{ $product }}
and then, you can call your component in both these ways:
<x-mycomponent />
<x-mycomponent product="$product" />
Note: #props is a Blade directive that allows you to pass data from a parent component to a child component.
I want to send an email with an attachement so therefore I need the absolute path to the pdf file.
I tried the flowing code in the controller:
$this->get('templating.helper.assets')->getUrl('MyDoc.pdf', 'doc');
But this returns:
/bundles/MyBundle/doc/MyDoc.pdf?v=1
This is the path without the domain for web access. But I need the real path starting the root of the disk, like example below. How is this possible? Thanks.
/home/MyAccount/public_html/web/src/company/MyBundle/Resources/public/doc/MyDoc.pdf
$this->get('templating.helper.assets') asset helper is about HTTP URI, not directory path.
However, you can easily get the right absolute directory web path like this:
$appPath = $this->container->getParameter('kernel.root_dir');
$webPath = realpath($appPath . '/../web');
$webPath should return something like this:
/home/martins/www/symfony-project/web
A bit of a cheat sheet for anyone else looking for different URL/URI paths for different applications.
Using the Symfony\Component\HttpFoundation\Request component, there are lots of different ways you can call upon this.
Consider this link http://dev.col.com/app_dev.php/my-route?bar=1&foo=bar
$r = $this->getRequest();
$r->getClientIp() 127.0.0.1
$r->getScriptName() /app_dev.php
$r->getPathInfo() /my-route
$r->getBasePath() ''
$r->getBaseUrl() /app_dev.php
$r->getScheme() http
$r->getPort() 80
$r->getHttpHost() dev.col.com
$r->getRequestUri() /app_dev.php/my-route?bar=1&foo=bar
$r->getBaseServerUrl() http://dev.col.com
$r->getUri() http://dev.col.com/app_dev.php/my-route?bar=1&foo=bar
$r->getUriForPath("/other-path") http://dev.col.com/app_dev.php/other-path
$r->getQueryString() bar=1&foo=bar
$r->isSecure() false
$r->getHost() dev.col.com
$r->getMethod() GET
$r->isXmlHttpRequest() false
Or you can do it in Twig as well. For example:
<div>
<a href="{{ job.url }}">
<img src="{{ app.request.scheme ~ '://' ~ app.request.host }}{{ asset(job.webPath) }}" alt="{{ job.company }} logo" />
</a>
</div>
<div>
<a href="{{ job.url }}">
<img src="{{ app.baseServerUrl }}{{ asset(job.webPath) }}"alt="{{ job.company }} logo" />
</a>
</div>
use Symfony\Component\Finder\Finder;
$finder = new Finder();
$finder->files()->in("dir")->name("name.file");
foreach ($finder as $file) {
// Dump the absolute path
var_dump($file->getRealPath());
}
From Docs: http://symfony.com/doc/current/components/finder.html
With the introduction of the asset component in Symfony 2.7, how can I output the relative url of an asset without a version number ?
I am using the following code whcih does not work anymore :
<img src="{{ asset('images/user.png') | imagine_filter('default') }}" alt="Image de profil" class="img-circle whitebg">
The asset function outputs an url with a version number and this is not handled properly by the imagine_filter :
http://mywebsite.com/media/cache/resolve/default/images/user.png%3Fversion=v1.0
My config :
framework:
assets:
version: 'v1.0'
version_format: '%%s?version=%%s'
base_path: ~
packages:
images:
base_path: /images
version_format: ''
Ideally I would be able to make the imagine filter work while keeping this versionning strategy
Otherwise, deactivating the versioning for images could be good enough
Thanks for your help !
Apply the filter to the relative path directly, asset() can be seen as a sort of helper:
<img src="{{ asset('user.png'|imagine_filter('default')) }}">
You can also sett the version (4th parameter) to false:
<img src="{{ asset('user.png'|imagine_filter('default'), null, false, false) }}">
You should explicitly point to the package that you want to use for concrete asset:
<img src="{{ asset('user.png', 'images') | imagine_filter('default') }}" alt="Image de profil" class="img-circle whitebg">
I think there is a bug in the bundle LiipImagineBundle. I explain:
Here is my new config of the bundle:
liip_imagine:
resolvers:
default:
web_path:
web_root: %kernel.root_dir%/../web/img
# %kernel.root_dir%/../web/img is the folder where filtered images will be created!
cache_prefix: media/cache
# media/cache the prefix of folder where the cached images will be created
filter_sets:
cache: ~
my_thumb:
quality: 75
filters:
thumbnail: { size: [120, 90], mode: outbound }
This is the twig part for displaying the image:
{# This way the filtered image will not be created!#}
<img src="{{ 'img/test.jpg' | imagine_filter('my_thumb') }}" />
{# That way, the filted images will be created. asset() must be used. #}
<img src="{{ asset('img/test.jpg' | imagine_filter('my_thumb')) }}" />
The generated link of the image is not correct! In fact, the obtained link is:
http://localhost/media/cache/my_thumb/img/test.jpg
The expected correct link is:
http://localhost/tuto/web/img/media/cache/my_thumb/img/test.jpg
There is a missing part in the url: tuto/web/img . Is this a bug?
To avoid that problem, I did this:
<img src="{{ asset('img/test.jpg' | imagine_filter('my_thumb'))|replace({'media':'tuto/web/img/media'}) }}" />
I guess that playing with twig is not a good solution.
It is a bug in LiipImagineBundle? If not, please give the correct config for that bundle!
Thanks!
I found finally the solution on github.
I'm nearly new in Symfony2 and I have a little question:
I'm developing an email template, which has txt and html parts (no problem with it)
The only 'problem' I have is with the absolute paths of assets in TWIG.
Inside my email.html.twig file I have something like this:
<img src="{{ asset('images/my-image.png') }}" alt="My image" /> but it writes the route with relative path.
I discovered a little solution to add absolute paths, something like this:
{% set abs = app.request.scheme ~ '://' ~ app.request.host %}
<img src="{{ abs ~ asset('images/my-image.png') }}" alt="My image" />
It works! But I want to improve this solution and also learn to create custom filters (I read the documentation, but I got a bit lost)
I want to create something like this:
<img src="{{ asset('images/my-image.png' | absolute) }}" alt="My image" />
But I don't know how to properly override the assetics extension. Can you help me?
Thanks a lot!!
Well, it is a little hard to copy paste the solution but I can make a short cookbook so you can go step by step and do it yourself:
1) You will have to implement Assetic/Filter/FilterInterface
2) If you look at the FilterInterface class, you will see that you have to to implement two methods: filterLoad and filterDump.
So, you will do something like this:
<?php
namespace You\YourBundle\Assetic\Filter;
use Assetic\Asset\AssetInterface;
use Assetic\Filter\FilterInterface;
class YourAsseticFilter implements FilterInterface
{
public function filterLoad(AssetInterface $asset)
{
// do something
}
public function filterDump(AssetInterface $asset)
{
$content = $asset->getContent();
// do something
$asset->setContent($content);
}
}
And after you to this, you have to something very similar to registering twig extensions in services.yml in YourBundle. Sure, it depens if you use YML, XML... configuration. I use yml so I will type it in yml :)
parameters:
your_bundle.class: You\YourBundle\Assetic\Filter\YourAsseticFilter
services:
your_bundle.assetic.your_assetic_filter:
class: %your_bundle.class%
tags:
- { name: assetic.filter }
- { alias: yourChosenNameForYourNewAsseticFilter }
And then you call it like | yourChosenNameForYourNewAsseticFilter, of course.