abdullahzubair/svg-upload-fix 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

abdullahzubair/svg-upload-fix

Composer 安装命令:

composer require abdullahzubair/svg-upload-fix

包简介

SVG Upload Fix - A module that modifies the image field upload process to bypass image validation for SVG files.

README 文档

README

When I first received the requirement to support SVG files on my Drupal 7 site, I was excited to explore my options. After some research, I decided to use the SVG Image module, which seemed simple and aligned perfectly with my needs. However, I was taken aback when I enabled SVG support for an image field. Upon attempting to upload an SVG file through the node creation form, I encountered the error:

“The specified file 'default.svg' could not be uploaded. The image file is invalid or the image type is not allowed.”

To make matters worse, when I tried to set an SVG as the default image, I received another frustrating error:

“'default.svg' could not be uploaded. Only files with the following extensions are allowed: jpg, jpeg, gif, png, txt, doc, xls, pdf, ppt, pps, odt, ods, odp.”

Determined to find a solution, I dove into the issue and came across an ongoing discussion in Issue #3458204. It became evident that I wasn't alone in facing this challenge. While a patch had been provided by a dear friend, Sam, I was hesitant to modify the core functionality of Drupal. Though I tried the patch and successfully resolved the node creation error, the default image issue persisted.

After digging deeper, I realized that when uploading files through the node creation form, Drupal attempts to validate the image type. Since raster and vector images are fundamentally different, SVG files were not being validated correctly, leading to the errors I was encountering.

To address this, I implemented a condition that checks if the field type is an image and whether SVG is included in the allowed file extensions. If an SVG is detected among the uploaded files, I modified the validation process to skip the standard image validation for that specific SVG file. This way, raster images would still be validated normally, while SVG files could be uploaded seamlessly without any hassle.

With the first issue resolved, I turned my attention to the second error: the inability to set an SVG as a default image. I realized that the default image upload process was not recognizing SVGs due to its reliance on the core allowed extensions, defined in includes/file.inc. Again, I was reluctant to modify the core, as I wanted to maintain best practices in my development.

Instead, I altered the field edit form, applying similar conditions to allow SVG as a valid extension for default images. With this change, SVG files could be uploaded successfully as default images as well.

During this process, I discovered another important detail about default images. Each image field has two instances: a default instance that is applied site-wide and a field-specific instance that overwrites the default for that particular field. To accommodate this, I ensured my solution worked for both scenarios, allowing for SVG uploads in both instances.

Finally, I wanted to customize the upload path for default images. While I could easily set custom paths for user-uploaded images, the default images were restricted to the default_images directory within the public files directory, specifically public://default_images/. To improve this, I added a feature that checks if a specific file directory is set for the field being edited. If so, the module would place the default image in that directory, ensuring better organization for uploaded SVG files.

I’m sharing this module publicly to help others who might encounter the same issues I faced. It took a considerable amount of time for research and debugging, but I’m glad to have created a solution that can benefit others in the Drupal community. Thank you!

abdullahzubair/svg-upload-fix 适用场景与选型建议

abdullahzubair/svg-upload-fix 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 10 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 abdullahzubair/svg-upload-fix 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 abdullahzubair/svg-upload-fix 我们能提供哪些服务?
定制开发 / 二次开发

基于 abdullahzubair/svg-upload-fix 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0
  • 更新时间: 2024-10-08