Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple API for creating thumbnail #142

Closed
fkrauthan opened this issue Feb 9, 2013 · 13 comments
Closed

Simple API for creating thumbnail #142

fkrauthan opened this issue Feb 9, 2013 · 13 comments
Labels
Attn: Critical This issue or PR is critical and should be rushed into a new release ASAP.

Comments

@fkrauthan
Copy link

I like to request a simple service to create a thumbnail. As parameter it would be good if i just give the path (like in a twig template) and the name of the filter. And then it auto generate the specific thumbnail.

@chrisjohnson00
Copy link

Just define a new filter for your thumbnail needs:

    filter_sets:
        post_image_large:
            quality: 80
            filters:
                relative_resize: { widen: 1920 }    # Transforms 1920 wide without scale loss
        post_image_medium:
            quality: 80
            filters:
                relative_resize: { widen: 1200 }    # Transforms 1200 wide without scale loss
        post_image_thumbnail:
            quality: 65
            filters:
                relative_resize: { widen: 125 }    # Transforms 125 wide without scale loss

Then just reference the new filter in your twig template like this:
<img src="{{ post.getWebPath('filePath1')|imagine_filter('post_image_thumbnail') }}"/>

@chrisjohnson00
Copy link

Or, just use the example from the docs...
https://github.com/liip/LiipImagineBundle#basic-usage

@fkrauthan
Copy link
Author

I know but I talk about a use case where I plan to cache the image directly after uploading. For that I like to use a simple API. The Current method is a little bit more complex then what I request here. For example: why do I need to add a Request object? What if I use the cache generating within a console command?

@chrisjohnson00
Copy link

You're going to have to be clearer on what functionality you require. When I read your initial comment it is the requirements for the existing thumbnail filter.
From my limited understanding of the bundle, it is designed to work from a request standpoint (request of the image that is). There are likely other bundles available that have the functionality you require, which do the filters on upload rather than as requested.

@fkrauthan
Copy link
Author

Like I said only on upload base is something different. But I think only because the bundle is designed on request base there is no point to extend it to support generating with an API too since the code for generating a thumbnail exists already.

@havvg
Copy link
Contributor

havvg commented Feb 13, 2013

There is an API, already. It's the controller itself (see https://github.com/liip/LiipImagineBundle#using-the-controller-as-a-service).

You can simply upload your image anywhere, add a DataLoader to retrieve that uploaded image (e.g. if you uploaded to Amazon S3, see https://github.com/liip/LiipImagineBundle/blob/master/Resources/doc/data-loader/stream.md), and create a filter set which contains the thumbnail filter, as described in https://github.com/liip/LiipImagineBundle#basic-usage.

Does this work for you, what's missing, what do you expect?
Could you share some code around the entry point into the bundle, where you expect the API endpoint of choice?

@fkrauthan
Copy link
Author

I know but I would like to see something like that: ThumbnailManager::create($relativePath, $filterName)

I do not understand why I should pass the request object within my thumbnail generating while using the Controller as a service.

@havvg
Copy link
Contributor

havvg commented Feb 13, 2013

Ah, now I got your point. I will create a PR for this kind of entry point.

@havvg
Copy link
Contributor

havvg commented Feb 13, 2013

@fkrauthan Could you check the #150, does it work for you, anything missing?

This requires you to manually place those images according to your CacheResolver.

@havvg
Copy link
Contributor

havvg commented Feb 13, 2013

An example, how you could use this in a command:

class GenerateCachedImagesCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        // ..
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $filename = ''; // the absolute path of the file

        $imagine = $this->getContainer()->get('liip_imagine');
        $filterManager = $this->getContainer()->get('liip_imagine.filter.manager');

        $image = $imagine->open($filename);
        $image = $filterManager->applyFilter($image, 'thumbnail');

        // process filtered image
    }
}

@fkrauthan
Copy link
Author

Yeah something like that looks good.

@havvg
Copy link
Contributor

havvg commented Feb 13, 2013

Merged;
please open a new issue or PR, if you need more of this stuff. Re-usability is always good :-)

@havvg havvg closed this as completed Feb 13, 2013
This was referenced Mar 5, 2013
@scottgutman
Copy link

I know this is closed, but I searched for a solution and could not find it, so I am going to put it here. This is how I got my thumbnail saved locally and return the filename of the thumbnail.

public function makeThumbNail( $fileName ) {
		$filenameParts = pathinfo( $fileName );
		$filter        = 'thumbnails';
		$dataManager   = $this->container->get( 'liip_imagine.data.manager' );
		$filterManager = $this->container->get( 'liip_imagine.filter.manager' );
		$image = $dataManager->find( $filter, $filenameParts['basename'] );
		$image = $filterManager->applyFilter( $image, $filter );

		/** @var CacheManager */
		$imagineCacheManager = $this->container->get( 'liip_imagine.cache.manager' );
		$imagineCacheManager->store( $image, $filenameParts['basename'], $filter, 'local_storage' );

		$url = $imagineCacheManager->resolve( $filenameParts['basename'], $filter );

		return $filenameParts['dirname'] . parse_url($url, PHP_URL_PATH);
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Attn: Critical This issue or PR is critical and should be rushed into a new release ASAP.
Projects
None yet
Development

No branches or pull requests

4 participants