joomla-ua/juimage

JUImage - library for render thumbs.

5.13 2023-02-11 08:53 UTC

README

JUImage - library for render thumbs with support webp and avif.

Create thumbs for Joomla! extension or stand-alone use.

Demo (All thumbs)

Use in Joomla! Extension

Usage

Stand-alone

Composer Install

composer require joomla-ua/juimage

You can then later update using composer:

composer update

Code example

After installing, you need to require Composer's autoloader:

require_once('vendor/autoload.php');

$config['root_path'] = __DIR__;
$config['img_blank'] = 'images/logos';

$juImg = new JUImage\Image($config);
$thumb = $juImg->render('images/sampledata/fruitshop/apple.jpg', [
	'w'     => '300',
	'h'     => '100',
	'q'     => '77',
	'cache' => 'img'
]);

echo '<img src="' . $thumb . '" alt="Apple" width="300" height="100">';

Joomla! Integration

Install

Install extention library (lib_juimage_v3.x.x.zip) using Joomla! Extension Manager.

Code example

Code for use in your extension.

JLoader::register('JUImage',  JPATH_LIBRARIES . '/juimage/JUImage.php');

$juImg = new JUImage();
$thumb = $juImg->render('images/sampledata/fruitshop/apple.jpg', [
	'w'     => '300',
	'h'     => '100',
	'q'     => '77',
	'cache' => 'img'
]);

echo '<img src="'. $thumb .'" alt="Apple" width="300">';

or

require_once(JPATH_SITE . '/libraries/juimage/vendor/autoload.php');

$juImg = new JUImage\Image();
$thumb = $juImg->render('images/sampledata/fruitshop/apple.jpg', [
	'w'     => '300',
	'h'     => '100',
	'q'     => '77',
	'cache' => 'img'
]);

echo '<img src="'. $thumb .'" alt="Apple" width="300">';

WebP support

<?php

$thumb = $juImg->render('images/sampledata/fruitshop/apple.jpg', [
	'w'     => '300',
	'h'     => '100',
	'q'     => '95',
	'webp'  => true
]);
?>

<picture>
	<source srcset="<?php echo $thumb->webp; ?>" type="image/webp">
	<img src="<?php echo $thumb->img; ?>" alt="Apple" width="300" height="100">
</picture>

Use GD2 lib for webp thumbs:

<?php

$thumb = $juImg->render('images/sampledata/fruitshop/apple.jpg', [
	'w'           => '300',
	'h'           => '100',
	'q'           => '95',
	'webp'        => true,
	'imagemagick' => false
]);
?>

AVIF support

AVIF image format (requires PHP 8.1.0)

<?php

$thumb = $juImg->render('images/sampledata/fruitshop/apple.jpg', [
	'w'     => '300',
	'h'     => '100',
	'q'     => '95',
	'avif'  => true
]);
?>

<picture>
	<source srcset="<?php echo $thumb->avif; ?>" type="image/avif">
	<img src="<?php echo $thumb->img; ?>" alt="Apple" width="300" height="100">
</picture>

How to combine WebP and AVIF?

<?php

$thumb = $juImg->render('images/sampledata/fruitshop/apple.jpg', [
	'w'     => '300',
	'h'     => '100',
	'q'     => '95',
	'avif'  => true,
	'webp'  => true
]);
?>

<picture>
	<source srcset="<?php echo $thumb->avif; ?>" type="image/avif">
	<source srcset="<?php echo $thumb->webp; ?>" type="image/webp">
	<img src="<?php echo $thumb->img; ?>" alt="Apple" width="300" height="100">
</picture>

YouTube and Vimeo support

Youtube:

$thumb = $juImg->render('https://www.youtube.com/watch?v=xxxxxxxxxxx', [
	'w' => '300',
	'h' => '100'
]);

Vimeo:

$thumb = $juImg->render('https://vimeo.com/xxxxxxxxx', [
	'w' => '300',
	'h' => '100'
]);

Image size support

<?php

$thumb = $juImg->render('images/sampledata/fruitshop/apple.jpg', [
	'w' => '300'
]);

// Image size for thumb
$size = $juImg->size($thumb);
	
echo '<img src="'. $thumb .'" alt="Apple" width="'. $size->width .'" height="'. $size->height .'">';	

Options

Add option to this array:

[
  	'w'     => '300',
  	'h'     => '100',
  	'q'     => '77',
  	'cache' => 'img'
]

License

GNU General Public License version 3 or later; see LICENSE.md

Software used

JUImage is based on the phpThumb() Class (James Heinrich), and fast-image-size library (Marc Alexander).

Sponsors

JetBrains

Thanks to JetBrains for supporting the project through sponsoring some All Products Packs within their Free Open Source License program.