Can someone point me in the right direction?

Published 1 year ago by madsynn

I need to create a function that will inventory all images and all sizes and versions of it are created on upload and if one is missing in any of the different sizes grab the original image and add it again so all version and sizes are in inventory.

I am not sure how to go about this. Can any of you tell me your thoughts on what to use for each step, i would love to hear them.

This is an example of my upload function with the different sizes being created. I would need this function to check each directory for the file and if missing regenerate it from the original image that is in the $dest.

$dest = 'uploads/products';

$Path = public_path() . $this->imgDir;
$ThumbPath = public_path() . $this->thumbDir;
$LoopPath = public_path() . $this->loopDir;
$ShopLoopPath = public_path() . $this->shopLoopDir;
$FitPath = public_path() . $this->shopLoopDir . "fit/";

File::exists($Path) or File::makeDirectory($Path);
File::exists($ThumbPath) or File::makeDirectory($ThumbPath);
File::exists($LoopPath) or File::makeDirectory($LoopPath);
File::exists($ShopLoopPath) or File::makeDirectory($ShopLoopPath);
File::exists($FitPath) or File::makeDirectory($FitPath);

$name = $request->file('thumbnail')->getClientOriginalName();
$from = $request->file('thumbnail');
Image::make($from)->resize($this->width, $this->height)->save($Path . $name);
Image::make($from)->resize($this->thumbWidth, $this->thumbHeight, function ($constraint) {$constraint->upsize(); })->save($ThumbPath . $name);
Image::make($from)->resize($this->loopWidth, $this->loopHeight, function ($constraint) {$constraint->upsize(); })->save($LoopPath . $name);
Image::make($from)->fit($this->shopLoopWidth, $this->shopLoopHeight, function ($constraint) {$constraint->upsize(); })->save($ShopLoopPath . $name);
Image::make($from)->resize($this->shopLoopWidth, $this->shopLoopHeight, function ($constraint) {$constraint->upsize(); })->save($FitPath . $name);

$request->file('thumbnail')->move($Path, $name);

$product = $request->all();
$product['thumbnail'] = $name;
Best Answer (As Selected By madsynn)
ohffs

It's hard to be very helpful without knowing what all those variables are and what happens after - but you could at least tidy things up a bit by having things like :

// your main function above
$this->createImages($request->file('thumbnail'));

...

public function createImages($fromFile)
{
    $this->createLoopImage($fromFile);
    ...
}

public function createLoopImage($fromFile)
{
    $this->makeImage($this->loopWidth, $this->loopHeight, $fromFile)
         ->save($this->LoopPath . $fromFile->getClientOriginalName());
}

public function makeImage($width, $height, $original)
{
    return Image::make($original)->resize($width, $height, function ($constraint) {
        $constraint->upsize();
    });
}

vipin93
vipin93
1 year ago (61,230 XP)

if u want version then add file name with timestamp like

$filename = time() . '.' . $request->file('thumbnail')->getClientOriginalName();

or

$filename = time() . '.' . $request->file('thumbnail')->getClientOriginalExtension();
ohffs
ohffs
1 year ago (258,180 XP)

It's hard to be very helpful without knowing what all those variables are and what happens after - but you could at least tidy things up a bit by having things like :

// your main function above
$this->createImages($request->file('thumbnail'));

...

public function createImages($fromFile)
{
    $this->createLoopImage($fromFile);
    ...
}

public function createLoopImage($fromFile)
{
    $this->makeImage($this->loopWidth, $this->loopHeight, $fromFile)
         ->save($this->LoopPath . $fromFile->getClientOriginalName());
}

public function makeImage($width, $height, $original)
{
    return Image::make($original)->resize($width, $height, function ($constraint) {
        $constraint->upsize();
    });
}

Please sign in or create an account to participate in this conversation.