softrang/image-helper
最新稳定版本:v1.0.6
Composer 安装命令:
composer require softrang/image-helper
包简介
A simple image upload, update, and delete helper for Laravel and PHP.
README 文档
README
A simple and developer-friendly helper for image upload, update, and delete in Laravel 12+, created by [Softrang](https://softrang.com).
- Upload images with optional resize (width & height)
- Update images (automatically deletes old image)
- Delete images safely from the public folder
- Optimize image quality and size (target 15–30 KB)
- Support for JPEG, PNG, GIF, WebP formats
1️⃣ Install via Composer
composer require softrang/image-helper
2️⃣ Upload Image
Upload an image with optional width and height. The function automatically resizes and optimizes the image.
$path = upload_image($request->file('image'), 'products', [ 'width' => 300, // Optional: Resize width 'height' => 300, // Optional: Resize height ]); // Save path to your database Product::create([ 'name' => $request->name, 'image' => $path ]);
3️⃣ Update Image
Update an image. This function automatically deletes the old image from the public folder and uploads the new one with optional resize.
$path = update_image($request->file('image'), $request->old_image, 'products', [ 'width' => 300, // Optional: Resize width 'height' => 300, // Optional: Resize height ]); // Update your database with new path $product->update([ 'image' => $path ]);
4️⃣ Delete Image
$image = $request->image; // Get image path from database delete_image($image);
5️⃣ Supported Image Formats
- jpg / jpeg
- png (supports transparency)
- gif
- webp (supports transparency)
6️⃣ Features
- ✅ Resize Images with optional width & height
- ✅ Quality Optimization to keep images between 15–30 KB
- ✅ Transparency Support for PNG & WebP
- ✅ Auto Delete Old Image on update
- ✅ Easy to Integrate in Laravel 12+ projects
- ✅ Standalone: No external packages required
7️⃣ Example Controller
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class ProductController extends Controller { public function store(Request $request) { $path = upload_image($request->file('image'), 'products', [ 'width' => 300, 'height' => 300 ]); return response()->json(['uploaded' => $path]); } public function update(Request $request) { $path = update_image($request->file('image'), $request->old_image, 'products', [ 'width' => 300, 'height' => 300 ]); return response()->json(['updated' => $path]); } public function destroy(Request $request) { delete_image($request->image); return response()->json(['deleted' => true]); } }
8️⃣ Notes
- Automatically creates directories if they don’t exist
- Optimized for performance and small file size
- Easy to customize allowed file types
9️⃣ License
MIT License – free to use, modify, and distribute.
1️⃣0️⃣ How to Display the Image
After storing the image path in your database, you can display it in Blade like this:
@php
$image = $request->image; // or from your database column
@endphp
<img src="{{ asset($image) }}" alt="Uploaded Image">
Note:
- If your images are stored inside
public/uploads, and your database stores'uploads/filename.jpg', thenasset($image)will generate the correct URL. - If your images are stored inside subdirectories of
public/uploads(likepublic/uploads/subfolder/filename.jpg), and your database does not store the full path, thenasset($image)may not generate the correct URL. - Do not prepend
'uploads/'again if your database path already contains it.
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-15