softrang/image-helper

A simple image upload, update, and delete helper for Laravel and PHP.

Installs: 7

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/softrang/image-helper

v1.0.6 2025-10-15 10:24 UTC

This package is auto-updated.

Last update: 2025-10-15 10:27:52 UTC


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', then asset($image) will generate the correct URL.
  • If your images are stored inside subdirectories of public/uploads (like public/uploads/subfolder/filename.jpg), and your database does not store the full path, then asset($image) may not generate the correct URL.
  • Do not prepend 'uploads/' again if your database path already contains it.