如何捕捉空白尺寸的图像?
How to catch images with blank dimensions?
我创建了一个 WordPress plugin,它向所有图像插入 width
和 height
属性,例如:
<img src="https://placehold.it/100x50">
- to -
<img src="https://placehold.it/100x50" width="100" height="50">
然而,当存在空白尺寸的图像时(对于 width
和 height
),我的代码并没有捕捉到它,例如:
<img src="https://placehold.it/100x50" width="" height="">
但是,当只有一个空白维度时,我的插件会捕捉到它。以下两个示例将起作用:
<img src="https://placehold.it/100x50" width="">
<img src="https://placehold.it/100x50" height="">
Here's the source code. On line 43 (#),这是我用来确定图像是否缺少 width
/height
:
的逻辑
if ( ! in_array( 'width', $img[1] ) || ! in_array( 'height', $img[1] ) ) {
我尝试了以下更新逻辑,但它不起作用:
if ( # Images with no width/height
! in_array( 'width', $img[1] ) || ! in_array( 'height', $img[1] ) ||
# Images with blank width/height
( in_array( 'width', $img[1] ) && in_array( '""', $img[1] ) ) || ( in_array( 'height', $img[1] ) && in_array( '""', $img[1] ) )
) {
我在正确的区域吗?我的逻辑哪里做错了?
<img ... width="">
的 width=""
部分不会导致字符串 width
和 ""
作为 $img
的元素吗?这是一场比赛,那里是 non-empty,所以这里的 ! in_array
可能是错误的。
如果根本没有 width/height 属性,您的代码将被执行。
只需检查所有宽度和高度的匹配的具体值,如果它的计算结果为字符串 ""
,则与您的代码中的反应相同
edit:您更新后的逻辑假设两个捕获的组是一个字符串,但它们不是。因此,您需要 (in_array( 'width', $img[1] ) and in_array( '""', $img[1] ))
而不是 in_array( 'width=""', $img[1] )
,加上相同的高度。
我在 1
索引中搜索我应该在 2
索引中查找的值,其中包含以下值:
if ( # Images with no width/height
! in_array( 'width', $img[1] ) || ! in_array( 'height', $img[1] ) ||
# Images with blank width/height
( in_array( 'width', $img[1] ) && in_array( '""', $img[2] ) ) || ( in_array( 'height', $img[1] ) && in_array( '""', $img[2] ) )
) {
我创建了一个 WordPress plugin,它向所有图像插入 width
和 height
属性,例如:
<img src="https://placehold.it/100x50">
- to -
<img src="https://placehold.it/100x50" width="100" height="50">
然而,当存在空白尺寸的图像时(对于 width
和 height
),我的代码并没有捕捉到它,例如:
<img src="https://placehold.it/100x50" width="" height="">
但是,当只有一个空白维度时,我的插件会捕捉到它。以下两个示例将起作用:
<img src="https://placehold.it/100x50" width="">
<img src="https://placehold.it/100x50" height="">
Here's the source code. On line 43 (#),这是我用来确定图像是否缺少 width
/height
:
if ( ! in_array( 'width', $img[1] ) || ! in_array( 'height', $img[1] ) ) {
我尝试了以下更新逻辑,但它不起作用:
if ( # Images with no width/height
! in_array( 'width', $img[1] ) || ! in_array( 'height', $img[1] ) ||
# Images with blank width/height
( in_array( 'width', $img[1] ) && in_array( '""', $img[1] ) ) || ( in_array( 'height', $img[1] ) && in_array( '""', $img[1] ) )
) {
我在正确的区域吗?我的逻辑哪里做错了?
<img ... width="">
的 width=""
部分不会导致字符串 width
和 ""
作为 $img
的元素吗?这是一场比赛,那里是 non-empty,所以这里的 ! in_array
可能是错误的。
如果根本没有 width/height 属性,您的代码将被执行。
只需检查所有宽度和高度的匹配的具体值,如果它的计算结果为字符串 ""
edit:您更新后的逻辑假设两个捕获的组是一个字符串,但它们不是。因此,您需要 (in_array( 'width', $img[1] ) and in_array( '""', $img[1] ))
而不是 in_array( 'width=""', $img[1] )
,加上相同的高度。
我在 1
索引中搜索我应该在 2
索引中查找的值,其中包含以下值:
if ( # Images with no width/height
! in_array( 'width', $img[1] ) || ! in_array( 'height', $img[1] ) ||
# Images with blank width/height
( in_array( 'width', $img[1] ) && in_array( '""', $img[2] ) ) || ( in_array( 'height', $img[1] ) && in_array( '""', $img[2] ) )
) {