簡體   English   中英

如何轉換 <img> 到PHP中的AMP

[英]How to convert <img> to AMP in PHP

將舊的html代碼轉換為AMP版本時出現問題:

我的文檔中有很多圖像,但是在源代碼中只有寬度值。

<img src="/url-img1.jpg" with="728"></img>
<img src="/url-img2.jpg" with="640"></img>

在AMP版本中,我需要添加以下高度值:

<amp-img src="/url-img1.jpg" width="{widht-of-image}" height="{height-of-image}" layout="responsive"></amp-img>
<amp-img src="/url-img2.jpg" width="{widht-of-image}" height="{height-of-image}" layout="responsive"></amp-img>

誰能告訴我該怎么做?

您可以使用getimagesize()

list($width, $height) = getimagesize('path_to_the_image/url-img1.jpg');
echo '<amp-img src="/url-img1.jpg" width="'. $width .'" height="'. $height . '" layout="responsive"></amp-img>';

但是,這有損AMP的目的,即在最后加載資源。 在他們這邊進行緩存應該可以解決這些問題,但是我仍然建議您對寬度進行硬編碼,對高度進行硬編碼。 您可以將其自動化一次,然后全部替換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM