Options & Callbacks - PHP

The first argument of the ImgSelect class is for options:

new ImgSelect(array(
    'upload_dir' => 'files/',
    'accept_file_types' => 'png|jpg|jpeg|gif',
    // other options...
));

Options

upload_dir

The upload directory relative to the server folder.

  • Type: string
  • Default: files

accept_file_types

The accepted file types.

  • Type: string
  • Default: png|jpg|jpeg|gif

mkdir_mode

The mkdir mode used when creating the upload directory.

  • Type: int
  • Default: 0755

max_file_size

The maximum file size in bytes.

  • Type: int
  • Default: null

min_file_size

The minimum file size in bytes.

  • Type: int
  • Default: 1

max_width

The maximum image width.

  • Type: int
  • Default: null

max_height

The maximum image height.

  • Type: int
  • Defaullt: null

min_width

The minimum image width:

  • Type: int
  • Default: 1

min_height

The minimum image height.

  • Type: int
  • Default: 1

force_crop

If the image size < crop size then force the resize.

  • Type: boolean
  • Default: true

crop_max_width

The crop maximum width.

  • Type: int
  • Default: null

crop_max_height

The crop maximum height.

  • Type: int
  • Default: null

Callbacks

before_upload

before_upload($image) - Called before uploading the file to the server.

The $image object has the following properties:

  • name - Image name (including the extension).
  • type - Image extension.
  • size - Image size in bytes.
  • path - Image path.
  • url - Image url.
  • width - Image width.
  • height - image height.

In this callback you can rename the uploaded file:

new ImgSelect(array(
    // options...
    `before_upload` => function ($image) {
        $image->name = 'custom_name.'.$image->type;
    },
));

upload_complete

upload_complete ($image) - Called when the upload is completed.

The $image object has the same properties as above.

before_crop

before_crop ($crop) - Called before cropping the image.

The $crop object has the following properties:

  • file_name - The image name to be cropped.
  • file_type - The image type.
  • src_path, dst_path, src_h, src_w, src_y, src_x, dst_w and dst_h are described in ImgSelect.php in the resizeImage method.

In this callback you may set the crop destionaton path so the original file will be keept:

new ImgSelect(array(
    // options...
    before_crop => function ($crop, $instance) {
        $crop->dst_path = $instance->getUploadPath('my_image.'.$crop->file_type);
    }
));

crop_complete

crop_complete ($image) - Called when the crop is completed.

The $image object has the same properties as above.

In this callback you may save the image into database using $image->name to get the image name.