PHP code example of mainul12501 / drag-drop-crop-image-upload
1. Go to this page and download the library: Download mainul12501/drag-drop-crop-image-upload library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mainul12501 / drag-drop-crop-image-upload example snippets
{!! DragDropCrop::generateUpload('profile_image') !!}
use DragDropCropImageUpload\Facades\DragDropCrop;
public function store(Request $request)
{
$imagePath = DragDropCrop::uploadFromBase64(
$request->input('profile_image'),
'uploads/avatars'
);
// Save $imagePath to your database
User::create([
'avatar' => $imagePath,
// ... other fields
]);
}
{!! DragDropCrop::generateUpload('field_name') !!}
{!! DragDropCrop::generateUpload('profile_image', [
'aspect_ratio' => 1, // 1:1 square (use 16/9 for widescreen)
'crop_width' => 300, // Output width in pixels
'crop_height' => 300, // Output height in pixels
'max_size_mb' => 5, // Maximum file size in MB
'quality' => 0.8, // JPEG quality (0.1 to 1.0)
]) !!}
{!! DragDropCrop::generateUpload('avatar', [
'modal_id' => 'profileModal', // Resets widget when modal closes
]) !!}
{!! DragDropCrop::generateUpload('photo', [
'labels' => [
'title' => 'Upload Your Photo',
'browse' => 'Click to select',
'supports' => 'JPG, PNG (Max 10MB)',
'cropped_title' => 'Preview:',
'change' => 'Choose Different',
'reset' => 'Start Over',
'rotate_left' => 'Rotate Left',
'rotate_right' => 'Rotate Right',
'crop' => 'Crop & Save',
],
]) !!}
$path = DragDropCrop::uploadFromBase64(
$request->input('profile_image'), // Base64 from form
'uploads/users', // Save to public/uploads/users/
'user_' . $user->id, // Prefix: user_123_...
$user->current_avatar // Delete old avatar
);
return [
// Default upload directory (relative to public/)
'upload_directory' => 'frontend/auth',
// Default aspect ratio (1 = square)
'aspect_ratio' => 1,
// Default crop dimensions
'crop_width' => 300,
'crop_height' => 300,
// Maximum file size in MB
'max_size_mb' => 5,
// JPEG compression quality (0.1 - 1.0)
'quality' => 0.8,
// Published assets path
'asset_path' => 'vendor/drag-drop-crop',
];
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use DragDropCropImageUpload\Facades\DragDropCrop;
class ProfileController extends Controller
{
public function store(Request $request)
{
$request->validate([
'name' => ' 'avatar'
);
}
$user = User::create([
'name' => $request->input('name'),
'avatar' => $avatarPath,
]);
return redirect()->route('profile.show', $user)
->with('success', 'Profile created!');
}
public function update(Request $request, User $user)
{
$request->validate([
'name' => '