You are here

function _print_access_images_via_file in Printer, email and PDF versions 7.2

Convert image paths to the file:// protocol.

In some Drupal setups, the use of the 'private' filesystem or Apache's configuration prevent access to the images of the page. This function tries to circumnvent those problems by accessing files in the local filesystem.

Parameters

string $html: Contents of the post-processed template already with the node data.

bool $images_via_file: If TRUE, convert also files in the 'public' filesystem to local paths.

Return value

string converted file names

4 calls to _print_access_images_via_file()
print_epub_phpepub_print_epub_generate in print_epub/lib_handlers/print_epub_phpepub/print_epub_phpepub.pages.inc
Implements hook_print_epub_generate().
print_pdf_dompdf_print_pdf_generate in print_pdf/lib_handlers/print_pdf_dompdf/print_pdf_dompdf.pages.inc
Implements hook_print_pdf_generate().
print_pdf_mpdf_print_pdf_generate in print_pdf/lib_handlers/print_pdf_mpdf/print_pdf_mpdf.pages.inc
Implements hook_print_pdf_generate().
print_pdf_tcpdf_print_pdf_generate in print_pdf/lib_handlers/print_pdf_tcpdf/print_pdf_tcpdf.pages.inc
Implements hook_print_pdf_generate().

File

includes/print.inc, line 84
Common functions used by several of the print modules.

Code

function _print_access_images_via_file($html, $images_via_file) {
  global $base_url, $language;
  $lang = function_exists('language_negotiation_get_any') && language_negotiation_get_any('locale-url') ? $language->language : '';

  // Always convert private to local paths.
  $pattern = "!(<img\\s[^>]*?src\\s*?=\\s*?['\"]?){$base_url}/(?:(?:index.php)?\\?q=)?(?:{$lang}/)?system/files/([^>\\?]*)(?:\\?itok=[a-zA-Z0-9\\-_]{8})?([^>]*?>)!is";
  $replacement = '$1file://' . realpath(variable_get('file_private_path', '')) . '/$2$3';
  $html = preg_replace($pattern, $replacement, $html);
  if ($images_via_file) {
    $pattern = "!(<img\\s[^>]*?src\\s*?=\\s*?['\"]?){$base_url}/(?:(?:index.php)?\\?q=)?(?:{$lang}/)?([^>\\?]*)(?:\\?itok=[a-zA-Z0-9\\-_]{8})?([^>]*?>)!is";
    $replacement = '$1file://' . DRUPAL_ROOT . '/$2$3';
    $html = preg_replace($pattern, $replacement, $html);
  }
  return $html;
}