I guess you solved the problem by now but as I have been looking for the same answer and finally managed to make it work here is my approach:
First I get all the img src in summernote's textarea then I read all the img files in the folder on the server. After that, I unlink the files in the folder which are not in summernote's img src array.
$dom = new domDocument;
$dom->loadHTML(html_entity_decode($content));
$dom->preserveWhiteSpace = false;
$imgs = $dom->getElementsByTagName("img");
$links = array();
$path = '../content/'.$id.'/';
$files = glob($path . "*.*");
for($i = 0; $i < $imgs->length; $i++) {
$links[] = $imgs->item($i)->getAttribute("src");
}
$result=array_diff($files, $links);
foreach($result as $deleteFile) {
if (file_exists($deleteFile)) {
array_map('unlink', glob($deleteFile));
}
}
}
Hope this will help others.
EDIT to answer to @eachone:
You can use AJAX too (I do it with jQuery).
$('#DivContainingSummernoteTextarea').blur(function(){
$contentPost = $("#SummernoteTextarea").code();
doAjax('something');
});
I store the content of summernote's textarea in $contentPost. Then doAjax will call the PHP and POST $content. If the img on the server is not in $content it will be deleted.