1. Go to this page and download the library: Download nmirceac/color-tools library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nmirceac / color-tools example snippets
namespace App;
class ImageStore extends \ColorTools\ImageStore {
protected $appends = ['basename', 'details', 'thumbnail'];
public function user()
{
return $this->morphedByMany(User::class,
'association',
'image_associations',
'association_id',
'image_id'
)->withPivot(self::$withPivot);
}
public function getThumbnailAttribute()
{
return $this->getUrl(function(\ColorTools\Image $image) {
$image->fit(450, 450);
});
}
public function fit($width, $height)
{
return $this->getUrl(function(\ColorTools\Image $image) use ($width, $height) {
$image->fit($width, $height);
});
}
}
namespace App;
use App\Jobs\SendResetPasswordEmail;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use ColorTools\HasImages;
class User extends Authenticatable
{
use SoftDeletes;
use Notifiable;
use HasImages;
protected $appends = ['image', 'details'];
...
...
...
public function image()
{
return $this->images()->first();
}
public function getImageAttribute()
{
return $this->image();
}
}
// create from file path
Image::create('pathTo.jpg');
Image::create('pathTo.png');
// create from string
Image::create(file_get_contents('pathTo.jpg'));
// create from URL
Image::create('http://domain.tld/image.jpg');
// create from GD
$gdResource = imagecreatefromgif('pathTo.gif');
Image::create($gdResource);
// create from Imagick
$imagickResource = new Imagick('pathTo.jpg');
Image::create($imagickResource);
// image modifiers
\ColorTools\Image $image = Image::create('pathTo.jpg');
// fit
$cropAnchor = \ColorTools\Image::CROP_ANCHOR_CENTER;
// possible other crop values
// \ColorTools\Image::CROP_ANCHOR_LEFT, \ColorTools\Image::CROP_ANCHOR_RIGHT
// \ColorTools\Image::CROP_ANCHOR_TOP, \ColorTools\Image::CROP_ANCHOR_BOTTOM
// resizing and cropping the image to fit in a box defined by specified width and height
$image->fit(1280, 720, $cropAnchor);
// resizing and covering the specified width and height
$image->resizeCover(1280, 720);
// resizing while being container within the specified width and height
$image->resizeContain(1280, 720);
// resizing specifing the width
$image->fillWidth(720);
// resizing specifing the height
$image->fillHeight(720);
// resizing while containing within the desired width - won't upscale smaller images
$image->containWidth(720);
// resizing while containing within the desired height - won't upscale smaller images
$image->containHeight(720);
// cropping
$image->doCrop(1280, 720, $cropAnchor);
// rotate
$image->doRotate(90); // degrees
// filter
$filter = \ColorTools\Image::FILTER_SEPIA;
$filterParameters = [2];
$image->applyFilter($filter, $filterParameters);
$filter = \ColorTools\Image::FILTER_ENHANCE;
$image->applyFilter($filter);
$filter = \ColorTools\Image::FILTER_MOTION_BLUR;
$filterParameters = [0.5, 0.5, 2];
$image->applyFilter($filter, $filterParameters);
// supported filter list
/*
\ColorTools\Image::FILTER_NEGATE,
\ColorTools\Image::FILTER_GRAYSCALE,
\ColorTools\Image::FILTER_BRIGTHNESS,
\ColorTools\Image::FILTER_CONTRAST,
\ColorTools\Image::FILTER_COLORIZE,
\ColorTools\Image::FILTER_EDGEDETECT,
\ColorTools\Image::FILTER_EMBOSS,
\ColorTools\Image::FILTER_GAUSSIAN_BLUR,
\ColorTools\Image::FILTER_SELECTIVE_BLUR,
\ColorTools\Image::FILTER_MEAN_REMOVAL,
\ColorTools\Image::FILTER_SMOOTH,
\ColorTools\Image::FILTER_PIXELATE,
\ColorTools\Image::FILTER_SEPIA,
\ColorTools\Image::FILTER_ENHANCE,
\ColorTools\Image::FILTER_EQUALIZE,
\ColorTools\Image::FILTER_AUTO_LEVEL,
\ColorTools\Image::FILTER_MOTION_BLUR,
\ColorTools\Image::FILTER_OIL_PAINT,
\ColorTools\Image::FILTER_POSTERIZE,
\ColorTools\Image::FILTER_RADIAL_BLUR,
\ColorTools\Image::FILTER_SEGMENT,
\ColorTools\Image::FILTER_SIGMOIDAL_CONTRAST,
\ColorTools\Image::FILTER_SKETCH,
\ColorTools\Image::FILTER_SOLARIZE,
\ColorTools\Image::FILTER_SPREAD,
\ColorTools\Image::FILTER_THRESHOLD,
\ColorTools\Image::FILTER_BLACK_THRESHOLD,
\ColorTools\Image::FILTER_WAVE,
\ColorTools\Image::FILTER_VIGNETTE,
\ColorTools\Image::FILTER_SWIRL,
\ColorTools\Image::FILTER_NOISE,
\ColorTools\Image::FILTER_BLUE_SHIFT,
\ColorTools\Image::FILTER_CHARCOAL,
\ColorTools\Image::FILTER_GAMMA,
\ColorTools\Image::FILTER_BLUR,
*/
// create from file path
ImageStore::createFromPath('pathTo.jpg');
// create from Laravel request and the file key
ImageStore::createFromRequest(request(), 'image');
// get by id
$image = ImageStore::find(3);
// publish image
$image = ImageStore::find(3);
$image->modifyImagePublish(function(\ColorTools\Image $img) {
$img->fit(1280, 720);
$img->applyFilter(\ColorTools\Image::FILTER_BLUR, [1, 2]);
});
// conditional resizing
$image = ImageStore::find(3);
$image->modifyImagePublish(function(\ColorTools\Image $img) {
if($img->width > $img->height) { // landscape
$img->fit(1280, 720);
} else {
$img->fit(720, 1280);
}
});
// publish image with specified format
$image = ImageStore::find(3);
$image->modifyImagePublish(function(\ColorTools\Image $img) {
$img->fit(1280, 720);
}, 'png');
// by default the format is auto which defaults to 'webp' if supported
// by the client or the original format of the source image
// get histogram data
$image = ImageStore::find(3);
$image->getHistogram();
// get histogram image
$image = ImageStore::find(3);
$image->getHistogramSrc();
// return a base64 encoded svg
$image->getHistogramSrc('c'); // color
$image->getHistogramSrc('r'); // red
$image->getHistogramSrc('g'); // green
$image->getHistogramSrc('b'); // blue
// get used colors
$image = ImageStore::find(3);
$image->colors;
/*
return object where the key is the hex code and the value is the coverage percentage
{#458 ▼
+"#919191": 27.16
+"#ffffff": 23.37
+"#663399": 14.69
+"#000000": 9.59
+"#ffff91": 7.77
+"#ff9191": 6.43
}
*/
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.