infernalmedia/responsive-image-craft
Composer 安装命令:
composer require infernalmedia/responsive-image-craft
包简介
ResponsiveImageCraft is a Laravel package that simplifies responsive image generation with a command-line interface, a flexible display component, and SCSS mixins for effortless background-image integration. Elevate your image handling capabilities and deliver stunning visuals effortlessly
README 文档
README
Introducing ResponsiveImageCraft, a powerful Laravel package that empowers you to effortlessly generate responsive images in various desired static formats through a simple command line interface. This package equips you with a versatile component to effortlessly display these responsive images on your web pages. Additionally, you'll find a collection of SCSS mixins to effortlessly generate the necessary code when using the images as background-images, ensuring a seamless integration into your projects.
With ResponsiveImageCraft, you can now efficiently manage and serve responsive images, enhancing your website's performance and user experience across various devices. Embrace the ease and flexibility of ResponsiveImageCraft as it takes care of the heavy lifting for you, delivering stunning visuals without the hassle. So why wait? Elevate your image handling capabilities today with ResponsiveImageCraft.
Installation
You can install the package via composer:
composer require infernalmedia/responsive-image-craft
You can publish the config file with:
php artisan vendor:publish --tag="responsive-image-craft-config"
This is the contents of the published config file:
return [ 'use_responsive_images' => env('USE_RESPONSIVE_IMAGES', true), 'source_disk' => env('RESPONSIVE_IMAGES_SOURCE_DISK', 'public'), 'target_disk' => env('RESPONSIVE_IMAGES_TARGET_DISK', 's3'), 'source_directory' => env('RESPONSIVE_IMAGES_SOURCE_DIRECTORY', 'images'), 'target_directory' => env('RESPONSIVE_IMAGES_TARGET_DIRECTORY', 'images'), 'sizes' => explode(',', env('RESPONSIVE_IMAGES_SIZES', "320,640,880,1024,1200,1760,2100")), 'extensions' => [ "jpg", "png", "avif", "webp", ], 'extensions_filters_rules' => [ "jpg" => ["png"], "png" => ["jpg"], "webp" => [], "avif" => [], ], 'extensions_to_ignore' => [ 'svg' ], 'filename_to_ignore' => [ 'favicon' ], 'supported_file_extensions' => [ "jpg", "webp", "png", "avif", "gif", "tiff", "pjpg" ], 'filename_spacer' => '@', 'container_css_class_name' => 'img-container' 'scss_path' => resource_path('/scss/utilities'), ];
Optionally, you can publish the scss helper. The scss needs sass:^1.57 to run correctly.
php artisan vendor:publish --tag="responsive-image-craft-scss"
Usage
Images generations
After configuring, the source and targets, run:
php artisan responsive-image-craft:generate
Optionally you can define the source
php artisan responsive-image-craft:generate --source-disk=public --relative-source-path=images
Responsive Image Component
Here is a basic working configuration
<x-infernal-responsive-img src="full/path/to/generated/image.original-extension"
alt="the alternate text"
height=1570 {{-- original height --}}
width=1216 {{-- original width --}} />
Will generate the following html
<div class="img-container the-css-class-to-add-to-the-wrapping-container"> <picture> <!-- load avif images if supported --> <source type="image/avif" srcset="url-to-img@320.avif 320w, ....{until 1216w}"> [...] <!-- the img format fallback --> <img src="url-to-img.jpg" srcset="url-to-img@320.jpg 320w,...{until 1216w}" alt="the alternate text" decoding="async" loading="lazy" width="1216" height="1570"> </picture> </div>
The following attributes are available for customization
-
$alt
The alternative text for the image. Learn more -
$skipPictureTag
A flag to skip thepicturetag and only render theimgtag. -
$src
The image relative path. -
$height
The height of the original image. Learn more -
$width
The width of the original image Learn more. The width value will define de maximum available image width, following the generated ones. -
$asyncDecoding
Enable async decoding of the image. This can improve page load performance by decoding images in the background. Learn more -
$lazy
Enable lazy loading of the image. When set to true, the image will load only when it's visible in the viewport. Learn more -
$containerClass
CSS class to add to the wrapping top<div>. You can apply custom styling using this class. -
$imgAttributes
Additional html attributes to be added to theimgtag. e.g.:"class=img-class data-attribute=attribute"
Here is complete example:
<x-infernal-responsive-img src="full/path/to/generated/image.original-extension"
alt="the alternate text"
container-class="the-css-class-to-add-to-the-wrapping-container" {{-- optional --}}
height=1570 {{-- original height --}}
width=1216 {{-- original width --}}
:async-decoding="true"
:lazy="true"
:skip-picture-tag="false"
img-attributes="attributes to add to img tag" />
Will generate the following html
<div class="img-container the-css-class-to-add-to-the-wrapping-container"> <picture> <!-- load avif images if supported --> <source type="image/avif" srcset="url-to-img@320.avif 320w, ....{until 1216w}"> [...] <!-- the img format fallback --> <img attributes to add to img tag src="url-to-img.jpg" srcset="url-to-img@320.jpg 320w,...{until 1216w}" alt="the alternate text" decoding="async" loading="lazy" width="1216" height="1570"> </picture> </div>
Using responsive image with Sass (and ViteJs)
Configuration
Set your vite.config.js to give env variables to your scss
import { defineConfig, loadEnv } from 'vite' import laravel from 'laravel-vite-plugin' import { AssetsScssFile } from './resources/js/AssetsScssFile' export default defineConfig(async ({ command, mode }) => { const env = loadEnv(mode, process.cwd()) const useResponsiveImages = Boolean( JSON.parse(env.VITE_USE_RESPONSIVE_IMAGES.toLowerCase()) ) const assetsUrl = useResponsiveImages ? env.VITE_ASSETS_URL : null const variables = new Map([ ['$assetsUrl', assetsUrl], ['$filenameSpacer', env.VITE_RESPONSIVE_IMAGES_FILENAME_SPACER], ]) const scss = new AssetsScssFile() scss.createSCSSFile(variables) return { plugins: [ laravel({ ... }), ], css: { preprocessorOptions: { scss: { additionalData: "@use 'assets' as *;", }, }, }, } })
Responsive Image SCSS Mixins and Styles
The Responsive Image SCSS module provides useful mixins and styles for creating responsive background images and managing media queries.
Mixin usage
Use the responsive-background-image-from-existing-css-var mixin to generate responsive background images from existing CSS vars generated by the laravel facade ResponsiveImageCraft::getCssVariables():
.example-class { @include responsive-background-image-from-existing-css-var( $sizes: (480px, 768px, 1024px), $extensions: ('webp', 'jpg'), $full: 'full' ); }
Or use the responsive-background-image mixin to generate responsive background images
.example-class { @include responsive-background-image( $base-relative-path: "images/example.jpg", $extensions: ('webp', 'jpg'), $breakpoints: (480px, 768px, 1024px), $file-name-spacer: '@' ); }
Be careful, the extensions list order will define the browser preference.
Other useful functions are available in /scss/_assets-url-helper.scss
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
infernalmedia/responsive-image-craft 适用场景与选型建议
infernalmedia/responsive-image-craft 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.13k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 08 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「infernalmedia」 「responsive-image-craft」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 infernalmedia/responsive-image-craft 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 infernalmedia/responsive-image-craft 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 infernalmedia/responsive-image-craft 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Cookie consent bottom window for Québec Law 25, forked from statikbe/laravel-cookie-consent
Alfabank REST API integration
Flysystem Adapter for the Gitea Repository files API v1
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
统计信息
- 总下载量: 6.13k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-04