Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
"illuminate/filesystem": ">6.0",
"illuminate/database": ">6.0",
"league/flysystem": "^1.0.23",
"psr/http-message": "^1.0"
"psr/http-message": "^1.0",
"intervention/image": "^2.5",
"guzzlehttp/guzzle": "^6.5|^7.1"
},
"require-dev": {
"orchestra/testbench": "^3.3|^4.0|^5.0|^6.0",
"orchestra/testbench": "^4.0|^5.0|^6.0",
"phpunit/phpunit": "^8.2.4|^9.0",
"vlucas/phpdotenv": "^3.3|^4.0|^5.0",
"vlucas/phpdotenv": "^4.0|^5.0",
"league/flysystem-aws-s3-v3" : "^1.0.23",
"guzzlehttp/promises": "^1.3",
"aws/aws-sdk-php": "^3.29.0",
"aws/aws-sdk-php": "^3.128.0",
"php-coveralls/php-coveralls": "^2.1",
"laravel/legacy-factories": "^1.0.4"
},
Expand Down
32 changes: 16 additions & 16 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
resolveDependencies="true"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix="Test.php">./tests/integration/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
<php>
<ini name="display_errors" value="true"/>
</php>
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">./src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix="Test.php">./tests/Integration/</directory>
</testsuite>
</testsuites>
<php>
<ini name="display_errors" value="true"/>
</php>
</phpunit>
28 changes: 28 additions & 0 deletions src/Exceptions/ImageManipulationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php


namespace Plank\Mediable\Exceptions;

class ImageManipulationException extends \Exception
{
public static function invalidMediaType(?string $type): self
{
return new self(
"Cannot manipulate media with an aggregate type other than 'image', got '{$type}'."
);
}

public static function unknownVariant(string $variantName): self
{
return new self(
"Unknown variant '{$variantName}'."
);
}

public static function unknownOutputFormat(): self
{
return new self(
"Unable to determine valid output format for file."
);
}
}
177 changes: 177 additions & 0 deletions src/ImageManipulation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

namespace Plank\Mediable;

class ImageManipulation
{
public const FORMAT_JPG = 'jpg';
public const FORMAT_PNG = 'png';
public const FORMAT_GIF = 'gif';
public const FORMAT_TIFF = 'tif';
public const FORMAT_BMP = 'bmp';
public const FORMAT_WEBP = 'webp';

public const VALID_IMAGE_FORMATS = [
self::FORMAT_JPG,
self::FORMAT_PNG,
self::FORMAT_GIF,
self::FORMAT_TIFF,
self::FORMAT_BMP
];

public const MIME_TYPE_MAP = [
self::FORMAT_JPG => 'image/jpeg',
self::FORMAT_PNG => 'image/png',
self::FORMAT_GIF => 'image/gif',
self::FORMAT_TIFF => 'image/tiff',
self::FORMAT_BMP => 'image/bmp',
self::FORMAT_WEBP => 'image/webp'
];

/** @var callable */
private $callback;

/** @var string|null */
private $outputFormat;

/** @var int */
private $outputQuality = 90;

/** @var callable|null */
private $beforeSave;

public function __construct(callable $callback)
{
$this->callback = $callback;
}

public static function make(callable $callback)
{
return new self($callback);
}

/**
* @return \Closure
*/
public function getCallback(): \Closure
{
return $this->callback;
}

/**
* @return int
*/
public function getOutputQuality(): int
{
return $this->outputQuality;
}

/**
* @param int $outputQuality
* @return $this
*/
public function setOutputQuality(int $outputQuality): self
{
$this->outputQuality = min(100, max(0, $outputQuality));

return $this;
}

/**
* @return string|null
*/
public function getOutputFormat(): ?string
{
return $this->outputFormat;
}

/**
* @param string|null $outputFormat
* @return $this
*/
public function setOutputFormat(?string $outputFormat): self
{
$this->outputFormat = $outputFormat;

return $this;
}

/**
* @return $this
*/
public function toJpegFormat(): self
{
$this->setOutputFormat(self::FORMAT_JPG);

return $this;
}

/**
* @return $this
*/
public function toPngFormat(): self
{
$this->setOutputFormat(self::FORMAT_PNG);

return $this;
}

/**
* @return $this
*/
public function toGifFormat(): self
{
$this->setOutputFormat(self::FORMAT_GIF);

return $this;
}

/**
* @return $this
*/
public function toTiffFormat(): self
{
$this->setOutputFormat(self::FORMAT_TIFF);

return $this;
}

/**
* @return $this
*/
public function toBmpFormat(): self
{
$this->setOutputFormat(self::FORMAT_BMP);

return $this;
}

/**
* @return $this
*/
public function toWebpFormat(): self
{
$this->setOutputFormat(self::FORMAT_WEBP);

return $this;
}

/**
* @return callable
*/
public function getBeforeSave(): ?callable
{
return $this->beforeSave;
}

/**
* @param callable $beforeSave
* @return $this
*/
public function beforeSave(callable $beforeSave): self
{
$this->beforeSave = $beforeSave;

return $this;
}
}
Loading