Skip to content

Commit

Permalink
Issue #94: Add SVG support for drag-and-drop upload (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
indigoxela authored May 3, 2024
1 parent a11de24 commit 38d2da6
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions tinymce.pages.inc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ function tinymce_image_upload($format) {
}

$destination = $upload_settings['scheme'] . '://' . $upload_settings['directory'];
$extensions = image_get_supported_extensions();
$validators = array(
'file_validate_is_image' => array(),
'file_validate_extensions' => array(implode(' ', $extensions)),
'tinymce_validate_is_image' => array(),
);
if ($upload_settings['max_size']) {
$validators['file_validate_size'] = array(parse_size($upload_settings['max_size']));
Expand All @@ -52,8 +54,8 @@ function tinymce_image_upload($format) {
'uploaded' => 1,
'location' => $url,
'fileId' => $file->fid,
'width' => $image_info['width'],
'height' => $image_info['height'],
'width' => !empty($image_info['width']) ? $image_info['width'] : '',
'height' => !empty($image_info['height']) ? $image_info['height'] : '',
);
}
else {
Expand All @@ -73,3 +75,28 @@ function tinymce_image_upload($format) {

return $response;
}

/**
* Custom validation callback for image upload.
*
* Currently file_validate_is_image() fails for newly uploaded files, as the uri
* is something like "/tmp/phpAbCde" - without file extension.
* @see https://github.com/backdrop/backdrop-issues/issues/6497
*/
function tinymce_validate_is_image(File $file) {
$errors = array();

// SVG have their own validator on upload (file_validate_svg).
// If SVG aren't supported, yet, TinyMCE would refuse to upload in the first
// place and even if upload was attempted, the server-side extension based
// validation would fail.
if ($file->filemime == 'image/svg+xml') {
return $errors;
}
// Validate that the image toolkit can handle this raster image.
if (!$info = image_get_info($file->uri)) {
$errors[] = t('The image appears to be broken.');
}

return $errors;
}

0 comments on commit 38d2da6

Please sign in to comment.