Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Jcrop Laravel Package

monstrum/jcrop

Laravel package that integrates the JCrop jQuery image-cropping plugin, making it easy to add client-side crop selection and coordinates to PHP applications. Simplifies including JCrop assets and wiring cropping into forms.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer (composer require <vendor>/<package>) and publish the package assets with php artisan vendor:publish --tag=jcrop-assets. Ensure jQuery is available before the plugin script. The quickest first use case is a simple image upload form with a cropping modal: include the provided Blade component or manually add the CSS/JS to your layout, then initialise JCrop on an <img> element inside a form. Check the package’s README and config/jcrop.php for default options (aspect ratio, min/max sizes, preview containers). A minimal working example: upload an image, display it in a modal, attach JCrop, and store the crop coordinates (x, y, w, h) in hidden inputs for server‑side processing.

Implementation Patterns

  • Coordinate‑based cropping: Use JCrop to capture selection coordinates client‑side, then send them to a Laravel controller. Combine with intervention/image to perform the actual crop: Image::make($uploaded)->crop($w, $h, $x, $y)->save($path).
  • AJAX workflow: On crop selection change, POST coordinates to an endpoint that returns a preview URL (using a temporary crop or a CSS‑based preview). Use Laravel’s CSRF token in the AJAX headers.
  • Blade component integration: Wrap the image, modal, and hidden inputs in a reusable Blade component (e.g., <x-jcrop-uploader :image="$model->image" />). Pass initial crop data via props for edit forms.
  • Event handling: Bind to JCrop events (crop.change, crop.release) to update hidden inputs and live previews. Use the package’s restructured jQuery plugin API (standard $.fn.jcrop) for consistency.
  • Multiple croppers: Initialise separate JCrop instances per image by scoping selectors (e.g., $('.jcrop-container').each(...)) and storing instance references for later method calls.
  • Server‑side validation: Always validate coordinates against the original image dimensions before cropping to prevent malicious input.

Gotchas and Tips

  • jQuery dependency: The plugin requires jQuery; ensure it loads before the package script. If using Laravel Mix/Vite, include jQuery globally or import it explicitly.
  • Asset publishing: After upgrading from the original tapmono/jcrop, republish assets and clear view cache (php artisan view:clear). The restructured plugin may have different file paths or option names.
  • Responsive images: JCrop works on the displayed image size, not the natural size. If the image is scaled via CSS, multiply coordinates by the natural/displayed ratio before sending to the server.
  • Modal timing: Initialise JCrop only after the image is fully loaded and visible. Use the load event or a small delay; otherwise selection handles may misalign.
  • CSRF and AJAX: When sending coordinates asynchronously, include X-CSRF-TOKEN in headers or use $.ajaxSetup with Laravel’s token meta tag.
  • Debugging: If the crop UI doesn’t appear, check the browser console for missing jQuery or plugin errors. Use console.log($.fn.jcrop) to confirm the plugin is registered.
  • Extension points: Override default options globally via $.fn.jcrop.defaults or per‑instance. The package’s config file may expose additional settings (e.g., allowSelect, bgColor); publish and edit it as needed.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel
draftsmaninc/draftsman
square-bit/laravel-volition
himito/laravel-mailman
htinlynn/genius-helper
maco-studios/hephaestus
adachsoft/php-code-style