Image CRUD provides a simple way to create, update and delete images in your CodeIgniter 4 application. Here's a basic example to get you started:
<?php
namespace App\Controllers;
use GroceryCrud\ImageCrud\ImageCrud;
class Gallery extends BaseController
{
public function example1()
{
$image_crud = new ImageCrud();
$image_crud->setTable('gallery')
->setPrimaryKeyField('id')
->setUrlField('image')
->setTitleField('title')
->setOrderingField('priority')
->setImagePath('assets/uploads/gallery');
$output = $image_crud->render();
return view('image_crud/template', $output);
}
}
Image CRUD provides the following methods to configure your image gallery:
Method name | Description | Example |
---|---|---|
setTable |
Set the table name of your photo gallery | $image_crud->setTable('example_1'); |
setPrimaryKeyField |
Set the primary key of your basic table that you have inserted at setTable | $image_crud->setPrimaryKeyField('image_id'); |
setUrlField |
Set the url field name that you want to store the image name | $image_crud->setUrlField('image_url'); |
setImagePath |
Set the folder path that you want to upload the image file starting from the base_url of your project without starting with "/" | $image_crud->setImagePath('assets/uploads/slideshow-images'); |
render |
Rendering everything that you have set. Or else you can tell it as "Make it work". Without this function nothing works. It is the core of the image crud. | $output = $image_crud->render(); |
setOrderingField |
Simply set the table field that is responsible for the ordering. | $image_crud->setOrderingField('priority'); |
setTitleField |
The field name for quick editing the title of the image. | $image_crud->setTitleField('image_title'); |
setRelationField |
Will automatically add the parameter at the URL (e.g. photos/45 the 45 is the category id) and it is also adding a where clause to the photo gallery | $image_crud->setRelationField('category_id'); |
unsetDelete |
Remove the delete button from all the images for this CRUD. | $image_crud->unsetDelete(); |
unsetUpload |
Remove the upload button from the interface | $image_crud->unsetUpload(); |
To use Image CRUD, you need to create a table with at least the following fields:
CREATE TABLE `gallery` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image` varchar(255) NOT NULL,
`title` varchar(100) DEFAULT NULL,
`priority` int(11) DEFAULT 0,
PRIMARY KEY (`id`)
);
This basic structure includes:
id
: Primary key fieldimage
: Field to store the image filenametitle
: Optional field for image titlespriority
: Field for ordering imagesYou can create image galleries related to other entities (like product images, category images, etc.) using the setRelationField
method:
<?php
namespace App\Controllers;
use GroceryCrud\ImageCrud\ImageCrud;
class Products extends BaseController
{
public function product_images()
{
// Example URL: /products/product_images/45
// The 45 is the product ID
// Image CRUD will automatically add the product ID to the images
// and filter the images by product ID
$image_crud = new ImageCrud();
$image_crud->setTable('product_images')
->setPrimaryKeyField('id')
->setUrlField('image')
->setTitleField('title')
->setRelationField('product_id')
->setOrderingField('priority')
->setImagePath('assets/uploads/products');
$output = $image_crud->render();
return view('image_crud/template', $output);
}
}
With this setup, the gallery will show only images related to the specified product ID and will automatically add the product ID to new images.