Skip to content

Commit

Permalink
Merge pull request #23 from mostafaznv/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
mostafaznv authored Dec 21, 2023
2 parents 67f6a11 + ab1f14e commit b2d39b1
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 28 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"ext-json": "*",
"ext-pdo": "*",
"imagine/imagine": "^1.3",
"intervention/image": "^2.7",
"intervention/image": "^3.1.0",
"ksubileau/color-thief-php": "^2.0",
"laravel/framework": "^10.4.1",
"php-ffmpeg/php-ffmpeg": "^1.1",
Expand Down
8 changes: 6 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Introduction

[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/mostafaznv/larupload/run-tests.yml?branch=master\&label=Build\&style=flat-square\&logo=github)](https://github.com/mostafaznv/larupload/actions) [![Codecov branch](https://img.shields.io/codecov/c/github/mostafaznv/larupload/master.svg?style=flat-square\&logo=codecov)](https://app.codecov.io/gh/mostafaznv/larupload) [![Quality Score](https://img.shields.io/scrutinizer/g/mostafaznv/larupload.svg?style=flat-square)](https://scrutinizer-ci.com/g/mostafaznv/larupload) ![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square) [![Latest Version on Packagist](https://img.shields.io/packagist/v/mostafaznv/larupload.svg?style=flat-square)](https://packagist.org/packages/mostafaznv/larupload) [![Total Downloads](https://img.shields.io/packagist/dt/mostafaznv/larupload.svg?style=flat-square)](https://packagist.org/packages/mostafaznv/larupload)
[<img src="https://img.shields.io/github/actions/workflow/status/mostafaznv/larupload/run-tests.yml?branch=master&#x26;label=Build&#x26;style=flat-square&#x26;logo=github" alt="GitHub Workflow Status" data-size="line">](https://github.com/mostafaznv/larupload/actions) [<img src="https://img.shields.io/codecov/c/github/mostafaznv/larupload/master.svg?style=flat-square&#x26;logo=codecov" alt="Codecov branch" data-size="line">](https://app.codecov.io/gh/mostafaznv/larupload) [<img src="https://img.shields.io/scrutinizer/g/mostafaznv/larupload.svg?style=flat-square" alt="Quality Score" data-size="line">](https://scrutinizer-ci.com/g/mostafaznv/larupload) <img src="https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square" alt="Software License" data-size="line"> [<img src="https://img.shields.io/packagist/v/mostafaznv/larupload.svg?style=flat-square" alt="Latest Version on Packagist" data-size="line">](https://packagist.org/packages/mostafaznv/larupload) [<img src="https://img.shields.io/packagist/dt/mostafaznv/larupload.svg?style=flat-square" alt="Total Downloads" data-size="line">](https://packagist.org/packages/mostafaznv/larupload)



Expand All @@ -14,7 +14,11 @@ It offers many useful features, including the ability to resize, crop, and optim

Moreover, Larupload can calculate the dominant colors of videos and images, as well as extract their width, height, and duration for videos and audio files

[![Donate](https://mostafaznv.github.io/donate/donate.svg)](getting-started/support-us.md)
<div align="left">

<figure><img src="https://mostafaznv.github.io/donate/donate.svg" alt="" width="188"><figcaption></figcaption></figure>

</div>



Expand Down
1 change: 0 additions & 1 deletion docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* [Laravel Nova integration](getting-started/laravel-nova-integration.md)
* [Support Us](getting-started/support-us.md)
* [License](getting-started/license.md)
* [Laravel Nova Integration](getting-started/laravel-nova-integration-1.md)

## 🚀 Basic Usage

Expand Down
6 changes: 3 additions & 3 deletions docs/advanced-usage/attachment/media-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ On the other hand, if you want to manipulate images or videos, you should use th

### Image Style

<table><thead><tr><th data-type="number">Index</th><th>Name</th><th>Type</th><th data-type="checkbox">Required</th><th>Default</th><th>Description</th></tr></thead><tbody><tr><td>1</td><td>name</td><td>string</td><td>true</td><td>–</td><td>style name. examples: thumbnail, small, ...</td></tr><tr><td>2</td><td>width</td><td>?int</td><td>false</td><td>null</td><td>width of the manipulated image</td></tr><tr><td>3</td><td>height</td><td>?int</td><td>false</td><td>null</td><td>height of the manipulated image</td></tr><tr><td>4</td><td>mode</td><td>LaruploadMediaStyle</td><td>false</td><td>AUTO</td><td>this argument specifies how Larupload should manipulate the uploaded image and can take on any of the following values: <code>FIT</code>, <code>AUTO</code>, <code>SCALE_WIDTH</code>, <code>SCALE_HEIGHT</code>, <code>CROP</code></td></tr></tbody></table>
<table><thead><tr><th width="100" data-type="number">Index</th><th width="99">Name</th><th width="196">Type</th><th data-type="checkbox">Required</th><th width="91">Default</th><th width="515">Description</th></tr></thead><tbody><tr><td>1</td><td>name</td><td>string</td><td>true</td><td>–</td><td>style name. examples: thumbnail, small, ...</td></tr><tr><td>2</td><td>width</td><td>?int</td><td>false</td><td>null</td><td>width of the manipulated image</td></tr><tr><td>3</td><td>height</td><td>?int</td><td>false</td><td>null</td><td>height of the manipulated image</td></tr><tr><td>4</td><td>mode</td><td>LaruploadMediaStyle</td><td>false</td><td>AUTO</td><td>this argument specifies how Larupload should manipulate the uploaded image and can take on any of the following values: <code>FIT</code>, <code>AUTO</code>, <code>SCALE_WIDTH</code>, <code>SCALE_HEIGHT</code>, <code>CROP</code></td></tr></tbody></table>

```php
<?php
Expand Down Expand Up @@ -47,7 +47,7 @@ class Media extends Model

### Video Style

<table><thead><tr><th data-type="number">Index</th><th>Name</th><th>Type</th><th data-type="checkbox">Required</th><th>Default</th><th>Description</th></tr></thead><tbody><tr><td>1</td><td>name</td><td>string</td><td>true</td><td>–</td><td>style name. examples: thumbnail, small, ...</td></tr><tr><td>2</td><td>width</td><td>?int</td><td>false</td><td>null</td><td>width of the manipulated video</td></tr><tr><td>3</td><td>height</td><td>?int</td><td>false</td><td>null</td><td>height of the manipulated video</td></tr><tr><td>4</td><td>mode</td><td>LaruploadMediaStyle</td><td>false</td><td>SCALE_HEIGHT</td><td>this argument specifies how Larupload should manipulate the uploaded video and can take on any of the following values: <code>FIT</code>, <code>AUTO</code>, <code>SCALE_WIDTH</code>, <code>SCALE_HEIGHT</code>, <code>CROP</code></td></tr><tr><td>5</td><td>format</td><td>X264</td><td>false</td><td>new X264</td><td>by default, the encoding format for video is <code>X264</code>. However, users can specify additional options for this format, including adjusting the <code>kilobitrate</code> for both <code>audio</code> and <code>video</code>. This allows for more precise configuration and optimization of the user's encoding preferences.</td></tr><tr><td>6</td><td>padding</td><td>bool</td><td>false</td><td>false</td><td>If set to <code>true</code>, padding will be applied to the video using a black color in order to fit the given dimensions.</td></tr></tbody></table>
<table><thead><tr><th width="103" data-type="number">Index</th><th width="99">Name</th><th width="196">Type</th><th data-type="checkbox">Required</th><th width="155">Default</th><th width="515">Description</th></tr></thead><tbody><tr><td>1</td><td>name</td><td>string</td><td>true</td><td>–</td><td>style name. examples: thumbnail, small, ...</td></tr><tr><td>2</td><td>width</td><td>?int</td><td>false</td><td>null</td><td>width of the manipulated video</td></tr><tr><td>3</td><td>height</td><td>?int</td><td>false</td><td>null</td><td>height of the manipulated video</td></tr><tr><td>4</td><td>mode</td><td>LaruploadMediaStyle</td><td>false</td><td>SCALE_HEIGHT</td><td>this argument specifies how Larupload should manipulate the uploaded video and can take on any of the following values: <code>FIT</code>, <code>AUTO</code>, <code>SCALE_WIDTH</code>, <code>SCALE_HEIGHT</code>, <code>CROP</code></td></tr><tr><td>5</td><td>format</td><td>X264</td><td>false</td><td>new X264</td><td>by default, the encoding format for video is <code>X264</code>. However, users can specify additional options for this format, including adjusting the <code>kilobitrate</code> for both <code>audio</code> and <code>video</code>. This allows for more precise configuration and optimization of the user's encoding preferences.</td></tr><tr><td>6</td><td>padding</td><td>bool</td><td>false</td><td>false</td><td>If set to <code>true</code>, padding will be applied to the video using a black color in order to fit the given dimensions.</td></tr></tbody></table>

```php
<?php
Expand Down Expand Up @@ -80,7 +80,7 @@ class Media extends Model

### Stream Style

<table><thead><tr><th data-type="number">Index</th><th>Name</th><th>Type</th><th data-type="checkbox">Required</th><th>Default</th><th>Description</th></tr></thead><tbody><tr><td>1</td><td>name</td><td>string</td><td>true</td><td>–</td><td>label for stream quality. highly recommended to use string labels like <code>720p</code></td></tr><tr><td>2</td><td>width</td><td>int</td><td>true</td><td>–</td><td></td></tr><tr><td>3</td><td>height</td><td>int</td><td>true</td><td>–</td><td></td></tr><tr><td>4</td><td>format</td><td>X264</td><td>true</td><td>–</td><td>by default, the encoding format for video is <code>X264</code>. However, users can specify additional options for this format, including adjusting the <code>kilobitrate</code> for both <code>audio</code> and <code>video</code>. This allows for more precise configuration and optimization of the user's encoding preferences.</td></tr><tr><td>5</td><td>padding</td><td>bool</td><td>false</td><td>false</td><td>If set to <code>true</code>, padding will be applied to the video using a black color in order to fit the given dimensions.</td></tr></tbody></table>
<table><thead><tr><th width="93" data-type="number">Index</th><th width="99">Name</th><th width="87">Type</th><th data-type="checkbox">Required</th><th width="91">Default</th><th width="515">Description</th></tr></thead><tbody><tr><td>1</td><td>name</td><td>string</td><td>true</td><td>–</td><td>label for stream quality. highly recommended to use string labels like <code>720p</code></td></tr><tr><td>2</td><td>width</td><td>int</td><td>true</td><td>–</td><td></td></tr><tr><td>3</td><td>height</td><td>int</td><td>true</td><td>–</td><td></td></tr><tr><td>4</td><td>format</td><td>X264</td><td>true</td><td>–</td><td>by default, the encoding format for video is <code>X264</code>. However, users can specify additional options for this format, including adjusting the <code>kilobitrate</code> for both <code>audio</code> and <code>video</code>. This allows for more precise configuration and optimization of the user's encoding preferences.</td></tr><tr><td>5</td><td>padding</td><td>bool</td><td>false</td><td>false</td><td>If set to <code>true</code>, padding will be applied to the video using a black color in order to fit the given dimensions.</td></tr></tbody></table>

```php
<?php
Expand Down
2 changes: 0 additions & 2 deletions docs/getting-started/laravel-nova-integration-1.md

This file was deleted.

6 changes: 5 additions & 1 deletion docs/getting-started/support-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ Your contribution, no matter how small, is deeply appreciated.&#x20;

Thank you for your support!

[![Donate](https://mostafaznv.github.io/donate/donate.svg)](https://mostafaznv.github.io/donate)
<div align="left">

<figure><img src="https://mostafaznv.github.io/donate/donate.svg" alt="" width="188"><figcaption></figcaption></figure>

</div>



48 changes: 31 additions & 17 deletions src/Storage/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use ColorThief\ColorThief;
use Exception;
use Illuminate\Http\UploadedFile;
use Intervention\Image\Image as InterventionImage;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\ImageManager;
use JetBrains\PhpStorm\ArrayShape;
use Mostafaznv\Larupload\DTOs\Style\ImageStyle;
Expand All @@ -19,7 +19,7 @@ class Image
{
protected readonly UploadedFile $file;

protected readonly InterventionImage $image;
protected readonly ImageInterface $image;

protected readonly string $disk;

Expand All @@ -37,11 +37,12 @@ public function __construct(UploadedFile $file, string $disk, LaruploadImageLibr

$path = $file->getRealPath();

$imageManager = new ImageManager([
'driver' => $library === LaruploadImageLibrary::GD ? 'gd' : 'imagick',
]);
$imageManager = $library === LaruploadImageLibrary::GD
? ImageManager::gd()
: ImageManager::imagick();

$this->image = $imageManager->make($path);

$this->image = $imageManager->read($path);
}


Expand Down Expand Up @@ -76,7 +77,7 @@ public function resize(string $saveTo, ImageStyle $style): bool


if ($this->driverIsLocal) {
$this->image->save($saveTo);
$this->save($saveTo);
}
else {
list($path, $name) = split_larupload_path($saveTo);
Expand All @@ -85,7 +86,7 @@ public function resize(string $saveTo, ImageStyle $style): bool
$tempName = time() . '-' . $name;
$temp = "$tempDir/$tempName";

$this->image->save($temp);
$this->save($temp);

Storage::disk($this->disk)->putFileAs($path, new File($temp), $name);

Expand Down Expand Up @@ -189,9 +190,7 @@ private function resizeAuto(int $width = null, int $height = null): void
*/
private function resizeCrop(int $width, int $height): void
{
$this->image->fit($width, $height, function($constraint) {
$constraint->upsize();
});
$this->image->cover($width, $height);
}

/**
Expand All @@ -200,9 +199,9 @@ private function resizeCrop(int $width, int $height): void
*/
private function resizeLandscape(int $width): void
{
$this->image->widen($width, function($constraint) {
$constraint->upsize();
});
$this->image->scale(
width: $width
);
}

/**
Expand All @@ -211,9 +210,9 @@ private function resizeLandscape(int $width): void
*/
private function resizePortrait(int $height): void
{
$this->image->heighten($height, function($constraint) {
$constraint->upsize();
});
$this->image->scale(
height: $height
);
}

/**
Expand All @@ -224,4 +223,19 @@ private function resizeExact(int $width, int $height): void
{
$this->image->resize($width, $height);
}

/**
* Save image file
*
* @param string $path
* @return void
*/
private function save(string $path): void
{
$isSvg = $this->file->getExtension() === 'svg' || $this->file->getClientOriginalExtension() === 'svg';

$isSvg
? $this->image->toPng()->save($path)
: $this->image->encode()->save($path);
}
}
Binary file modified tests/Support/Data/image.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/Support/LaruploadTestConsts.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class LaruploadTestConsts
'width' => 150,
'height' => 189,
'mime_type' => 'image/gif',
'color' => '#0a660b',
'color' => '#086e09',
'name' => [
'hash' => '710bf7618c3a942d5c3279ff0bb282c1.gif',
]
Expand Down

0 comments on commit b2d39b1

Please sign in to comment.