wp-cli/media-command

将文件作为附件导入,重新生成缩略图或列出已注册的图像尺寸。

安装次数: 4,771,592

依赖项: 6

建议者: 1

安全性: 0

星标: 44

关注者: 10

分支: 41

公开问题: 18

语言:Gherkin

类型:wp-cli-package

v2.2.0 2024-06-06 09:32 UTC

README

将文件作为附件导入,重新生成缩略图或列出已注册的图像尺寸。

Testing

快速链接: 使用 | 安装 | 贡献 | 支持

使用

此包实现了以下命令

wp media

将文件作为附件导入,重新生成缩略图或列出已注册的图像尺寸。

wp media

示例

# Re-generate all thumbnails, without confirmation.
$ wp media regenerate --yes
Found 3 images to regenerate.
1/3 Regenerated thumbnails for "Sydney Harbor Bridge" (ID 760).
2/3 Regenerated thumbnails for "Boardwalk" (ID 757).
3/3 Regenerated thumbnails for "Sunburst Over River" (ID 756).
Success: Regenerated 3 of 3 images.

# Import a local image and set it to be the featured image for a post.
$ wp media import ~/Downloads/image.png --post_id=123 --title="A downloaded picture" --featured_image
Imported file '/home/person/Downloads/image.png' as attachment ID 1753 and attached to post 123 as featured image.
Success: Imported 1 of 1 images.

# List all registered image sizes
$ wp media image-size
+---------------------------+-------+--------+-------+
| name                      | width | height | crop  |
+---------------------------+-------+--------+-------+
| full                      |       |        | N/A   |
| twentyfourteen-full-width | 1038  | 576    | hard  |
| large                     | 1024  | 1024   | soft  |
| medium_large              | 768   | 0      | soft  |
| medium                    | 300   | 300    | soft  |
| thumbnail                 | 150   | 150    | hard  |
+---------------------------+-------+--------+-------+

# Fix orientation for specific images.
$ wp media fix-orientation 63
1/1 Fixing orientation for "Portrait_6" (ID 63).
Success: Fixed 1 of 1 images.

wp media import

从本地文件或URL创建附件。

wp media import <file>... [--post_id=<post_id>] [--post_name=<post_name>] [--file_name=<name>] [--title=<title>] [--caption=<caption>] [--alt=<alt_text>] [--desc=<description>] [--skip-copy] [--preserve-filetime] [--featured_image] [--porcelain[=<field>]]

选项

<file>...
	Path to file or files to be imported. Supports the glob(3) capabilities of the current shell.
	    If file is recognized as a URL (for example, with a scheme of http or ftp), the file will be
	    downloaded to a temp file before being sideloaded.

[--post_id=<post_id>]
	ID of the post to attach the imported files to.

[--post_name=<post_name>]
	Name of the post to attach the imported files to.

[--file_name=<name>]
	Attachment name (post_name field).

[--title=<title>]
	Attachment title (post title field).

[--caption=<caption>]
	Caption for attachment (post excerpt field).

[--alt=<alt_text>]
	Alt text for image (saved as post meta).

[--desc=<description>]
	"Description" field (post content) of attachment post.

[--skip-copy]
	If set, media files (local only) are imported to the library but not moved on disk.
	File names will not be run through wp_unique_filename() with this set.

[--preserve-filetime]
	Use the file modified time as the post published & modified dates.
	Remote files will always use the current time.

[--featured_image]
	If set, set the imported image as the Featured Image of the post it is attached to.

[--porcelain[=<field>]]
	Output a single field for each imported image. Defaults to attachment ID when used as flag.
	---
	options:
	  - url
	---

示例

# Import all jpgs in the current user's "Pictures" directory, not attached to any post.
$ wp media import ~/Pictures/**\/*.jpg
Imported file '/home/person/Pictures/landscape-photo.jpg' as attachment ID 1751.
Imported file '/home/person/Pictures/fashion-icon.jpg' as attachment ID 1752.
Success: Imported 2 of 2 items.

# Import a local image and set it to be the post thumbnail for a post.
$ wp media import ~/Downloads/image.png --post_id=123 --title="A downloaded picture" --featured_image
Imported file '/home/person/Downloads/image.png' as attachment ID 1753 and attached to post 123 as featured image.
Success: Imported 1 of 1 images.

# Import a local image, but set it as the featured image for all posts.
# 1. Import the image and get its attachment ID.
# 2. Assign the attachment ID as the featured image for all posts.
$ ATTACHMENT_ID="$(wp media import ~/Downloads/image.png --porcelain)"
$ wp post list --post_type=post --format=ids | xargs -d ' ' -I % wp post meta add % _thumbnail_id $ATTACHMENT_ID
Success: Added custom field.
Success: Added custom field.

# Import an image from the web.
$ wp media import http://s.wordpress.org/style/images/wp-header-logo.png --title='The WordPress logo' --alt="Semantic personal publishing"
Imported file 'http://s.wordpress.org/style/images/wp-header-logo.png' as attachment ID 1755.
Success: Imported 1 of 1 images.

# Get the URL for an attachment after import.
$ wp media import http://s.wordpress.org/style/images/wp-header-logo.png --porcelain | xargs -I {} wp post list --post__in={} --field=url --post_type=attachment
http://wordpress-develop.dev/wp-header-logo/

wp media regenerate

重新生成一个或多个附件的缩略图。

wp media regenerate [<attachment-id>...] [--image_size=<image_size>] [--skip-delete] [--only-missing] [--delete-unknown] [--yes]

选项

[<attachment-id>...]
	One or more IDs of the attachments to regenerate.

[--image_size=<image_size>]
	Name of the image size to regenerate. Only thumbnails of this image size will be regenerated, thumbnails of other image sizes will not.

[--skip-delete]
	Skip deletion of the original thumbnails. If your thumbnails are linked from sources outside your control, it's likely best to leave them around. Defaults to false.

[--only-missing]
	Only generate thumbnails for images missing image sizes.

[--delete-unknown]
	Only delete thumbnails for old unregistered image sizes.

[--yes]
	Answer yes to the confirmation message. Confirmation only shows when no IDs passed as arguments.

示例

# Regenerate thumbnails for given attachment IDs.
$ wp media regenerate 123 124 125
Found 3 images to regenerate.
1/3 Regenerated thumbnails for "Vertical Image" (ID 123).
2/3 Regenerated thumbnails for "Horizontal Image" (ID 124).
3/3 Regenerated thumbnails for "Beautiful Picture" (ID 125).
Success: Regenerated 3 of 3 images.

# Regenerate all thumbnails, without confirmation.
$ wp media regenerate --yes
Found 3 images to regenerate.
1/3 Regenerated thumbnails for "Sydney Harbor Bridge" (ID 760).
2/3 Regenerated thumbnails for "Boardwalk" (ID 757).
3/3 Regenerated thumbnails for "Sunburst Over River" (ID 756).
Success: Regenerated 3 of 3 images.

# Re-generate all thumbnails that have IDs between 1000 and 2000.
$ seq 1000 2000 | xargs wp media regenerate
Found 4 images to regenerate.
1/4 Regenerated thumbnails for "Vertical Featured Image" (ID 1027).
2/4 Regenerated thumbnails for "Horizontal Featured Image" (ID 1022).
3/4 Regenerated thumbnails for "Unicorn Wallpaper" (ID 1045).
4/4 Regenerated thumbnails for "I Am Worth Loving Wallpaper" (ID 1023).
Success: Regenerated 4 of 4 images.

# Re-generate only the thumbnails of "large" image size for all images.
$ wp media regenerate --image_size=large
Do you really want to regenerate the "large" image size for all images? [y/n] y
Found 3 images to regenerate.
1/3 Regenerated "large" thumbnail for "Sydney Harbor Bridge" (ID 760).
2/3 No "large" thumbnail regeneration needed for "Boardwalk" (ID 757).
3/3 Regenerated "large" thumbnail for "Sunburst Over River" (ID 756).
Success: Regenerated 3 of 3 images.

wp media image-size

列出WordPress注册的图像尺寸。

wp media image-size [--fields=<fields>] [--format=<format>]

选项

[--fields=<fields>]
	Limit the output to specific fields. Defaults to all fields.

[--format=<format>]
	Render output in a specific format
	---
	default: table
	options:
	  - table
	  - json
	  - csv
	  - yaml
	  - count
	---

可用字段

以下字段将默认显示每个图像尺寸

  • 名称
  • 宽度
  • 高度
  • 裁剪
  • 比例

示例

# List all registered image sizes
$ wp media image-size
+---------------------------+-------+--------+-------+-------+
| name                      | width | height | crop  | ratio |
+---------------------------+-------+--------+-------+-------+
| full                      |       |        | N/A   | N/A   |
| twentyfourteen-full-width | 1038  | 576    | hard  | 173:96|
| large                     | 1024  | 1024   | soft  | N/A   |
| medium_large              | 768   | 0      | soft  | N/A   |
| medium                    | 300   | 300    | soft  | N/A   |
| thumbnail                 | 150   | 150    | hard  | 1:1   |
+---------------------------+-------+--------+-------+-------+

安装

此包包含在WP-CLI本身中,无需额外安装。

要安装此包的最新版本,覆盖WP-CLI中包含的版本,请运行

wp package install [email protected]:wp-cli/media-command.git

贡献

我们感谢您主动为这个项目做出贡献。

贡献不仅仅限于代码。我们鼓励您根据自己的能力以最适合的方式做出贡献,例如撰写教程、在当地聚会中进行演示、帮助其他用户解决支持问题或修订我们的文档。

要了解更多信息,请查看WP-CLI贡献指南。此包遵循那些政策和指南。

报告错误

认为你找到了错误?我们非常希望你能帮助我们修复它。

在创建新的问题之前,你应该搜索现有问题,看看是否有现有解决方案,或者它是否已在更新的版本中修复。

在你进行了一些搜索并发现没有为你这个错误打开或修复的问题后,请创建一个新问题。请尽可能提供详细信息,并在可能的情况下提供清晰的复制步骤。有关更多指导,请查看我们的错误报告文档

创建拉取请求

想要贡献一个新功能?请首先打开一个新问题,讨论这个功能是否适合该项目。

一旦你决定投入时间来推进你的拉取请求,请遵循我们创建拉取请求的指南,以确保它是一个愉快的体验。有关在本地为此包工作的具体信息,请参阅"设置"。

支持

GitHub问题不适合一般性支持问题,但您还可以尝试其他途径:https://wp-cli.org/#support

此README.md文件是使用wp scaffold package-readme文档)从项目的代码库动态生成的。若要提出更改建议,请向代码库的相应部分提交pull request。