Skip to content

Instantly share code, notes, and snippets.

@atorscho
Last active September 19, 2022 09:32
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save atorscho/d59a418eca5bfdb712b5 to your computer and use it in GitHub Desktop.
Save atorscho/d59a418eca5bfdb712b5 to your computer and use it in GitHub Desktop.
Helpers to manipulate with files and images for Laravel projects.
<?php
namespace App\Helpers;
use Closure;
use Image;
use Intervention\Image\Constraint;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class FileHelper
{
/**
* Save the uploaded image.
*
* @param UploadedFile $file Uploaded file.
* @param int $maxWidth
* @param string $path
* @param Closure $callback Custom file naming method.
*
* @return string File name.
*/
public static function saveImage(UploadedFile $file, $maxWidth = 150, $path = null, Closure $callback = null)
{
if (!$path) {
$path = config('filesystems.uploads.images');
}
if ($callback) {
$fileName = $callback();
} else {
$fileName = self::getFileName($file);
}
$img = self::makeImage($file);
$img = self::resizeImage($img, $maxWidth);
self::uploadImage($img, $fileName, $path);
return $fileName;
}
/**
* Get uploaded file's name.
*
* @param UploadedFile $file
*
* @return null|string
*/
protected static function getFileName(UploadedFile $file)
{
$filename = $file->getClientOriginalName();
$filename = date('Ymd_His') . '_' . strtolower(pathinfo($filename, PATHINFO_FILENAME)) . '.' . pathinfo($filename, PATHINFO_EXTENSION);
return $filename;
}
/**
* Create the image from upload file.
*
* @param UploadedFile $file
*
* @return \Intervention\Image\Image
*/
protected static function makeImage(UploadedFile $file)
{
return Image::make($file);
}
/**
* Resize image to the configured size.
*
* @param \Intervention\Image\Image $img
* @param int $maxWidth
*
* @return \Intervention\Image\Image
*/
protected static function resizeImage(\Intervention\Image\Image $img, $maxWidth = 150)
{
$img->resize($maxWidth, null, function (Constraint $constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
return $img;
}
/**
* Save the uploaded image to the file system.
*
* @param \Intervention\Image\Image $img
* @param string $fileName
* @param string $path
*/
protected static function uploadImage($img, $fileName, $path)
{
$img->save(public_path($path . $fileName));
}
}
<?php
use App\Helpers\FileHelper;
use Symfony\Component\HttpFoundation\File\UploadedFile;
if (!function_exists('save_image')) {
/**
* Save the uploaded image.
*
* @param UploadedFile $file Uploaded file.
* @param int $maxWidth
* @param string $path
* @param Closure $callback Custom file naming method.
*
* @return string File name.
*/
function save_image(UploadedFile $file, $maxWidth = 150, $path = null, Closure $callback = null)
{
return FileHelper::saveImage($file, $maxWidth, $path, $callback);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment