src attribute of img tags


With this: item_image('square_thumbnail')

I get something like this:

<img src="http://localhost:8080/omeka3/files/square_thumbnails/a1641b89b518599b049efa6017f92040.jpg" alt="altText" title="Title">

But I need only the value of the src atribute (http://localhost:8080/omeka3/files/square_thumbnails/a1641b89b518599b049efa6017f92040.jpg)

How to do it?


Solved with:

$img_tag = item_image('square_thumbnail'); //devuelve la etiqueta <img> entera
$doc = new DOMDocument();
$doc->loadHTML($img_tag); // carga el html
$xpath = new DOMXPath($doc);
$nodelist = $xpath->query("//img"); // busca la imagen
$node = $nodelist->item(0); // obtiene la primera imagen
$value = $node->attributes->getNamedItem('src')->nodeValue;

echo "<meta itemprop='thumbnailUrl' content='".$value."'/>";

You might get there more directly with something like this, which is what those Omeka functions do

// Use the default representative file.
        $file = $record->getFile();
        if (!$file) {
            return false;

        if ($file->hasThumbnail()) {
            $uri = $file->getWebPath($format);
        } else {
            $uri = img($this->_getFallbackImage($file));
        $props['src'] = $uri;

In that, the $record is the item you are looking at on the page. The $uri is what item_image uses to produce the src attribute on the image. So, if you have the record, you could more directly get at the url to the image without going through the DOMDocument steps.

usefull! thanks