定制 daycode/stup-images 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

daycode/stup-images

Composer 安装命令:

composer require daycode/stup-images

包简介

Stup Images or Store Or Update Images is a Package For Storing / Updating the Images, More Clear Codes and Upgrade Readability

README 文档

README

Stup Image is a powerful Laravel package for storing and updating images with clean, readable code. Integrated with the Intervention Image library, it provides an elegant solution for handling file uploads with advanced features like automatic resizing, configurable filename hashing, and flexible storage options.

Downloads License

✨ Features

  • 🚀 Simple and intuitive API for file uploads
  • 🖼️ Automatic image resizing on upload
  • 🔒 Configurable filename hashing for security
  • 📁 Flexible storage disk configuration (local, S3, etc.)
  • 🔄 Sync upload (replace old file automatically)
  • 📦 Multiple file uploads support
  • 🗑️ Easy file deletion
  • ⚙️ Customizable configuration
  • ✅ File extension validation

📦 Installation

1. Install via Composer

composer require daycode/stup-images

2. Publish Configuration (Optional)

php artisan vendor:publish --tag=stup-image

This will create a config/stup-image.php file where you can customize the package behavior.

3. Configure Storage Disk

Make sure your config/filesystems.php has the desired disk configured. By default, the package uses the FILESYSTEM_DISK from your .env file.

FILESYSTEM_DISK=public

4. Link Storage (if using public disk)

php artisan storage:link

5. Clear Cache

php artisan optimize:clear

⚙️ Configuration

After publishing the config file, you can customize these options in config/stup-image.php:

return [
    // Hash filenames using MD5 + timestamp for security
    'hash_filename' => true,

    // Allowed file extensions. Use ['*'] for all, or specify: ['jpg', 'png', 'gif']
    'allowed_extensions' => ['*'],
];

🚀 Usage

Basic Setup

Add the Stupable trait to your controller or any class:

use Daycode\StupImage\Stupable;

class UserController extends Controller
{
    use Stupable;
    
    // Your methods here...
}

📤 Upload File

Upload a single file to storage:

public function store(Request $request)
{
    $filename = $this->uploadFile(
        file: $request->file('avatar'),
        path: 'images/avatars'
    );

    User::create([
        'name' => $request->name,
        'avatar' => $filename,
    ]);

    return redirect()->back()->with('success', 'User created successfully!');
}

🖼️ Upload with Auto Resize

Automatically resize images during upload:

public function store(Request $request)
{
    // Resize to 800x600 pixels
    $filename = $this->uploadFile(
        file: $request->file('thumbnail'),
        path: 'images/thumbnails',
        resize: [800, 600]
    );

    Post::create([
        'title' => $request->title,
        'thumbnail' => $filename,
    ]);
}

🔄 Sync Upload (Update & Replace)

Upload a new file and automatically delete the old one:

public function update(Request $request, User $user)
{
    $filename = $user->avatar;

    if ($request->hasFile('avatar')) {
        $filename = $this->syncUploadFile(
            file: $request->file('avatar'),
            oldFileName: $user->avatar,
            path: 'images/avatars'
        );
    }

    $user->update([
        'name' => $request->name,
        'avatar' => $filename,
    ]);

    return redirect()->back()->with('success', 'User updated successfully!');
}

📦 Upload Multiple Files

Upload multiple files at once:

public function store(Request $request)
{
    $filenames = $this->uploadMultipleFiles(
        files: $request->file('gallery'),
        path: 'images/gallery'
    );

    foreach ($filenames as $filename) {
        Gallery::create([
            'image' => $filename,
        ]);
    }

    return redirect()->back()->with('success', 'Gallery uploaded successfully!');
}

🗑️ Delete File

Delete a file from storage:

public function destroy(User $user)
{
    $this->deleteFile(
        fileName: $user->avatar,
        path: 'images/avatars'
    );

    $user->delete();

    return redirect()->back()->with('success', 'User deleted successfully!');
}

📖 API Reference

uploadFile(UploadedFile $file, string $path, ?array $resize = []): string

Upload a single file to storage.

Parameters:

  • $file - The uploaded file instance
  • $path - Storage path (relative to your configured disk)
  • $resize - Optional. Array with [width, height] for automatic resizing

Returns: Filename (string) or throws UploadException

syncUploadFile(UploadedFile $file, ?string $oldFileName, string $path): string

Upload a new file and delete the old one.

Parameters:

  • $file - The new uploaded file
  • $oldFileName - The old filename to delete (nullable)
  • $path - Storage path
  • $resize - Optional. Array with [width, height] for automatic resizing

Returns: New filename (string)

uploadMultipleFiles(array $files, string $path): array

Upload multiple files at once.

Parameters:

  • $files - Array of uploaded files
  • $path - Storage path
  • $resize - Optional. Array with [width, height] for automatic resizing

Returns: Array of filenames or throws UploadException

deleteFile(string $fileName, string $path): void

Delete a file from storage.

Parameters:

  • $fileName - The filename to delete
  • $path - Storage path

🔧 Advanced Configuration

Using Different Storage Disks

The package respects your FILESYSTEM_DISK environment variable. To use different disks:

Local Storage:

FILESYSTEM_DISK=public

Amazon S3:

FILESYSTEM_DISK=s3
AWS_ACCESS_KEY_ID=your-key
AWS_SECRET_ACCESS_KEY=your-secret
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=your-bucket

DigitalOcean Spaces, Cloudinary, etc.: Configure your disk in config/filesystems.php and set FILESYSTEM_DISK accordingly.

Restrict File Extensions

Edit config/stup-image.php:

'allowed_extensions' => ['jpg', 'jpeg', 'png', 'gif', 'webp'],

Disable Filename Hashing

If you want to keep original filenames:

'hash_filename' => false,

🛡️ Security

  • Filenames are hashed by default using MD5 + timestamp to prevent conflicts and enhance security
  • File extension validation prevents unauthorized file types
  • Uses Laravel's Storage facade for secure file handling

📝 Examples

Complete CRUD Example

use Daycode\StupImage\Stupable;

class ProductController extends Controller
{
    use Stupable;

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'image' => 'required|image|max:2048',
        ]);

        $filename = $this->uploadFile(
            file: $request->file('image'),
            path: 'products',
            resize: [1200, 800]
        );

        Product::create([
            'name' => $request->name,
            'image' => $filename,
        ]);

        return redirect()->route('products.index');
    }

    public function update(Request $request, Product $product)
    {
        $request->validate([
            'name' => 'required',
            'image' => 'nullable|image|max:2048',
        ]);

        $filename = $product->image;

        if ($request->hasFile('image')) {
            $filename = $this->syncUploadFile(
                file: $request->file('image'),
                oldFileName: $product->image,
                path: 'products'
            );
        }

        $product->update([
            'name' => $request->name,
            'image' => $filename,
        ]);

        return redirect()->route('products.index');
    }

    public function destroy(Product $product)
    {
        $this->deleteFile($product->image, 'products');
        $product->delete();

        return redirect()->route('products.index');
    }
}

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

This package is open-sourced software licensed under the MIT license.

👨‍💻 Credits

💬 Support

If you find this package helpful, please consider giving it a ⭐ on GitHub and Packagist!

For issues or questions, please use the GitHub Issues page.

daycode/stup-images 适用场景与选型建议

daycode/stup-images 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 363 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 07 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 daycode/stup-images 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-24