waughj/wp-theme-picture

WordPress主题图像自动生成图片标签HTML的类。

v0.3.1 2019-06-04 20:11 UTC

This package is auto-updated.

Last update: 2024-09-05 08:19:31 UTC


README

WordPress主题图像自动生成图片标签HTML的类。

与HTMLPicture一样工作,但自动应用WPThemeImage目录,而不是期望用户自己提供。

对于在https://www.example.com上设置的名为“example”的主题

use WaughJ\WPThemePicture\WPThemePicture;

$picture = new WPThemePicture( 'photo', 'jpg', '320w 240h, 800w 400h, 1200w 800h', [ 'directory' => 'img' ] );
$picture->print();

//    Will print out the following ( #s after m?= will vary ):
//    <picture>
//        <source srcset="https://www.example.com/wp-content/themes/example/img/photo-320x240.jpg?m=1554999461" media="(max-width:320px)">
//        <source srcset="https://www.example.com/wp-content/themes/example/img/photo-800x400.jpg?m=1554999461" media="(max-width:800px)">
//        <source srcset="https://www.example.com/wp-content/themes/example/img/photo-1200x800.jpg?m=1554999461" media="(min-width:801px)">
//        <img src="https://www.example.com/wp-content/themes/example/img/photo-320x240.jpg?m=1554999461" alt="" />
//    </picture>

如果版本设置为(默认)且服务器找不到文件,则此功能将抛出MissingFileException。有关更多信息,请参阅HTMLPicture文档。

处理异常的简单方法

use WaughJ\FileLoader\MissingFileException;
use WaughJ\WPThemePicture\WPThemePicture;

try
{
	$picture = new WPThemePicture( 'photo', 'jpg', '320w 240h, 800w 400h, 1200w 800h, 2400w 600h', [ 'directory' => 'img' ] );
}
catch ( MissingFileException $e )
{
	$picture = $e->getFallbackContent();
}

$picture->print(); // Will print without versioning for files that can’t be found & won’t throw exception.

变更日志

0.3.0

  • 更新到HTMLPicture最新版本

0.2.0

  • 使代码与新HTMLPicture兼容

0.1.0

  • 首次发布