Replace all images with <amp-img> tag and add size attributes

I’m looking to run through my content variable and replace all <img.... with <amp-img width="" height="" where I use getimagesize to find the width and heights for each image.

I have this code to get the size attributes, but I’m not sure how to use preg_replace to replace each image with its own attributes?

    $my_image = array_values(getimagesize('test.jpg'));
      //use list on new array
      list($width, $height, $type, $attr) = $my_image;

Thanks for any help!

Here you go:
Hope it helps.

Hi @Mohammed and welcome to the forum.

The link you supplied only replaces the amp-img tag and does not insert the image width or height dimensions.

I have temporarily removed the amp-img tags on one of my sites because not only are width and height explicitly required, it is also necessary to remove all inline style references.

Google removed over 500 offending pages from their search and even though the pages no longer have the amp-img references I do not know when Google will include the revised pages. I can see no way to simply re-instate the pages and marking the errors as cleared :frowning:

Thanks, this was great!
Re image width/height, I am only working with one size so have hard coded a replace to add these :slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.