Files

The Quantum's `File` library built on codeguy/upload and gumlet/php-image-resize packages and uploads files and manipulates images very easily. It can validate, crop, resize and work on uploaded images in many ways.

To start using the library you need to make it available in your controller


        use Quantum\Libraries\File\File;
        
        

Assuming the following simple form to organize the image upload:


<form action="/upload" method="post" enctype="multipart/form-data">
    <label> Upload your photo:
        <input type="file" name="photo" />
    </label>
    <input type="submit" value="Upload" />
</form>

        

Once the form is submited, you need to check if the request contains a file


if (Request::hasFile('photo')) {
    // handling the file
}
        
        

Next you will need to create instance of `File` class to start working with the file:


$file = new File('photo', UPLOADS_DIR);
        
        

Once the instnace is created you can access to data about the file that has been uploaded

  • $file->getNameWithExtension() - Returns filename with extension
  • $file->getExtension() - Returns only extension
  • $file->getMimetype() - Returns mime type
  • $file->getSize() - Returns the file size
  • $file->getDimensions() - Returns image dimensions

Validating files

There are 3 types of validation:

  • Mime type validation
  • Size validation
  • Dimensions validation (specific to images)

A validation rules has similar structure


$rules = [
    [
        'type' => 'mime',
        'values' => ['image/jpg', 'image/png', 'image/gif']
    ],
    [
        'type' => 'size',
        'values' => '2M'
    ],
    [
        'type' => 'dimensions',
        'values' => ['width' => 200, 'height' => 200]
    ],
];
        
        

Now you need to add those validations


$file->addValidations($rules);
        
        

Once the validation rules are applied, you will need to validate the file


if ($file->validate()) {
    // file is valid, so you can proceed to save it
} else {
    out($file->getErrors(), 1);
}

        

The method $file->getErrors() contains errors when validation rules are not passed.

Before saving the file you will need to name it.


    $file->setName('avatar');

        

And finally save it


    $file->save();

        

The `File` library allows you to resize or crop the image before saving it.


$file->setName('avatar');
$file->modify('resizeToBestFit', [100, 150], 'avatar_thumb');
$file->save();

        

The modeify() method accpets 3 parameters

  • Function name (callback)
  • Parameters passed to that function
  • Alternative name

There are several callback functions related to image manipulations

  • $file->modify('scale', $percentage, $laternative_name) - Scales the image by given percentage
  • $file->modify('resizeToHeight', $size, $laternative_name) - Resize an image according to height
  • $file->modify('resizeToWidth', $size, $laternative_name) - Resize an image according to width
  • $file->modify('resizeToLongSide', $size, $laternative_name) - Resize according to a long side
  • $file->modify('resizeToShortSide', $size, $laternative_name) - Resize according to a short side
  • $file->modify('resizeToBestFit', [$width, $height], $laternative_name) - Resize to best fit
  • $file->modify('resize', [$width, $height], $laternative_name) - Resizes by given width and height
  • $file->modify('crop', [$width, $height], $laternative_name) - Crops by given width and height
<< Prev Next >>