vagovszky/imokutr

PHP图像缩略图生成器,主要用于Nette框架,也可作为独立的PHP库使用

1.0.5 2021-04-17 16:56 UTC

This package is auto-updated.

Last update: 2024-08-28 23:39:55 UTC


README

PHP图像缩略图生成器,主要用于Nette框架,也可作为独立的PHP库使用。

文档:https://vladimir.skach.cz/imokutr

基本理念

Imokutr是一个PHP库,旨在自动从原始图像生成缩略图。

创建时考虑了以下假设

  1. 我们在目录中有一些原始图像。

示例

/disk0/web/files/articles/2018/10/image.jpg
/disk0/web/files/gallery/pigs/pig01.png
/disk0/web/files/wallpaper/bifFish.png
  1. 我们在某个地方存储了这些图像的相对路径,例如在数据库中

示例

id |title                 | url
1  | Blue image           | /articles/2018/10/Blueimage.jpg
2  ! Cute pig at our farm | /gallery/pigs/pig01.png
3  | Big fish             | /wallpaper/bifFish.png
  1. 我们想在网站上轻松创建和显示列表图像、概述图像或从原始图像创建的画廊缩略图图像。

Nette集成

Composer

composer install skachcz/imokutr

在neon.config中注册Nette扩展和配置

extensions:
    imokutr: SkachCz\Imokutr\Nette\ImokutrExtension

imokutr:
    originalRootPath: /home/webs/nette-app/www
    thumbsRootPath: /home/webs/nette-app/www/thumbs
    thumbsRootRelativePath: /thumbs
    qualityJpeg: 85
    qualityPng: 8

在*.latte模板中使用

参数 - 宽度、高度、resizeType、cropParameter

width (必选) - 缩略图像素宽度。

height (必选) - 缩略图像素高度。

resizeType (可选) - 默认为 'w'

'w' - 缩略图宽度将与参数相同,缩略图高度根据图像比例计算

'h' - 缩略图高度将与高度参数相同,缩略图宽度根据图像比例计算

  ↖    ↑   ↗
    1  2  3
 ←  8  0  4  →
    7  6  5
  ↙    ↓   ↘

'c' - 裁剪 - 缩略图将被裁剪到特定的宽度和高度

{'/files/original/image1.jpg'|imoUrl:100:200:'h'}

cropParameter (可选) 0-8 - 默认为 0

Nette过滤器

{imoTag '/files/original/image1.jpg', 300, 150, 'w'}
<img src="%url%" width="%width%" height="%height%" alt="my alt" title="my title">
{/imoTag}

{imoTag '/files/original/image1.jpg', 300, 150, 'h'}
<img src="%url%" width="%width%" height="%height%" alt="my alt" title="my title">
{/imoTag}

{imoTag '/files/original/image1.jpg', 300, 150, 'c'}
<img src="%url%" width="%width%" height="%height%" alt="my alt" title="my title">
{/imoTag}

{imoTag '/files/original/image1.jpg', 300, 150, 'c', 5}
<img src="%url%" width="%width%" height="%height%" alt="my alt" title="my title">
{/imoTag}

在宏代码内部,你可以使用占位符 %url%、%width% 和 %height%

独立库

使用composer

composer.json ''' { "require": { "skachcz/imokutr": "master-dev" } } '''

<?php

use SkachCz\Imokutr\Imokutr;
use SkachCz\Imokutr\Config;
use SkachCz\Imokutr\Html;

require_once(__DIR__ . "/Imokutr/imokutr.php");

$config = new Config(
    __DIR__ . "/images/original", 
    __DIR__ . "/images/thumbs", 
    "/images/thumbs"
    );

$kutr = new Imokutr($config);

<?php  
// original image is in /images/original/img1.jpg
$img = $kutr->getThumbnail("img1.jpg", 200, 100, 'w'); ?>

<img src="<?php echo $img['url'] ?>" width="<?php echo $img['width'] ?>" height="<?php echo $img['height'] ?>">
or
<?php echo Html::img($img) ?>


<?php  $img = $kutr->getThumbnail("img1.jpg", 300, 300, 'c', 5); ?>

<?php echo Html::img($img, "Photo", "My photo", ["data-text" => "My text", "onclick" => 'alert("box")', "class" => "red"] ) ?>