0

I need to make next feature on my site: user writing an article and attach image in it, images are often stored not on localhost. I need to download this images to localhost and replace links to localhost images.

For example:

<img ... src="http://bob.com/img/image1.png" ... >
<img ... src="http://bob.com/img/image2.png" .... >

Script will find src content, download images and replace it like this:

<img ... src="/images/image1.png" ... >
<img ... src="/images/image2.png" .... >

I understand how to parse all src from code:

$subject = # i will put there article content (with img tags etc)
$result = array();
preg_match("/<img.*?src="(.*?)".*?>/", $subject, $result);

Now $result array will contain all link to images. Nice. Now I have some questions.

1) If I use preg_replace, will it help me to solve this task? In my opinion not, because preg_replace will replace content instantly (so I can't download image, create new link for stored on localhost image and somehow set this as argument for preg_replace, because it is run yet). Am I right with this assumption?

2) Okay. I can form an array, like I said. After that I download all images from this array. After that, somehow, I will replace all old images, for new images. I think it is more realistic. Am I right?

Something like that:

$subject = # i will put there article content (with img tags etc)
$result = array();
preg_match("/<img.*?src="(.*?)".*?>/", $subject, $result);

foreach($result as $src)
{
 $new_src = downloadImage($src);
 # somehow replace old image with new image there. How?
}

3) How exactly I can replace links if I will use 2nd method?

Sharikov Vladislav
  • 7,049
  • 9
  • 50
  • 87

2 Answers2

1

A Php DOMDocument Example how to manipulate HTML image tags.

$dom=new DOMDocument();
$dom->loadHTML($html_src);
$imgs = $dom->getElementsByTagName('img');
foreach($imgs as $img){

    $img_src = $img->getAttribute('src'); // will give you the src String

    //do something here

    $img->setAttribute('src',$new_src); // change your src= value

}

You can manipulate everything with setAttribute when the Attribute exist.

If you want be sure, that src is set, then you can use hasAttribute

demonking
  • 2,584
  • 5
  • 23
  • 28
  • 1 second. `$html_src` - what is content of this variable? And will `Php::DomDocument` will work with variables? I mean... User wrote an article, I will save it in some variable. Can I work with this variable by `Php::DomDocument`? Well, 2nd question is not too important, if I will know answer for first one. – Sharikov Vladislav Jan 28 '14 at 16:56
  • in my example $html_src is the html content from the page to show you how to load the html source – demonking Jan 29 '14 at 08:51
  • Ok. But I need to swap links in a text, which was inputted by user. Is it possible? Like so: `$text = $_POST['article_text']; $dom=new DOMDocument(); $dom->loadHTML($text); .... #etc` – Sharikov Vladislav Jan 29 '14 at 09:40
  • Yes, you could use it like this, when your $_POST variable contains html code, but maybe you have to overthink it, when you have to send HTML code through $_POST. – demonking Jan 29 '14 at 12:47
  • I am adding news/article to my site. I fill in all fields in form (title, message, tags etc...), pressing send button and handler, which will get `$_POST` parameters loading. Whats wrong? How I can do it another ways? – Sharikov Vladislav Jan 29 '14 at 16:07
1

I think you need preg_replace_callback but keep in mind that downloadImage may fail. so handle the failure gracefully (a fallback image or a retry queue)

McFog
  • 51
  • 3