Macrotone Blogs

Macrotone blogs upon Joomla, our products and other matters.
Font size: +

Emailing €˜items€™ with relative image URLs from Joomla.

issues-48We have been looking at a small ‘opportunity’ which we discovered when we were emailing material such as an ‘article’ or Issue Tracker ‘issue’ from our Joomla system.

The problem is quite simple to explain since it can be caused by the content editor that is forced to use relative URLs.

This can be resolved by creating a new profile in the editor (such as JCE) which is configured to use absolute urls and then assign the profile to the particular component where absolute URLs are needed.

There is a small note on it for JCE which explains how to do this in more detail.

There is of course an alternative which we have investigated which is to change the relative URL to an absolute URL in our emailing routine.


1. We are basically looking for the string ‘src=&#34’ i.e. as in the following:    src=”media/somepicture.png”

2. We then have to ensure that all of the found strings are unique.  This would not be true if for example an image was in the ‘body’ more than once, or we happen to have text in the article using the same construct.

3. Since the return from the preg_match_all routine is a multi-dimensional array when we search for duplicate strings we need to ‘serialise’ and ‘unserialise’ it and for this we use a routine that we obtained from

4. Having done that we now can replace the string by adding the domain in front of it. We deliberately also add the search string ‘src="’ to our str_replace call as wee to ensure that any occurrences of the actual URL outside of the img tag are left untouched by the replace.  [We could of course be more rigorous and enhance the string search to ensure that it also includes the img tag as well, but we leave that to the reader and another day.]

We will use the following code which is added to our routine which builds up the email ‘body’.

      // Add conversion of relative img paths to absolute paths.
      $domain = JURI::root(); //get site root from Joomla
      preg_match_all('/src\="(.*?)"/im', $body, $matches);
      // Need to ensure that all of the entries in the $matches array are unique otherwise
      // the str_replace function will replace it again on subsequent call.
      $matches = self::super_unique($matches);
      foreach($matches[1] as $n=>$link) {
         if(substr($link, 0, 4) != 'http') {
            //$body = str_replace($matches[1][$n], $domain . $matches[1][$n], $body);
            $srcstr = "src=\"";  // Add in the src=" string itself otherwise the string without the src=" is also changed.
            $body = str_replace($srcstr.$matches[1][$n], $srcstr.$domain . $matches[1][$n], $body);

Then we add the following routine into our class file.

    * Handle multidimensional arrays for unique strings.
    * See:
   function super_unique($array)
      $result = array_map("unserialize", array_unique(array_map("serialize", $array)));
      foreach ($result as $key => $value) {
         if ( is_array($value) ) {
            $result[$key] = self::super_unique($value);
      return $result;

Now we have completed our code changes and it does indeed perform as expected.

Debugging PHP with Java console.
Trouble Shooting Flow Chart
Go To Top

The Macrotone Consulting Web site would like to use cookies to store information on your computer, to improve our website. Cookies used for the essential operation of the site have already been set. To find out more about the cookies we use and how to delete them, see our Privacy Policy.

I accept cookies from this site.