Different thumbs for landscape and portrait orientation

Hi-

I’m trying to have two thumbnail sizes in general; one for landscape orientated and one for landscape orientated pics.
I wasn’t sure if there is any easy solution to this- at least i havent found one in the thumbnail section inside the docs.
What did not work out at all is this:

<div class="grid-item">
    <a href="<?php echo $project->url() ?>">
    <?php if($image = $project->images()->sortBy('sort', 'asc')->first()): ?>
    		<?php for($image->orientation == landscape){ ?>
      <img class="project-image" src="<?php echo thumb($image, array('height' => 200, 'crop' => true))->url() ?>" alt="<?php echo $project->title()->html() ?>" >
      <?php } ?>


    		<?php for($image->orientation == portrait){ ?>
      <img class="project-image" src="<?php echo thumb($image, array('height' => 20, 'crop' => true))->url() ?>" alt="<?php echo $project->title()->html() ?>" >
      		<?php } ?>

    <?php endif ?>

phpskills=0

There are some issues here:

  • for should be if
  • the parentheses are missing after orientation
<div class="grid-item">
  <a href="<?php echo $project->url() ?>">
    <?php if($image = $project->images()->sortBy('sort', 'asc')->first()): ?>
    	<?php if($image->orientation() == 'landscape'){ ?>
          <img class="project-image" src="<?php echo thumb($image, array('width' => 200, 'height' => '100', crop' => true))->url() ?>" alt="<?php echo $project->title()->html() ?>" >
      <?php } ?>


    	<?php if($image->orientation() == 'portrait'){ ?>
          <img class="project-image" src="<?php echo thumb($image, array('width' => 20, 'height' => 100, 'crop' => true))->url() ?>" alt="<?php echo $project->title()->html() ?>" >
      	<?php } ?>

   <?php endif ?>
 </a>
</div>

Have you turned on debugging in your config.php? If not, pls. do:

c::set('debug', true);

Edit: I modified the code, the thumb() method throws a warning when ‘height’ is used as its first parameter …

1 Like

Hey thanks for your quick reply!

It worked with portrait/landscape in quotes !
But it completely ignores my css layout right now- is this even a proper method of doing what I want?

thanks again…

Oh, yes, I overlooked the quotes, my bad. I’ve corrected it above. Can you tell us what your CSS is like or what you want to achieve?

I guess, height: 20 is a typo for orientation = portrait?

Ok the layout messup came due to an extra closing div tag…

You are right - i made this height very small to see the changes right away :slight_smile:

As the layout now works, I noticed in google developer tools, that my thumbnails werent generated properly: developer tools tells me that the pictures natural size is the same as the original file…

Is the thumbs folder writable?

Is this even necessary/possible with xampp on windows? the thumbfolder generated a subfolder called ‘projects’ and inside that folder are all the projects i have subpages for on the site - but they are all emtpy!

:frowning:

That means that the thumb folder is writable at least.

Could you please check in your phpinfo() if GD is available (there should be something like GD Support: enabled)?

Well, as long as it works on the server, it doesn’t really matter, but it should actually work on Windows with Xampp. You could check if the gd extension is enabled using phpinfo().

@lukasbestle was faster …

yes it is :confused:

GD Support	enabled
GD Version	bundled (2.1.0 compatible)
FreeType Support	enabled
FreeType Linkage	with freetype
FreeType Version	2.5.5
GIF Read Support	enabled
GIF Create Support	enabled
JPEG Support	enabled
libJPEG Version	9 compatible
PNG Support	enabled
libPNG Version	1.5.18
WBMP Support	enabled
XPM Support	enabled
libXpm Version	30411
XBM Support	enabled
WebP Support	enabled

Hm, that seems right. To be honest I don’t know what else it could be. Does your code work on a different server?

If you can’t test on a remote server, you could try to install another local server, e.g. MAMP for Windows for testing purposes.

@fptt:

If I look in the source code of the rendered page I see:

<br />
<b>Warning</b>:  get_resource_type() expects parameter 1 to be resource, boolean given in <b>E:\xampp_7.0.8\htdocs\kirby-2.3.1\kirby\toolkit\vendors\abeautifulsite\SimpleImage.php</b> on line <b>57</b><br />

I can not tell you, why the above code gives this warning with Kirby 2.3.1. May be the code is for Kirby older than 2.3.0…, I haven’t found it in the Kirby docs yet.

I think any square images furthermore are not shown with that two "if"s. The function $file->orientation() has three answers: landscape, portrait or square.

But I suggest to use the following code, tested on the page “/projects” of the Starterkit with Kirby 2.3.1 using XAMPP 7.0.8 (PHP 7.0.8) on Windows:


<div class="grid-item">
  <a href="<?php echo $project->url(); ?>">
<?php if($image = $project->images()->sortBy('sort', 'asc')->first()): ?>
<?php if($image->orientation() == 'landscape'){ ?>
    <img class="project-image" src="<?php echo $image->width(200)->url() ?>" alt="<?php echo $project->title()->html(); ?>" >
<?php } else { ?>
    <img class="project-image" src="<?php echo $image->height(200)->url() ?>" alt="<?php echo $project->title()->html(); ?>" >
<?php }; ?>
<?php else: ?>
    <?php echo $project->title()->html(); ?>
<?php endif; ?>
  </a>
</div>

Hints:

  • This code needs Kirby 2.3.0+ .
  • Change “200” two times to the identical size, you need or want for the longer border of the images. This code shows all images with this same longest border, if the original image is not lesser than this size.
  • I have added the project title as text, if no image is available (else:). You may want to add some css for this.

Addendum:

The hints apply accordingly!

If we use the Kirby helper “ecco” and the $file function “isLandscape”, the code can look like:


<div class="grid-item">
  <a href="<?php echo $project->url(); ?>">
<?php if($image = $project->images()->sortBy('sort', 'asc')->first()): ?>
    <img class="project-image" src="<?php ecco ($image->isLandscape(), $image->width(200)->url(), $image->height(200)->url()); ?>" alt="<?php echo $project->title()->html(); ?>">
<?php else: ?>
    <?php echo $project->title()->html(); ?>
<?php endif; ?>
  </a>
</div>

If we use the ternary operator instead like @texnixe suggested sometimes, the code could look like:


<div class="grid-item">
  <a href="<?php echo $project->url(); ?>">
<?php if($image = $project->images()->sortBy('sort', 'asc')->first()): ?>
    <img class="project-image" src="<?php echo ($image->isLandscape() ? $image->width(200)->url() : $image->height(200)->url(); ?>" alt="<?php echo $project->title()->html(); ?>"> 
<?php else: ?>
    <?php echo $project->title()->html(); ?>
<?php endif; ?>
  </a>
</div>

Good luck!

@anon77445132: Pls. note that $file->width() only resizes an image; @fptt, however, wants to crop the image,so $file->crop() would be the correct method.

However, the old thumb() syntax should still work and create thumbnails.

Ok, but on Kirby 2.3.1, I think we can only use ONE:
either crop or width, nor both.

@texnixe, but your code leads to the told warning.

If you want to crop, you use crop() with a size, not width().

Yes, but then we don’t need to distinguish the formats landscape, portrait and square, I think.
But that has to answer @fptt, I can not know…, @fptt please look at $file->crop($width, $height = null, $quality = null)
The answer depends on what you want to get!

Well, the warning seems to be caused by “height” as the first parameter, if you use width first and then height, everything is fine.

1 Like

Sorry, there is only “height” I think.

That’s exactly what I said, that’s what’s causing the warning, which means that the code above does not work, because the thumb method expects ‘width’ as the first parameter.

@fptt: I corrected the code above because of this!

1 Like