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

How to scale images in a controller #35

Closed
stoefln opened this issue Oct 12, 2011 · 7 comments
Closed

How to scale images in a controller #35

stoefln opened this issue Oct 12, 2011 · 7 comments

Comments

@stoefln
Copy link

stoefln commented Oct 12, 2011

Found no documentation about that. Could you add it?

Best regards
Stephan

@avalanche123
Copy link
Owner

You can get ahold of Imagine service by calling $container->get('imagine'); once you have it, http://imagine.readthedocs.org/en/latest/index.html will help you with how to use it

@stoefln
Copy link
Author

stoefln commented Oct 12, 2011

thanks very much!

@jmikola
Copy link
Collaborator

jmikola commented Oct 12, 2011

Since I just implemented this last night in JmikolaImagineBundle, I'd suggest something like:

<?php
$box = $image->getSize();
$resizedImage = $image->resize($box->scale(1.5));

That would scale an image by its own dimensions (probably what you want).

@avalanche123
Copy link
Owner

may I reiterate:

<?php
$image->resize($image->getSize()->scale(1.5));

@jmikola
Copy link
Collaborator

jmikola commented Oct 12, 2011

@avalanche123: I guess you don't care about Law of Demeter anymore :)

@avalanche123
Copy link
Owner

@jmikola, do care :) here we're dealing with Box immutable value object (immutable and value object pretty much assumes each other). I feel that law of demeter makes a lot of sense when each method call is returning another object. so if we had A::b() -> B and B::c() -> C then

<?php
$a = new A();
$a->b()->c(); // would be terrible as you'd have to mock the whole chain in tests

while in our case we'd do something like:

<?php
$size = new Box(100, 100);

// stub out initial size
$image->expects($this->once())
    ->method('getSize')
    ->will($this->returnValue($size))
;

$image->expects($this->once())
    ->method('resize')
    ->with(new Box(150, 150))
;

// our filter that executes my above code
$filter->apply($image);

As you can see, mocking value objects is unnecessary as they are easy to construct and have on dependencies on other objects (easy to construct and have no dependencies is pretty much the same thing).

Another example would be PHP's DateTime class, it is globally accessible and easy to construct.

To conclude, don't violate the LOD for service objects, while don't bother with it in cases where object operates on its internal state. For example, builder's fluent interface, but not the factory method...

Cheers! :)

@avalanche123
Copy link
Owner

just realized, that the example is talking about and applicable to 'newables' in general and not only value objects

you can read about 'newables' at http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/

Bluestart83 pushed a commit to Bluestart83/AvalancheImagineBundle that referenced this issue Jun 10, 2013
Extracted the abstract class Resolver from WebPathResolver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants