fronty/responsive-images 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fronty/responsive-images

最新稳定版本:v0.30

Composer 安装命令:

composer require fronty/responsive-images

包简介

Library that will make working with images in your responsive Wordpress template a breeze.

README 文档

README

Library that will make working with images in your responsive Wordpress template a breeze.

What's in the box:

  • Generates <img> tags with precise srcset and sizes attributes for images from WP Media Library.
  • Generates <img> tags wrapped in aspect-ratio preserving wrapper for both WP Media images and theme images. Great to prevent CLS with lazyloaded images.
  • Clean API to define responsive image sizes in either mobile-first (min-width) or desktop-first (max-width) manner.
  • Helper to define images sizes based on taken colums in Bootstrap grid for each breakpoint.
  • Replace responsive image in Gutenberg image block.
  • Generates sanitized and minified <svg> code, which can be safely inlined to HTML.

Code

<?php
$sizes = img_sizes([
	0 => [400, 250], // 400x250px cropped image for viewport 0 - 575px
	576 => 500, // 500px wide, proportional height image for viewpoer 576 - 767px
	768 => 700, // ...
	992 => 900,
	1200 => 1100
]);
img_upload($attachment_id)->responsiveImgTag($sizes, ['class' => 'img-fluid', 'alt' => 'My image']);

Output

<img
	src="https://res.cloudinary.com/your-name/q_auto:eco,f_auto,w_1100,c_fit/your-project/2021/05/your-image.jpg"
	sizes="(min-width: 1200px) 1100px,
		(min-width: 992px) 900px,
		(min-width: 768px) 700px,
		(min-width: 576px) 500px,
		400px"
	srcset="https://res.cloudinary.com/your-name/q_auto:eco,f_auto,w_1100,c_fit/your-project/2021/05/your-image.jpg 1100w,
		https://res.cloudinary.com/your-name/q_auto:eco,f_auto,w_900,c_fit/your-project/2021/05/your-image.jpg 900w,
		https://res.cloudinary.com/your-name/q_auto:eco,f_auto,w_700,c_fit/your-project/2021/05/your-image.jpg 700w,
		https://res.cloudinary.com/your-name/q_auto:eco,f_auto,w_500,c_fit/your-project/2021/05/your-image.jpg 500w,
		https://res.cloudinary.com/your-name/q_auto:eco,f_auto,w_400,h_250,c_fill/your-project/2021/05/your-image.jpg 400w"
	width="1100" height="619"
	alt="My image"
	class="img-fluid">

Thanks a lot to Adam Laita and his post about image optimization (in Czech only). It was the original inspiration for this library.

Instalation

Option 1: with Composer

Recommended way to install the library is using Composer:

composer require fronty/responsive-images

Include composer's autoload in functions.php:

// functions.php
require_once(__DIR__ . '/vendor/autoload.php');

Option 2: without Composer

  1. Download source code of the library and unzip it into your theme directory.
  2. Require files manually in functions.php
// functions.php

require_once(__DIR__ . '/responsive-images-master/src/Sizes/ImageSize.php');
require_once(__DIR__ . '/responsive-images-master/src/Sizes/ImageSizeList.php');
require_once(__DIR__ . '/responsive-images-master/src/Sizes/BootstrapSizes.php'); // Required only if you use Bootstrap

require_once(__DIR__ . '/responsive-images-master/src/BaseImage.php');
require_once(__DIR__ . '/responsive-images-master/src/UploadImage.php');
require_once(__DIR__ . '/responsive-images-master/src/ThemeImage.php');

WP Plugins

  1. Install Fly Dynamic Image Resizer and activate it on local development version. You can use this on production as well, but you would miss the speed of CDN.

  2. On production version install, activate and configure Auto Cloudinary, which will automatically upload all your images to Cloudinary using it's fetch API.

This library decides whether to use Fly Dynamic Image Resizer or Auto Cloudinary plugin, but make sure only one of these plugins is activated at a time. If none of these plugins is activated, library will fall back to default Wordpress attachment getter wp_get_attachment_image_src() with size given as array.

The library can work with SVGs as well. To support SVGs in WP Media Library, install SVG Support.

With this, you are ready to instantiate all classes described below directly in your template files. However to optimize your images and code as much as possible, there are more steps suggested.

Working with image sizes

To work with responsive images, we first has to prepare one or more image sizes for various breakpoints. Image sizes can be defined using following classes:

See detailed documentation of these objects.

Working with images

Library includes two main classes for work with images:

See detailed documentation of these objects.

WP filters

Both ThemeImage and UploadImage objects allows you to change some of their outputs using WP filter mechanism.

See list of all filters available.

Dependencies

Nette\Utils

The code depends on lightweight utility classes from Nette\Utils, especially following classes:

Other dependencies

@todo - nemazat src v lazyloadingu, misto toho dat placeholder (doplnit do konfigurace) - UploadImage

统计信息

  • 总下载量: 147
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固