ralphmorris's avatar

Image Intervention - pixelation when trying to create variations

I have the following methods:

    public function saveWithVariations($file, $path, $fileName)
    {
        $imagePath = $file->getRealPath();
        $rawImage = \Image::make($imagePath);

        $rawImage->resize(1000, null, function ($constraint) {
            $constraint->aspectRatio();
            $constraint->upsize();
        })->encode('jpg', 80);

        $uploaded = $this->saveToStorage(
            $rawImage->stream()->__toString(),
            $path . $fileName
        );

        if ($uploaded) 
        {
        $imageVariations = config('imagesizes');

            foreach ($imageVariations as $key => $value) {

                $image = $rawImage;
                
                if ($uploaded) 
                {
                    $uploaded =$this->saveToStorage(
                        $image->fit($value['w'], $value['h'])->stream()->__toString(),
                        $path . $key . '-' . $fileName
                    );
                    }    
            }
            return $uploaded;
        } else 
        {
            return false;
        }

    }

    public function saveToStorage($file, $path)
    {
        return Storage::put($path, $file, 'public');
    }

And a config:

return [
    '860-710' => ['w' => 860, 'h' => 710], // Profile image 430/355
    '560-440'=> ['w' => 560, 'h' => 440], // Artist Index page
    '400'=> ['w' => 400, 'h' => 400],
    '150-124' => ['w' => 150, 'h' => 124], // Profile image thumbnail 75/62
];

If I start reordering the array so that a say 150-124 goes above 860-710 the images become pixilated. This looks like it is because the image is being resized to 150/124 then back up to 860/710 however I am trying to get the rawImage each time instead of keep manipulating the same image so that I don't get this problem. That is why I am only resizing once the variable is named $image. then recalling $rawImage later.

However this doesn't seem to be working. I could just keep them in size order but the problem will arise when image sizes start to overlap. Do I have to recall Image::make each time? Would that be quite expensive load wise?

And help appreciated.

0 likes
0 replies

Please or to participate in this conversation.