A light weight php library for cropping and resizing images.
To install SimpleImageCropper run the following command:
$ composer require sayme/simple-image-cropper
Initialize SimpleImageCropper with URL as image source.
use SimpleImageCropper\Cropper;
$cropper = new Cropper('http://example.com/your-image.png');
Or you can use $_FILES['filename']['tmp_name']
as source.
$cropper = new Cropper($_FILE['filename']['tmp_name']);
When initializing the Cropper you will have access to some of the original image meta.
- Image width
$cropper->getWidth()
- Image height
$cropper->getHeight()
- Image type
$cropper->getType()
Cropping the image in center and saving the new image.
$width = 150;
$height = 150;
// This will crop the image in center with the new width and height
$cropper->crop($width, $height);
// This will save your new image as mynewimage.png in the current directory
$cropper->save('mynewimage.png');
// You can also set the quality of the image to be saved in the second parameter.
// The quality is by default 75, you can set it to a quality between 0-100
$cropper->save('mynewumage.png', 50);
You can also output the image as BLOB for saving it in your database or just outputting it directly.
echo $cropper->getData();
You can also set the background color of pngs (RGB)
// set the color
$color = [
'r' => 150,
'g' => 150,
'b' => 150
];
$cropper->crop($width, $height, $color['r'], $color['g'], $color['b']);
use SimpleImageCropper\Cropper;
$cropper = new Cropper('http://example.com/your-image.png');
// Crop the image by 200x200
$cropper->crop(200, 200);
// Save the image as mynewimage.png
$cropper->save('mynewimage.png');
// Set header
header('Content-Type: image/png');
use SimpleImageCropper\Cropper;
$cropper = new Cropper('http://example.com/your-image.png');
// Crop the image by 200x200 and output it.
echo $cropper->crop(200, 200)->getData();
$cropper = new Cropper($_FILES['filename']['tmp_name']);
// Set the new width
$newWidth = 306;
// Check proportions
$proportion = $newWidth / $img->width;
// Set the new height
$newHeight = $img->height * $proportion;
// Crop the image by its new width and height
$cropper->crop($newWidth, $newHeight);
// Save the image with a 50% quality
$cropper->save('mynewimage.png', 50);