Jump to content

Textformatter: Relative Image Urls to HttpUrls

Recommended Posts

I need to send E-Mails with embedded images. Therefore the image urls need to be absolute httpUrls instead of relative ones like /site/assets/...; I didn't find one, so I created this:

<?php namespace ProcessWire;

 * Converts relative CKEditor image urls to httpUrls
 * eg /site/assets/files/123/demo.png
 * to https://www.demo.com/site/assets/files/123/demo.png

class TextformatterImagesHttpUrl extends Textformatter {

  public static function getModuleInfo() {
    return array(
      'title' => 'Relative Image Urls to HttpUrls', 
      'version' => '1.0.0', 
      'summary' => "Converts relative CKEditor image urls to httpUrls.", 

  public function format(&$str) {
    $url = $this->wire->config->urls->files;
    $httpUrl = $this->wire->pages->get(1)->httpUrl;
    $httpUrl .= ltrim($url, "/");
    $url = str_replace("/", "\/", $url);
    $str = preg_replace("/src=\"$url/m", "src=\"$httpUrl", $str);
  • Like 3
  • Thanks 1

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...