roelvanduijnhoven/reverse-image-cover

此包已被废弃,不再维护。作者建议使用 jouwweb/reverse-image-cover 包代替。

计算在视口覆盖下源图像的可见部分

0.3.1 2019-05-06 06:44 UTC

This package is auto-updated.

Last update: 2022-02-01 12:50:10 UTC


README

一个小型库,可以计算当图像作为元素背景投影时显示的源图像部分。

例如,可以使用它通过预处理图像以仅返回裁剪部分来减少带宽。最终结果将与查看器相同。

目前仅支持使用百分比定位图像并覆盖图像的背景。

用法

$viewport = new BackgroundCoverViewport(
  $viewportWidth,
  $viewportHeigt,
  $backgroundPositionX,
  $backgroundPositionY
);

$crop = $viewport->computeUsedCrop(
  $imageWidth,
  $imageHeight
);

echo "Visible part of image starts at({$crop[0]}, {$crop[1]}) ";
echo "and has a dimension of {$crop[2]} x {$crop[3]}";

测试

可以通过以下方式运行测试

  1. composer install
  2. vendor/bin/phpunit

测试套件可以可视化如下

  1. 运行服务器 php -S localhost:8080
  2. 访问 http://localhost:8080/tests/demo.php`