Re,
ok I ended up modifying the content of a CKEditor field before il gets saved.
I made a module with autoload=true and hooked it after the input field is processed.
the module will detect images that are from the web or encoded in base64 (begining with http, ftp and data) and fetch it , copy it to images field and replace the link with the new uploaded image link.
public function init() {
// add before-hook to the inputfield render method
$this->addHookAfter("InputfieldCKEditor::processInput", $this, "downloadPastedImages");
}
public function downloadPastedImages(HookEvent $event) {
// // get the current field
$field = $event->object;
if($field->name === 'projet_body' && $field->value !== '' ) {
$page = $this->modules->ProcessPageEdit->getPage();
// ===== custom class
$manipulator = new textManipulator;
// custom function of the class to get images src into a array
$images = $manipulator->getImagesArray($field->value);
// vérifie si c'est un lien image qui vient du web avec http en début
$images_fromweb = array();
foreach($images as $key=>$image){
if(substr( $image, 0, 4 ) === "http" || substr( $image, 0, 4 ) === "data" || substr( $image, 0, 3 ) === "ftp") $images_fromweb[] = $image;
}
$filename = $page->name."_image_";
// si on a des images du web non copié encore => on efface tout pour copier
if(count($images_fromweb) > 0){
// efface toute les images
// autre technique :
$page->images->removeAll();
$page->save();
}
//wire('pages')->uncache($page); // just in case we need the memory
foreach($images_fromweb as $key=>$image){
// save images if not in local server
$page->setOutputFormatting(false);
// copie de l'image vers le dossier de la page (sans extension encore)
$file = $page->images->path . $filename.$key;
$file_url = $page->images->url . $filename.$key;
$temp_image = file_get_contents($image);
file_put_contents($file, $temp_image);
// detecte l'extension
$ext = ".png";
if(mime_content_type($file) === "image/gif") {
$ext = ".gif";
} else if(mime_content_type($file) === "image/jpeg") {
$ext = ".jpg";
} else if(mime_content_type($file) === "image/bmp") {
$ext = ".bmp";
}else if(mime_content_type($file) === "image/svg+xml") {
$ext = ".svg";
}
// ajoute l'extension
rename($file,$file.$ext);
// remplace le chemin de l'image à présent copiée dans le texte source
$field->value = str_replace($image, $file_url.$ext, $field->value);
// add to page
$page->images->add($file.$ext);
// save the page
$page->save();
}
}
}