Skip to content

Commit

Permalink
Coffecode PHP8 Uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
robsonvleite committed Apr 2, 2022
1 parent 182682c commit 14fbff5
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 59 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified CONTRIBUTING.md
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion composer.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
}
],
"require": {
"php": ">=7.2",
"php": ">=8.0",
"ext-gd": "*",
"ext-mbstring": "*",
"ext-exif": "*"
Expand Down
Empty file modified example/file.php
100644 → 100755
Empty file.
Empty file modified example/image.php
100644 → 100755
Empty file.
Empty file modified example/media.php
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions example/send.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<div class="form">
<form name="env" method="post" enctype="multipart/form-data">
<p>Envie um arquivo AI (postscript)</p>
<?php
require __DIR__ . "/../src/Uploader.php";
require __DIR__ . "/../src/Send.php";
Expand Down
Empty file modified example/style.css
100644 → 100755
Empty file.
14 changes: 8 additions & 6 deletions src/File.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CoffeeCode\Uploader;

use Exception;

/**
* Class CoffeeCode File
*
Expand All @@ -15,7 +17,7 @@ class File extends Uploader
* @var array allowed file types
* https://www.freeformatter.com/mime-types-list.html
*/
protected static $allowTypes = [
protected static array $allowTypes = [
"application/zip",
'application/x-rar-compressed',
'application/x-bzip',
Expand All @@ -33,7 +35,7 @@ class File extends Uploader
* Allowed extensions to types.
* @var array
*/
protected static $extensions = [
protected static array $extensions = [
"zip",
"rar",
"bz",
Expand All @@ -50,15 +52,15 @@ class File extends Uploader
/**
* @param array $file
* @param string $name
* @return null|string
* @throws \Exception
* @return string
* @throws Exception
*/
public function upload(array $file, string $name): string
{
$this->ext = mb_strtolower(pathinfo($file['name'])['extension']);
$this->ext($file);

if (!in_array($file['type'], static::$allowTypes) || !in_array($this->ext, static::$extensions)) {
throw new \Exception("Not a valid file type or extension");
throw new Exception("Not a valid file type or extension");
}

$this->name($name);
Expand Down
47 changes: 12 additions & 35 deletions src/Image.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CoffeeCode\Uploader;

use Exception;

/**
* Class CoffeeCode Image
*
Expand All @@ -14,7 +16,7 @@ class Image extends Uploader
* Allow jpg, png and gif images, use from check. For new extensions check the imageCrete method
* @var array allowed media types
*/
protected static $allowTypes = [
protected static array $allowTypes = [
"image/jpeg",
"image/png",
"image/gif",
Expand All @@ -26,20 +28,20 @@ class Image extends Uploader
* @param int $width
* @param array|null $quality
* @return string
* @throws \Exception
* @throws Exception
*/
public function upload(array $image, string $name, int $width = 2000, ?array $quality = null): string
{
if (empty($image['type'])) {
throw new \Exception("Not a valid data from image");
throw new Exception("Not a valid data from image");
}

if (!$this->imageCreate($image)) {
throw new \Exception("Not a valid image type or extension");
} else {
$this->name($name);
throw new Exception("Not a valid image type or extension");
}

$this->name($name);

if ($this->ext == "gif") {
move_uploaded_file("{$image['tmp_name']}", "{$this->path}/{$this->name}");
return "{$this->path}/{$this->name}";
Expand All @@ -61,7 +63,6 @@ protected function imageCreate(array $image): bool
if ($image['type'] == "image/jpeg") {
$this->file = imagecreatefromjpeg($image['tmp_name']);
$this->ext = "jpg";
$this->checkAngle($image);
return true;
}

Expand All @@ -85,10 +86,10 @@ protected function imageCreate(array $image): bool
*/
private function imageGenerate(int $width, array $quality): void
{
$fileX = imagesx($this->file);
$fileY = imagesy($this->file);
$imageW = ($width < $fileX ? $width : $fileX);
$imageH = ($imageW * $fileY) / $fileX;
$fileX = intval(imagesx($this->file));
$fileY = intval(imagesy($this->file));
$imageW = ($width < $fileX ? (int)$width : (int)$fileX);
$imageH = intval(($imageW * $fileY) / $fileX);
$imageCreate = imagecreatetruecolor($imageW, $imageH);

if ($this->ext == "jpg") {
Expand All @@ -106,28 +107,4 @@ private function imageGenerate(int $width, array $quality): void
imagedestroy($this->file);
imagedestroy($imageCreate);
}

/**
* Check image (JPG, PNG) angle and rotate from exif data.
* @param $image
*/
private function checkAngle($image): void
{
$exif = @exif_read_data($image["tmp_name"]);
$orientation = (!empty($exif["Orientation"]) ? $exif["Orientation"] : null);

switch ($orientation) {
case 8:
$this->file = imagerotate($this->file, 90, 0);
break;
case 3:
$this->file = imagerotate($this->file, 180, 0);
break;
case 6:
$this->file = imagerotate($this->file, -90, 0);
break;
}

return;
}
}
14 changes: 8 additions & 6 deletions src/Media.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CoffeeCode\Uploader;

use Exception;

/**
* Class CoffeeCode Media
*
Expand All @@ -15,7 +17,7 @@ class Media extends Uploader
* @var array allowed media types
* https://www.freeformatter.com/mime-types-list.html
*/
protected static $allowTypes = [
protected static array $allowTypes = [
"audio/mp3",
"audio/mpeg",
"video/mp4",
Expand All @@ -25,23 +27,23 @@ class Media extends Uploader
* Allowed extensions to types.
* @var array
*/
protected static $extensions = [
protected static array $extensions = [
"mp3",
"mp4"
];

/**
* @param array $media
* @param string $name
* @return null|string
* @throws \Exception
* @return string
* @throws Exception
*/
public function upload(array $media, string $name): string
{
$this->ext = mb_strtolower(pathinfo($media['name'])['extension']);
$this->ext($media);

if (!in_array($media['type'], static::$allowTypes) || !in_array($this->ext, static::$extensions)) {
throw new \Exception("Not a valid media type or extension");
throw new Exception("Not a valid media type or extension");
}

$this->name($name);
Expand Down
8 changes: 5 additions & 3 deletions src/Send.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CoffeeCode\Uploader;

use Exception;

/**
* Class CoffeeCode Send
*
Expand Down Expand Up @@ -36,14 +38,14 @@ public function __construct(
* @param array $file
* @param string $name
* @return string
* @throws \Exception
* @throws Exception
*/
public function upload(array $file, string $name): string
{
$this->ext = mb_strtolower(pathinfo($file['name'])['extension']);
$this->ext($file);

if (!in_array($file['type'], static::$allowTypes) || !in_array($this->ext, static::$extensions)) {
throw new \Exception("Not a valid file type or extension");
throw new Exception("Not a valid file type or extension");
}

$this->name($name);
Expand Down
27 changes: 19 additions & 8 deletions src/Uploader.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
abstract class Uploader
{
/** @var string */
protected $path;
protected string $path;

/** @var resource */
protected $file;

/** @var string */
protected $name;
protected string $name;

/** @var string */
protected $ext;
protected string $ext;

/** @var array */
protected static $allowTypes = [];
protected static array $allowTypes = [];

/** @var array */
protected static $extensions = [];
protected static array $extensions = [];

/**
* @param string $uploadDir
Expand Down Expand Up @@ -67,11 +67,14 @@ public static function isExtension(): array
*/
protected function name(string $name): string
{
$name = filter_var(mb_strtolower($name), FILTER_SANITIZE_STRIPPED);
$name = filter_var(mb_strtolower($name), FILTER_UNSAFE_RAW);
$formats = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜüÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿRr"!@#$%&*()_-+={[}]/?;:.,\\\'<>°ºª';
$replace = 'aaaaaaaceeeeiiiidnoooooouuuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr ';
$name = str_replace(["-----", "----", "---", "--"], "-",
str_replace(" ", "-", trim(strtr(utf8_decode($name), utf8_decode($formats), $replace))));
$name = str_replace(
["-----", "----", "---", "--"],
"-",
str_replace(" ", "-", trim(strtr(utf8_decode($name), utf8_decode($formats), $replace)))
);

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

Expand Down Expand Up @@ -104,6 +107,14 @@ protected function path(string $path): void
$this->path = "{$path}/{$yearPath}/{$mothPath}";
}

/**
* @param array $file
*/
protected function ext(array $file): void
{
$this->ext = mb_strtolower(pathinfo($file['name'])['extension']);
}

/**
* @param $inputName
* @param $files
Expand Down

0 comments on commit 14fbff5

Please sign in to comment.