ApexLeo's avatar

Laravel Image Upload Issues ( Experts please )

Hi Everyone, i am having issues with image upload functionality. Let me just tell u that i am able to upload images to server so i am not here for enctype problem. lets just go through the point i have, and m also using laravel intervention.

  1. When i try to save my image using saveAs function it save my image perfectly.
  2. but when i try to save my image with intervention $image->make() it turn my webpage to blank i mean my request stop and show a complete white page. and it happen only when i try to save image greater then 200 - 300 kB file. but intervention work fine with 20-80 kB file.
  3. now lets get to laravel image validation. when i use 'image' => 'image|mimes:jpg.etc' it gives me error Image must be an image but when i remove "image" validation and only remain "mimes" validation then there is no error and image is saved perfectly. and again its happening with 200-300 kB file which is jpg and woking fine on 20-80 kB file which in png.

anyone have any suggestions??

0 likes
2 replies
ApexLeo's avatar

@loyd here is the code

public static function StoreImage($image, $fieldName, $path)
    {
        $thumbData = [];
        $imageData = [];

        if (isset($image)) {
            $fileNameWithExt = $image->getClientOriginalName();
            $fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
            $extension = $image->getClientOriginalExtension();
            $fileNameToStore = time() . '.' . $extension;

            $storagePath = storage_path('app/'.$path);
            if(!File::isDirectory($storagePath)) { File::makeDirectory($storagePath, 0777, true, true); }

            $imageData = self::generateImage($image, $path, $fileNameToStore);
            $thumbData = self::generateThumbnail($image, $path, $fileNameToStore);

        } else {
            return false;
        }

        return $data = [
            'image' => $imageData,
            'thumb_image' => $thumbData,
            'type' => mime_content_type($storagePath.$fileNameToStore),
            'extension' => strtolower($extension),
        ];
    }

    private static function generateThumbnail($image, $path, $fileNameToStore)
    {
        $fileNameToStore = 'thumb_' . $fileNameToStore;
        $image = self::createThumbnail($image, $path, $fileNameToStore, 200, 200);

        return $data = [
            'imageName' => $fileNameToStore,
            'path' => $path,
            'size' => $image->filesize(),
        ];
    }

    public static function createThumbnail($image, $path, $fileNameToStore, $width, $height)
    {
        $image = Image::make($image)->resize($width, $height, function ($constraint) {
            $constraint->aspectRatio();
        });
        $image->save(storage_path('app/' . $path . $fileNameToStore)); // at this point my page goes blank
        return $image;
    }

    private static function generateImage($image, $path, $fileNameToStore)
    {
//        $image = Image::make($image);
//        $image->save(storage_path('app/' . $path . $fileNameToStore));  // at this point my page goes blank
        $image->storeAs($path, $fileNameToStore);

        return $data = [
            'imageName' => $fileNameToStore,
            'path' => $path,
            'size' => filesize(storage_path('app/' . $path . $fileNameToStore)), // $image->filesize(),
        ];
    }

and here is the validation for image

'nullable|image|mimes:jpeg,jpg,png|max:2048' // this is not working saying image must be an image
'nullable|mimes:jpeg,jpg,png|max:2048' // this one is working fine

Please or to participate in this conversation.