FocalPointField-Craft icon indicating copy to clipboard operation
FocalPointField-Craft copied to clipboard

Position syntax using FPF with Imager and Eager Loaded Images

Open jayhlee opened this issue 7 years ago • 15 comments

I'm having trouble getting the correct syntax for my FPF field when I'm using it with Imager with eager-loaded images. Specifically, I'm not sure what follows "position: " because this doesn't seem to work:

{% set image = entry.projectFeatureImage[0] ?? null %}
{% set transformedImages = craft.imager.transformImage(image, [
	{ width: 1200 },
	{ width: 720 },
	{ width: 290 }
	], {
		format: 'jpg',
		ratio: 3/4,
		allowUpscale: false,
		mode: 'crop',
		position: image.focalPointField,
		interlace: true
	}) %}

When I test out {{ image.focalPointField }}, it outputs the correct percentages that I set for my image positions (e.g. '30% 40%'). However, it doesn't seem to work within the Imager code syntax. Any guidance would be great!

jayhlee avatar Mar 22 '17 00:03 jayhlee

That looks correct to me. What's the error you're seeing?

aelvan avatar Mar 25 '17 20:03 aelvan

I get an "Undefined offset: 1" PHP notice. Specifically, it says the error is at:

craft/plugins/imager/services/ImagerService.php(914), which is... list($leftOffset, $topOffset) = explode(' ', $position);

jayhlee avatar Mar 25 '17 21:03 jayhlee

Weird. Since you're mentioning eager loading, does it work if you don't eager-load?

aelvan avatar Mar 26 '17 14:03 aelvan

Just tried using it without the eager loading, but I still get the same error thrown. Here's what it's showing.

screen shot 2017-03-27 at 2 50 40 pm

jayhlee avatar Mar 27 '17 22:03 jayhlee

There must be something wrong with the value that is being passed in, that space in '30% 40%' maybe isn't a space? Is '30% 40%' the default value you've assigned to the field? Try erasing the value, write it again, save, and check again.

Also, what happens if you replace the position part with position: image.focalPointField ?? '50% 50%',?

aelvan avatar Mar 28 '17 08:03 aelvan

+1

lukakemperle avatar Mar 29 '17 15:03 lukakemperle

@aelvan I tried removing the positions, but I get the same error. I also replaced the position to what you wanted me to try out, but I get the same error.

jayhlee avatar Mar 29 '17 19:03 jayhlee

Can you try opening up craft/plugins/imager/services/ImagerService.php, right before line 914 add Craft::dd($position), and let me know what the output is.

Is there only this one image on the page, or several? If there're more than one, can you try to make a minimal test case with only one and see if you still get the error.

aelvan avatar Mar 29 '17 19:03 aelvan

@aelvan Hi Andre, Sorry about the late reply. I reduced the images down to one on the page and added the line of code as you instructed. Here's the error message: errors

jayhlee avatar Apr 03 '17 21:04 jayhlee

Any progress on this? I'm getting the same error.

ghost avatar Jun 21 '17 23:06 ghost

Yeah, this keeps popping up from time to time, and the reason is always that an invalid value is being passed. I'd check (and double check) that the default values that is set in the config and on the field(s), are correct. It's suppose to be xx% xx%, with a plain space inbetween. No dash, comma, invisible utf-8 space thingy, or whatever...

aelvan avatar Jun 22 '17 09:06 aelvan

Ah, I see what the issue is. You HAVE to input a default value for the FocalPoint field. For some reason the default value FocalPoint says it will output doesn't get outputted if you leave it blank. So looping over an image array with some images having a focal point and some not was causing the error. As soon as I added a default value (50% 50%) to my field everything worked fine.

ghost avatar Jun 22 '17 14:06 ghost

Ok, that sounds like a bug. I'll have a look at it.

aelvan avatar Jun 22 '17 14:06 aelvan

I got the same problem. While waiting for a fix, is there a simple code solution I can use, instead of looping through all images and giving them a default value?

samlous avatar Sep 04 '17 07:09 samlous

@samlous All you have to do is put the default value in the Focal Point Field when you create it, not every time you insert images in the code. Once that default value is there you should be fine.

ghost avatar Sep 04 '17 16:09 ghost