Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 29
ImageHelper
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
72
0.00% covered (danger)
0.00%
0 / 29
 upload
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 8
 resize
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 13
 get
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 8
<?php
namespace App\Helpers;
use ImageResize;
// use phpDocumentor\Reflection\File;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
/**
* Class based Helper to upload and resize Images
*/
class ImageHelper
{
    /**
     * Helper to upload and resize Images
     * @param $image File to upload
     * @param int $width
     * @param int $height
     * @return string
     */
    public static function upload($image, $width= 200, $height= 200)
    {
        $newFileName = time().'.'.$image->extension();
        // Resize image and upload to Thumbnail path
        $destinationPath = public_path('/uploads/images/thumbs');
        $img = ImageResize::make($image->path());
        $img->resize($width, $height, function ($constraint) {
            $constraint->aspectRatio(); // to save the ratio
        })->save($destinationPath.'/'.$newFileName);
        // upload file to  Original path
        $destinationPath = public_path('/uploads/images');
        $image->move($destinationPath, $newFileName);
        return $newFileName;
    }
    /**
     * Class based Helper to resize Images
     * @param $image file to resize, string
     * @param int $width
     * @param int $height
     * @return string to resized image url
     */
    public static function resize($image, $width= 200, $height= 200): string
    {
        if (preg_match('/https:\/\/img\.youtube\.com/', $image, $match) ) {
            return $image;
        }
        $image= public_path($image);
        if (! File::exists($image) ) {
            return asset('logo.png');
        }
        $imageInfo= pathinfo($image);
        $newFileName = $imageInfo['filename'].'-'.$width.'X'.$height.'.'.$imageInfo['extension'];
        // Resize image and upload to Thumbnail path
        // Thumbnail Path
        $upload_dir= '/uploads/images/thumbs';
        $destinationPath = public_path($upload_dir);
        // Save Image
        $img = ImageResize::make( $image );
        $img->resize($width, $height, function ($constraint) {
            $constraint->aspectRatio(); // to save the ratio
        })->save($destinationPath.'/'.$newFileName);
        return asset($upload_dir.'/'.$newFileName);
    }
    /**
     * Class based Helper to Check if Image exist
     * @param $image file
     * @return string image url
     */
    public static function get( $image ): string
    {
        if(!$image) {
            return asset('logo.png');
        }
        if (preg_match('/https:\/\/img\.youtube\.com/', $image, $match) ) {
            return $image;
        }
        $imagePath= public_path($image);
        if (File::exists($imagePath)) {
            return asset($image);
        } else {
            return asset('logo.png');
        }
    }
}