项目作者: armin-runggaldier

项目描述 :
PHP Framework for image manipulation
高级语言: PHP
项目地址: git://github.com/armin-runggaldier/croppy.git
创建时间: 2020-07-24T21:31:13Z
项目社区:https://github.com/armin-runggaldier/croppy

开源协议:MIT License

下载


Croppy

Croppy - PHP Framework for image manipulation

Version: 1.3
PHP Version: php >= 8.0

Features

  • image resize
  • image crop
  • extend image area
  • image conversion
  • supported formats: .jpeg, .jpg, .png, .gif, .webp
  • image quality management
  • image save to filesystem or output
  • designed for PHP >= 8.1

Installation

Croppy is actually available trough Composer (recommended) or direct download.

To use Composer, just add this line to your composer.json file:

  1. "croppy/croppy": "~1.1"

and run the follow command:

  1. composer install

You can also run the follow command in the terminal (you must be in the project directory):

  1. composer require croppy/croppy

Usage

Basic usage:

  1. use Croppy\Croppy\Croppy;
  2. use Croppy\Croppy\Exception;
  3. require 'path/to/src/Croppy.php';
  4. require 'path/to/src/Exception.php';
  5. $croppy = new Croppy();
  6. $croppy->setSourcePath(__DIR__.'/path/to/image.jpg');
  7. $croppy->setJpegQuality(60);
  8. $croppy->resize(700, 700, false); // width, height, crop
  9. $croppy->save(__DIR__.'/path/to/destination.jpg');

Resize, crop and output:

  1. use Croppy\Croppy\Croppy;
  2. use Croppy\Croppy\Exception;
  3. require 'path/to/src/Croppy.php';
  4. require 'path/to/src/Exception.php';
  5. $croppy = new Croppy();
  6. $croppy->setSourcePath(__DIR__.'/path/to/image.png');
  7. $croppy->setPngCompression(6);
  8. // set crop position x, y
  9. $croppy->setCropPosition($croppy::CROPCENTER, $croppy::CROPSTART); // $croppy::CROPSTART | $croppy::CROPCENTER | $croppy::CROPEND
  10. // set background color
  11. $croppy->setBackground(255, 255, 255, 100); // R, G, B, opacity
  12. $croppy->resize(700, 700, true); // width, height, crop
  13. $croppy->output();
  14. // get path of input file
  15. echo $croppy->getSourcePath();

Documentation

setSourcePath (string $sourcePath) : void
Defines the path of the source image, can be relative or absolute

setJpegQuality (int $quality = 80) : void
Defines quality for JPEG images (0 - 100), 100 best quality

setWebpQuality (int $quality = 80) : void
Defines quality for WEBP images (0 - 100), 100 best quality

setPngCompression (int $compression = 6) : void
Defines quality for PNG images (0 - 9), 0 means no compression

setUpScaleAllowed (bool $upScaleAllowed = false) : void
Deinfes if images should be scaled up (distort image), default is false

getSourcePath () : string
Returns the path to the source image. It could return a relative or absolute path (depends on input)

setCropPosition ($cropAlignmentX = Croppy::CROPCENTER, $cropAlignmentY = Croppy::CROPCENTER) : void
Defines the crop position of the image. $crop flag must be set to true.
$cropAlignmentX defines the horizontal alignment, $cropAlignmentY defines the vertical alignment. Possible values: Croppy::CROPSTART, Croppy::CROPCENTER, Croppy::CROPEND

setBackground ($backgroundColorR, $backgroundColorB, $backgroundColorG, $opacity = 100) : void
Set background color, which is always set in resize and extend method. Actually Croppy only supports RGB. $opacity defines the opacity of the background color (100 is complete transparent).

resize (float $destinationWidth, float $destinationHeight, bool $crop = false) : bool
Resize image to the given $destinationWidth and $destinationHeight. If $crop is set to true, the image will be cropped. If $crop is set to false, the image will be adjusted (max width and max height).

extend (float $destinationWidth, float $destinationHeight) : bool
Extends image area and fill background with $backgroundColor. Could also used for crop image without resize.

save ($destinationPath, $convertType = false) : bool
Writes the generated image to the defined $destinationPath.
$convertType enables the conversion of the image type. The destination image type will be identify from the filename in $destinationPath (ex. path/to/image.webp will convert the image to WEBP). Return true on success or false on failure.

output ($convertType = false) : void
Streams the image direct to output and set Content-Type header.
$convertType enables the conversion of the image type. The destination image type will be identify from the filename in $destinationPath (ex. path/to/image.webp will convert the image to WEBP). Return true on success or false on failure.


Planned features for further releases:

  • image ratio resize

Changelog version 1.3 (15.11.2022):

  • Support for PHP Version 8
  • minor bugfixes for $upScaleAllowed property

Changelog version 1.2 (31.12.2020):

  • image upscale definiton
  • extend image area and fill with background color
  • image type conversion (for example .png to .jpg)

Changelog version 1.1 (19.09.2020):

  • image quality settings (jpeg, png and webp)

If you are missing a feature, please create a pull request :)


Made with ❤ in South Tyrol by Armin Runggaldier