Getting imgix to work with kirby

I’m trying to get imgix to work and am getting the following error. I read this post but I’m not sure how best to solve the problem.

<?php foreach($piece->thumbnails()->yaml() as $image): ?>   
    <?php if($image = $piece->image($image)): ?>
        <a href="<?= $image->url(); ?>" data-fancybox="gallery" data-slug="<?php echo str::slug($piece->title())?>" data-caption="<?= $piece->title()->html(); ?>">
            <img src="<?php echo imgix($image, url()) ?>" /> 
    <?php endif ?>
<?php endforeach; ?>

and this is the plugin code

function imgix($url, $params = array()) {
  if(is_object($url)) {
    $url = $url->url();
  $url = trim(str_replace(array(url(), ''), '', $url), '/');
  $url = '' . $url . '?' . http_build_query($params);
  return $url;

Well, your function call is not correct. You pass a string (url()) as second parameter ($params) to the imgix() function, where it expects an array. So not surprising, the http_build_query() method complains, because it wants an array or object, but is fed a string instead.

This is how you call the method (change parameters as required):

<img src="<?php echo imgix($image, array('w' => 300)) ?>" alt="" />
1 Like