joomla-ua/juimage

JUImage - 渲染缩略图的库。

5.13 2023-02-11 08:53 UTC

README

JUImage - 支持webp和avif渲染缩略图的库。

为Joomla!扩展或独立使用创建缩略图。

演示(所有缩略图)

在Joomla!扩展中使用

使用方法

独立

Composer安装

composer require joomla-ua/juimage

然后您可以使用composer更新

composer update

代码示例

安装后,您需要引入Composer的自动加载器

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!集成

安装

使用Joomla!扩展管理器安装扩展库(lib_juimage_v3.x.x.zip)。

代码示例

在您的扩展中使用的代码。

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">';

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支持

<?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>

使用GD2库进行WebP缩略图

<?php

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

AVIF支持

AVIF图像格式(需要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>

如何结合WebP和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和Vimeo支持

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'
]);

图像尺寸支持

<?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 .'">';	

选项

将选项添加到此数组

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

许可证

GNU通用公共许可证版本3或更高版本;请参阅LICENSE.md

使用的软件

JUImage基于phpThumb()类(James Heinrich)和fast-image-size库(Marc Alexander)。

赞助商

JetBrains

感谢JetBrains通过其免费开源许可计划赞助一些所有产品包以支持项目。