Jump to content

Allow setting ImageSizer upscaling via Pageimage->size()


teppo
 Share

Recommended Posts

It would be nice to be able to turn upscaling off (set it to false on ImageSizer) via size() function of Pageimage. Currently size accepts two parameters (width, height) -- how 'bout adding upscaling (true/false, default to true.. or perhaps "disable upscaling" and default to false, whichever makes more sense) as third parameter? :)

  • Like 2
Link to comment
Share on other sites

Nobody likes upscaling. But if I call a function asking for it to create something at a certain size, it's because that's the size required. If there are other conditions surrounding that, shouldn't it be left to the caller to decide? i.e.

if($image->width > 300) $image = $image->width(300); 

If not wild about extra arguments (especially booleans) in API functions because they aren't self explanatory in the resulting code. So I'll only use them when absolutely necessary. Whereas, a bit of code like above is self explanatory to us now, and years from now when things like "what does the 3rd argument mean" are no longer fresh on our minds. :) On the other hand, I've got no problem with self explanatory function names, like widthNoUpscale(), but sometimes adding more makes the API less simple.

  • Like 2
Link to comment
Share on other sites

I understand what Ryan says here, but I will like everyone that suggests these :D

I even think that something along maxSize should be the default behavior, because no one wants to see giant pixels on any picture.

  • Like 1
Link to comment
Share on other sites

This depends quite a bit on which way you've used to working with images / thumbs. I myself find it very hard to think of any possible situation where I'd like to upscale an image -- and if I really wanted to do that, I'd be happy to do it with CSS, though usually I prefer to have too small images centered etc. so that they look at least somewhat ok. This is why I've so far ended up adding if's before every single size-call, which gets kinda annoying in the long run.

On the other hand, I really, really appreciate what Ryan is saying here. Any and all extra params / features / whatever should go through very serious consideration before being implemented, unless you wan't to end up with bloated code no-one really understands anymore. Seen this happen way too many times.. :)

  • Like 1
Link to comment
Share on other sites

It should be quite easy to add these methods to Pageimage by a module. ImageSizer already has everything we need.

I just did a quick hack and put the Pageimage->size method into my template.php, just to see whats possible with a few modifications, and it seems to work great.

function resizeImage($image, $width, $height, $cropping=true, $upscaling=true) {
	$width = (int) $width;
	$height = (int) $height;
	$basename = basename($image->basename(), "." . $image->ext());   // i.e. myfile
	$basename .= '.' . $width . 'x' . $height . "." . $image->ext(); // i.e. myfile.100x100.jpg
	$filename = $image->pagefiles->path() . $basename;
	if(!is_file($filename)) {
		if(@copy($image->filename(), $filename)) {
			$sizer = new ImageSizer($filename);
			$sizer->setCropping($cropping);
			$sizer->setUpscaling($upscaling);
			$sizer->resize($width, $height);
			if($image->config->chmodFile) chmod($filename, octdec($image->config->chmodFile));
		}
	}
	$pageimage = clone $image;
	$pageimage->setFilename($filename);
	$pageimage->setOriginal($image);
	return $pageimage;
}

usage like this:

$neverUpdscaledImage = resizeImage ($page->image, 100,100,true,false);
$uncroppedImage = resizeImage ($page->image, 100,100,false,true);

echo "<img src={$neverUpdscaledImage->url} />";

Btw, I dont't recommend to do it like this. I just wanted to test whats possible.

  • Like 1
Link to comment
Share on other sites

This depends quite a bit on which way you've used to working with images / thumbs. I myself find it very hard to think of any possible situation where I'd like to upscale an image

I think this is correct, that it really just depends the way you are used to working with images. For the sites I build, if I ask for an image of a specific size, it's because that's the size I need. I don't care what it has to do to get there, because the needs of my layout are more important than the image itself. An image of any size other than what I asked for would be inconsistent at best or a layout breaker at worst. I'm usually dealing with lots of images that I depend on being consistent with each other in size. I hate upscaling, but I hate inconsistency even more. If something has to be upscaled then I'm going to notice that and try to find a better image, but at least the damage is limited to the image rather than the layout or consistency.

Here's another way to accomplish it. Put this somewhere in one of your first template includes:

function sizeDown(HookEvent $event) {
  $width = (int) $event->arguments[0];
  $height = (int) $event->arguments[1];
  $image = $event->object; 
  if($image->width < $width) $width = $image->width;
  if($image->height < $height) $height = $image->height; 
  $event->return = $image->size($width, $height); 
}

$wire->addHook('Pageimage::sizeDown', null, 'sizeDown'); 

Now you can substitute any $image->size() call with $image->sizeDown():

$image = $page->image->sizeDown(300, 200); 
  • Like 3
Link to comment
Share on other sites

@Ryan: that seems like a valid solution, thanks! I'll be implementing it in most of our projects from now on :)

.. and for the record, I really hate inconsistency too. It's just that quite often inconsistent image sizes are something we have no control over; this happens when content is either fetched from a third party with varying level of consistency or added by not-exactly-web-savvy content authors.. :)

Like I said, generally I prefer to control these situations with styling rather than scaling, so whenever an image really needs to be of proper size I place it inside a container -- horizontally and/or vertically centered, depending on situation. This has worked for most situations so far.

  • Like 1
Link to comment
Share on other sites

@Ryan: it seems that a lot of these processes, like $wire->addHook(), that could be amazingly powerful to make certain thinks simple, aren't explicit in the api guide/cheatsheet, etc.. Which is strange, seing as they are all so clean. Is this by design?

Are we not supposed to do things this way?

EDIT: Ok, so that was dumb. Just found the apigen Documentation, which has basically everything (although browsing the actual source is also a breeze). Still, there are some replies in this forum that could easily be made into a collection of "intermediate developer" examples.. Cookbook style :)

Link to comment
Share on other sites

Ryan writes documentation based on demand. Currently basics that most people need are covered very well, but some more advanced topics are missing "official" documentation. Lot's of information is here on forums (it is a bit searching - or what we try to encourage - asking), but the official stuff is coming.

But after the basics - as you mentioned too - I think the great thing is that source code is very easy to follow and mostly very well commented. Good example is the addHook() method which has nice example here: /site/modules/Helloworld.module (which is referenced from here: http://processwire.com/api/modules/).

Link to comment
Share on other sites

  • 6 months later...

Just in the case that somebody else is looking for a solution to disallow upscaling like i was right now:

I just read this thread and thought about using one of the functions above - then i found another thread where some options which were recently added to the PageImage class were discribed:

http://processwire.c...tion-questions/

Even cropping direction was added now:

http://processwire.c...ons/#entry22535

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...