Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
153 views
in Q2A Core by
edited by

AMP theme is almost ready, I just want to solve a problem.

I want to convert from <img> to <amp-img> with width and height, this is my first project so I don't understand how to do it. Please explain in a little detail.

by
Umm...I think you should use javascript for changing them without making any change to the Q2A core.
by
Tell the details if you want to solve
by
Do you have the code on github?
by
+1
@ghaplaman Sorry, but I ain't familiar with javascript.But I believe you can use it for your purpose.And if you want help from peoples, you should upload it on github.So we can all make it better.

1 Answer

+2 votes
by

The basic code for just the tags would be this, as I posted on GitHub:

$content = str_replace('<img ', '<amp-img ', $content);

If the image has width and height attributes then that should be enough. It seems like the WYSIWYG editor adds it in style tags instead. So you'll need a regular expression, something along these lines should work:

$regex = '#<img (.+?) style="height:([0-9]+)px; width:([0-9]+)px">#';
$replace = '<amp-img $1 height="$2" width="$3"></amp-img>';
$content = preg_replace($regex, $replace, $content);

That should be useful as a starting point, hope it helps.

by
Thanks, but now there's a problem, only visitors can see the picture, but when you login, you can't see the picture.
by
Would you please provide the link?
...