Unable to get the image from a structure field - Running Kirby 2.2

I’m running the kirby 2.2 beta. I’m having some issues getting the an image from the structured field. I’ve tried getting it in different ways but none seem to be working. Maybe I’m just a noob at this :joy: …or maybe it’s just a bug. Everything else works fine with the exception of the image/thumb. Anyways, here’s the test code I’m currently using and the blueprint:

<?php foreach(page('slides')->slides()->toStructure() as $slide): ?>
	<div class="slide" style="background-image: url();">
		

		<!-- Just test code.-->
		<div style="background: red; color: white; height: 200px; width: 100%; margin-top: 200px">

				<!-- This is the part that doesn't seem to be working -->		
				<?php echo thumb($slide->image($slide->background()), array('width' => 1440, 'height' => 650, 'crop' => true, 'upscale' => true, 'quality' => 80))->url() ?>

		</div>

		
		<div class="slide-content wrap">
			<div class="slide-meta">
				<?php if ($slide->display_title()->isTrue()): ?>
				<h2 class="title"><?php echo $slide->title() ?></h2>
				<?php endif ?>

				<?php if ($slide->text()->isNotEmpty()): ?>
				<p class="summary"><?php echo $slide->text() ?></p>
				<?php endif ?>

				<?php if ($slide->button_text()->isNotEmpty() && $slide->button_link()->isNotEmpty()): ?>
				<a class="btn btn-big<?php ecco($slide->button_style() == 'btn-line', ' btn-line'); ecco($slide->button_style() == 'btn-dark', ' btn-dark')?>" role="button" href="<?php echo $slide->button_link() ?>">
					<?php echo html($slide->button_text()) ?>
				</a>
				<?php endif ?>
			</div>
		</div>
	</div>
	<?php endforeach ?>

title: Slides

pages: false
deletable: false
files:
  type: image
  sortable: true

fields:
  title:
    label: Title
    type:  text
  slides:
    label: Slides
    type: structure
    entry: >
      <h1>{{title}}</h1><br />
      {{description}}
    fields:
      title:
        label: Title
        type: text
      display_title:
        label: Display Title
        type: toggle
        text: yes/no
      background:
        label: Futured Slide Image
        type: select
        options: images
        required: true
      description:
        label: Description
        type: textarea
      button_text:
        label: Button Text
        type: text
        width: 1/2
      button_style:
        label: Button Style
        type: select
        width: 1/2
        default: btn-green
        options:
          btn-green: Solid Green
          btn-dark: Solid Black
          btn-line: Line
      button_link:
        label: Button Link
        type: text

Thanks!

Edit:
I had been using the code above for kirby 2.1(with the exception of ->toStructure() of course) but I have updated the slides to the structured field type, that’s when it stopped working. Previously I was using a separate page for the slides and everything was working fine.

In the thumb line, you need to reference $page instead of $slide:

<?php echo thumb($page->image($slide->background()), array('width' => 1440, 'height' => 650, 'crop' => true, 'upscale' => true, 'quality' => 80))->url() ?>

Just tried it, didn’t work. It throws this error:

Fatal error: Uncaught The given image is invalid thrown in /Applications/MAMP/htdocs/kirby-copy/kirby/toolkit/lib/thumb.php on line 64

Ok, the page is not the current page, so it should rather be

<?php echo thumb(page('slides')->image($slide->background()), array('width' => 1440, 'height' => 650, 'crop' => true, 'upscale' => true, 'quality' => 80))->url() ?>

That seems to do the trick! Thank you! :+1:

Also thumbs can be handled a little more clearly in 2.2: https://twitter.com/getkirby/status/665129237117038593

2 Likes