bernhard

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.