Skip to content

Instantly share code, notes, and snippets.

@iam-raihan
Created August 8, 2020 14:37
Show Gist options
  • Save iam-raihan/b824c1d7eda574d7ad3533fa2a5fcac6 to your computer and use it in GitHub Desktop.
Save iam-raihan/b824c1d7eda574d7ad3533fa2a5fcac6 to your computer and use it in GitHub Desktop.
replace summernote image contents with storage path
function processSummerNote($content)
{
if (empty($content)) {
return $content;
}
$content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8');
$dom = new \DOMDocument('1.0', 'utf-8');
libxml_use_internal_errors(true);
$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$images = $dom->getElementsByTagName('img');
foreach ($images as $count => $image)
{
$src = $image->getAttribute('src');
if (preg_match('/data:image/', $src)) {
preg_match('/data:image\/(?<mime>.*?)\;/', $src, $groups);
$mimeType = $groups['mime'];
$uniqueId = str_random();
$path = "images/$uniqueId.$mimeType";
$fileContents = file_get_contents($src);
Storage::disk('s3')->put($path, $fileContents, 'public');
$image->removeAttribute('src');
$image->setAttribute('src', makeS3Url($path));
}
}
return $dom->saveHTML();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment