Skip to content

Commit

Permalink
PHP 8 version
Browse files Browse the repository at this point in the history
  • Loading branch information
robsonvleite committed Apr 7, 2022
1 parent 11e2121 commit 123cdfa
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 60 deletions.
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
[![PHP from Packagist](https://img.shields.io/packagist/php-v/coffeecode/uploader.svg?style=flat-square)](https://packagist.org/packages/coffeecode/uploader)
[![Latest Version](https://img.shields.io/github/release/robsonvleite/uploader.svg?style=flat-square)](https://github.com/robsonvleite/uploader/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Build](https://img.shields.io/scrutinizer/build/g/robsonvleite/uploader.svg?style=flat-square)](https://scrutinizer-ci.com/g/robsonvleite/uploader)
[![Quality Score](https://img.shields.io/scrutinizer/g/robsonvleite/uploader.svg?style=flat-square)](https://scrutinizer-ci.com/g/robsonvleite/uploader)
[![Total Downloads](https://img.shields.io/packagist/dt/coffeecode/uploader.svg?style=flat-square)](https://packagist.org/packages/coffeecode/uploader)

###### Uploader is a set of small classes for sending images, files, and media received by a form of your application. The Uploader handles, validates and sends the files to your server. Image class can still handle sizes with the gd library.

Uploader é um conjunto de pequenas classes para envio de imagens, arquivos e midias recebidos por um formulário da sua
Uploader é um conjunto de pequenas classes para envio de imagens, arquivos e midias recebidos por um formulário de sua
aplicação. O Uploader trata, valida e envia os arquivos a seu servidor. A classe de imagem ainda consegue tratar
tamanhos com a biblioteca gd.

## About CoffeeCode

###### CoffeeCode is a set of small and optimized PHP components for common tasks. Held by Robson V. Leite, and the UpInside team. With them, you perform routine tasks with fewer lines, writing less and doing much more.
###### CoffeeCode is a set of small and optimized PHP components for common tasks. Held by Robson V. Leite and the UpInside team. With them you perform routine tasks with fewer lines, writing less and doing much more.

CoffeeCode é um conjunto de pequenos e otimizados componentes PHP para tarefas comuns. Mantido por Robson V. Leite e a
equipe UpInside. Com eles você executa tarefas rotineiras com poucas linhas, escrevendo menos e fazendo muito mais.
Expand Down Expand Up @@ -52,7 +51,7 @@ composer require coffeecode/uploader
Para mais detalhes sobre como usar o upload, veja uma pasta de exemplo no diretório do componente. Nela terá um exemplo
de uso para cada classe. CoffeeCode Uploader funciona assim:

#### Upload an Image
#### Upload Image

```php
<?php
Expand All @@ -62,14 +61,14 @@ $image = new CoffeeCode\Uploader\Image("uploads", "images", 600);
if ($_FILES) {
try {
$upload = $image->upload($_FILES['image'], $_POST['name']);
echo "<img src='$upload' width='100%'>";
echo "<img src='{$upload}' width='100%'>";
} catch (Exception $e) {
echo "<p>(!) $e->getMessage()</p>";
echo "<p>(!) {$e->getMessage()}</p>";
}
}
```

#### Upload an File
#### Upload File

```php
<?php
Expand All @@ -79,14 +78,14 @@ $file = new CoffeeCode\Uploader\File("uploads", "files");
if ($_FILES) {
try {
$upload = $file->upload($_FILES['file'], $_POST['name']);
echo "<p><a href='$upload' target='_blank'>@CoffeeCode</a></p>";
echo "<p><a href='{$upload}' target='_blank'>@CoffeeCode</a></p>";
} catch (Exception $e) {
echo "<p>(!) $e->getMessage()</p>";
echo "<p>(!) {$e->getMessage()}</p>";
}
}
```

#### Upload an Media
#### Upload Media

```php
<?php
Expand All @@ -96,9 +95,9 @@ $media = new CoffeeCode\Uploader\Media("uploads", "medias");
if ($_FILES) {
try {
$upload = $media->upload($_FILES['file'], $_POST['name']);
echo "<p><a href='$upload' target='_blank'>@CoffeeCode</a></p>";
echo "<p><a href='{$upload}' target='_blank'>@CoffeeCode</a></p>";
} catch (Exception $e) {
echo "<p>(!) $e->getMessage()</p>";
echo "<p>(!) {$e->getMessage()}</p>";
}
}
```
Expand All @@ -108,14 +107,16 @@ if ($_FILES) {
```php
<?php

$postscript = new CoffeeCode\Uploader\Send("uploads", "postscript", ["application/postscript"], ["ai"]);
$postscript = new CoffeeCode\Uploader\Send("uploads", "postscript", [
"application/postscript"
], ["ai"]);

if ($_FILES) {
try {
$upload = $postscript->upload($_FILES['file'], $_POST['name']);
echo "<p><a href='$upload' target='_blank'>@CoffeeCode</a></p>";
echo "<p><a href='{$upload}' target='_blank'>@CoffeeCode</a></p>";
} catch (Exception $e) {
echo "<p>(!) $e->getMessage()</p>";
echo "<p>(!) {$e->getMessage()}</p>";
}
}
```
Expand All @@ -132,7 +133,7 @@ try {
}
echo "Success!";
} catch (Exception $e) {
echo "<p>(!) $e->getMessage()</p>";
echo "<p>(!) {$e->getMessage()}</p>";
}
```

Expand Down
8 changes: 3 additions & 5 deletions example/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
if ($_FILES) {
try {
$upload = $file->upload($_FILES['file'], $_POST['name']);
echo "<p><a href='$upload' target='_blank'>@CoffeeCode</a></p>";
echo "<p><a href='{$upload}' target='_blank'>@CoffeeCode</a></p>";
} catch (Exception $e) {
echo "<p>(!) $e->getMessage()</p>";
echo "<p>(!) {$e->getMessage()}</p>";
}
}
?>
<label>
<input type="text" name="name" placeholder="File Name" required/>
</label>
<input type="text" name="name" placeholder="File Name" required/>
<input type="file" name="file" required/>
<button>Send File</button>
</form>
Expand Down
8 changes: 3 additions & 5 deletions example/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
if ($_FILES) {
try {
$upload = $image->upload($_FILES['image'], $_POST['name']);
echo "<img alt='' src='$upload' width='100%'>";
echo "<img src='{$upload}' width='100%'>";
} catch (Exception $e) {
echo "<p>(!) $e->getMessage()</p>";
echo "<p>(!) {$e->getMessage()}</p>";
}
}
?>
<label>
<input type="text" name="name" placeholder="Image Name" required/>
</label>
<input type="text" name="name" placeholder="Image Name" required/>
<input type="file" name="image" required/>
<button>Send Image</button>
</form>
Expand Down
8 changes: 3 additions & 5 deletions example/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
if ($_FILES) {
try {
$upload = $media->upload($_FILES['file'], $_POST['name']);
echo "<p><a href='$upload' target='_blank'>@CoffeeCode</a></p>";
echo "<p><a href='{$upload}' target='_blank'>@CoffeeCode</a></p>";
} catch (Exception $e) {
echo "<p>(!) $e->getMessage()</p>";
echo "<p>(!) {$e->getMessage()}</p>";
}
}
?>
<label>
<input type="text" name="name" placeholder="File Name" required/>
</label>
<input type="text" name="name" placeholder="File Name" required/>
<input type="file" name="file" required/>
<button>Send Media</button>
</form>
Expand Down
14 changes: 5 additions & 9 deletions example/send.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,20 @@
// "application/postscript"
//], ["ai"], false); //SEM PASTAS DE ANO E MÊS

$postscript = new CoffeeCode\Uploader\Send(
"uploads", "postscript", [
$postscript = new CoffeeCode\Uploader\Send("uploads", "postscript", [
"application/postscript"
], ["ai"]
);
], ["ai"]);

if ($_FILES) {
try {
$upload = $postscript->upload($_FILES['file'], $_POST['name']);
echo "<p><a href='$upload' target='_blank'>@CoffeeCode</a></p>";
echo "<p><a href='{$upload}' target='_blank'>@CoffeeCode</a></p>";
} catch (Exception $e) {
echo "<p>(!) $e->getMessage()</p>";
echo "<p>(!) {$e->getMessage()}</p>";
}
}
?>
<label>
<input type="text" name="name" placeholder="File Name" required/>
</label>
<input type="text" name="name" placeholder="File Name" required/>
<input type="file" name="file" required/>
<button>Send File</button>
</form>
Expand Down
4 changes: 2 additions & 2 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function upload(array $file, string $name): string
}

$this->name($name);
move_uploaded_file($file['tmp_name'], "$this->path/$this->name");
return "$this->path/$this->name";
move_uploaded_file($file['tmp_name'], "{$this->path}/{$this->name}");
return "{$this->path}/{$this->name}";
}
}
10 changes: 5 additions & 5 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public function upload(array $image, string $name, int $width = 2000, ?array $qu
$this->name($name);

if ($this->ext == "gif") {
move_uploaded_file($image['tmp_name'], "$this->path/$this->name");
return "$this->path/$this->name";
move_uploaded_file("{$image['tmp_name']}", "{$this->path}/{$this->name}");
return "{$this->path}/{$this->name}";
}

$this->imageGenerate($width, ($quality ?? ["jpg" => 75, "png" => 5]));
return "$this->path/$this->name";
return "{$this->path}/{$this->name}";
}

/**
Expand Down Expand Up @@ -94,14 +94,14 @@ private function imageGenerate(int $width, array $quality): void

if ($this->ext == "jpg") {
imagecopyresampled($imageCreate, $this->file, 0, 0, 0, 0, $imageW, $imageH, $fileX, $fileY);
imagejpeg($imageCreate, "$this->path/$this->name", $quality['jpg']);
imagejpeg($imageCreate, "{$this->path}/{$this->name}", $quality['jpg']);
}

if ($this->ext == "png") {
imagealphablending($imageCreate, false);
imagesavealpha($imageCreate, true);
imagecopyresampled($imageCreate, $this->file, 0, 0, 0, 0, $imageW, $imageH, $fileX, $fileY);
imagepng($imageCreate, "$this->path/$this->name", $quality['png']);
imagepng($imageCreate, "{$this->path}/{$this->name}", $quality['png']);
}

imagedestroy($this->file);
Expand Down
4 changes: 2 additions & 2 deletions src/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function upload(array $media, string $name): string
}

$this->name($name);
move_uploaded_file($media['tmp_name'], "$this->path/$this->name");
return "$this->path/$this->name";
move_uploaded_file($media['tmp_name'], "{$this->path}/{$this->name}");
return "{$this->path}/{$this->name}";
}
}
4 changes: 2 additions & 2 deletions src/Send.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function upload(array $file, string $name): string
}

$this->name($name);
move_uploaded_file($file['tmp_name'], "$this->path/$this->name");
return "$this->path/$this->name";
move_uploaded_file($file['tmp_name'], "{$this->path}/{$this->name}");
return "{$this->path}/{$this->name}";
}
}
18 changes: 9 additions & 9 deletions src/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ abstract class Uploader
public function __construct(string $uploadDir, string $fileTypeDir, bool $monthYearPath = true)
{
$this->dir($uploadDir);
$this->dir("$uploadDir/$fileTypeDir");
$this->path = "$uploadDir/$fileTypeDir";
$this->dir("{$uploadDir}/{$fileTypeDir}");
$this->path = "{$uploadDir}/{$fileTypeDir}";

if ($monthYearPath) {
$this->path("$uploadDir/$fileTypeDir");
$this->path("{$uploadDir}/{$fileTypeDir}");
}
}

Expand Down Expand Up @@ -76,10 +76,10 @@ protected function name(string $name): string
str_replace(" ", "-", trim(strtr(utf8_decode($name), utf8_decode($formats), $replace)))
);

$this->name = "$name." . $this->ext;
$this->name = "{$name}." . $this->ext;

if (file_exists("$this->path/$this->name") && is_file("$this->path/$this->name")) {
$this->name = "$name-" . time() . ".$this->ext";
if (file_exists("{$this->path}/{$this->name}") && is_file("{$this->path}/{$this->name}")) {
$this->name = "{$name}-" . time() . ".{$this->ext}";
}
return $this->name;
}
Expand All @@ -102,9 +102,9 @@ protected function path(string $path): void
{
list($yearPath, $mothPath) = explode("/", date("Y/m"));

$this->dir("$path/$yearPath");
$this->dir("$path/$yearPath/$mothPath");
$this->path = "$path/$yearPath/$mothPath";
$this->dir("{$path}/{$yearPath}");
$this->dir("{$path}/{$yearPath}/{$mothPath}");
$this->path = "{$path}/{$yearPath}/{$mothPath}";
}

/**
Expand Down

0 comments on commit 123cdfa

Please sign in to comment.