2017 © Pedro Peláez
 

library simple-image-cropper

A light weight php library for cropping and resizing images.

image

sayme/simple-image-cropper

A light weight php library for cropping and resizing images.

  • Sunday, March 11, 2018
  • by sayme
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

SimpleImageCropper

A light weight php library for cropping and resizing images., (*1)

Build Status, (*2)

Usage

Download and install SimpleImageCropper

To install SimpleImageCropper run the following command:, (*3)

``` bash $ composer require sayme/simple-image-cropper, (*4)


### Initialize Initialize SimpleImageCropper with URL as image source. ```php use SimpleImageCropper\Cropper; $cropper = new Cropper('http://example.com/your-image.png');

Or you can use $_FILES['filename']['tmp_name'] as source., (*5)

$cropper = new Cropper($_FILE['filename']['tmp_name']);

When initializing the Cropper you will have access to some of the original image meta., (*6)

  • Image width $cropper->getWidth()
  • Image height $cropper->getHeight()
  • Image type $cropper->getType()

Crop and save image

Cropping the image in center and saving the new image., (*7)

$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);

Output image as BLOB

You can also output the image as BLOB for saving it in your database or just outputting it directly., (*8)

echo $cropper->getData();

Color png backgrounds

You can also set the background color of pngs (RGB), (*9)

// set the color
$color = [
    'r' => 150,
    'g' => 150,
    'b' => 150
];

$cropper->crop($width, $height, $color['r'], $color['g'], $color['b']);

Examples

Save image

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');

Output image (BLOB)

// 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();

Resize image and keep the proportions

$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);

The Versions

11/03 2018

dev-master

9999999-dev

A light weight php library for cropping and resizing images.

  Sources   Download

MIT

The Requires

  • ext-gd *
  • php ^7.1

 

The Development Requires

by Sami Piirainen