Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

barki's avatar
Level 1

Image intervention - image source not readable

I am using file_get_contents() php function to get images from the url (importing products to database and fetching images url from the excel sheet) and then using image intervention to upload to my server. Some images are downloaded and then uploaded but some throughs exception 'image source not readable', Kindly look at my code and help me

				$image = null;
				if (!empty($value[1]) AND @fopen($value[1], 'r')) {
    					$image = @file_get_contents($value[1]);
				}
				$product_image = '';
				if ($image) {
								$directory = 'products';
								$name = $this->runCode() . '-' . Str::slug($value[0]) . time();
								$image_url = $directory . $name;
								$product_image = env('OBJECT_URL') .  $image_url . '.png';
								UploadHelper::base64($directory, $image, $name, 'png');
				}

				my upload helper:

				public static function base64($directory, $image, $name,  $format)
				 {
    					$imageRender =  Image::make($image)->encode($format);
    					Storage::disk('linode')->put($directory . $name . '.' . $format, $imageRender, 'public');
}
0 likes
15 replies
Sinnbeck's avatar

Can you share one of the urls that fails, so we can try it out?

barki's avatar
Level 1

@Sinnbeck basically this is worksheet and contains more than 100 products, even I do not know, which url fails

barki's avatar
Level 1

@Sinnbeck no, basically I am using try catch block, unfortunately I cannot get which url is throwing error

$data = $xlsx->rows();
DB::beginTransaction();
try {
        foreach ($data as $key => $value) {
		// ignore error
            $image = null;
            if (!empty($value[1]) AND @fopen($value[1], 'r')) {
                $image = @file_get_contents($value[1]);
            }

            // if image is not found in the url 

            // return is_file($image);
            $product_image = '';
            if ($image) {
                $directory = 'products';
                $name = $this->runCode() . '-' . Str::slug($value[0]) . time();
                $image_url = $directory . $name;
                $product_image = env('OBJECT_URL') .  $image_url . '.png';
                UploadHelper::base64($directory, $image, $name, 'png');
            }
            MproductToImages::create([
                'product_id' => $product->product_id,
                'product_image' => $product_image
            ]);
DB::commit();

        return response()->json(['msg' => 'Product has been successfully imported.']);
    } catch (\Exception $e) {
        DB::rollBack();

        return response()->json(['msgErr' => $e->getMessage()]);
    }
Sinnbeck's avatar

@barki So when it fails it hits this?

DB::rollBack();

 return response()->json(['msgErr' => $e->getMessage()]);

If so, you should be able to get the last used image. While debugging you can move it inside the foreach to easily get the last $value

Sinnbeck's avatar

@barki I tried copying the code into my editor but it seems broken. Missing } somewhere

Try this

} catch (\Exception $e) {
        DB::rollBack();

        return response()->json(['msgErr' =>$value[1]]);
    }
Sinnbeck's avatar

@barki I cannot open that in the browser either :) You can then update the error to specifiy the bad url

barki's avatar
Level 1

@Sinnbeck that's why it throws exception, how to handle this kind of scenario?

Sinnbeck's avatar

@barki Not sure what you mean? Your exception is being caught the problem is reported to the user? What else do you want ?

Sinnbeck's avatar

@barki I dont think fopen makes much sense in this case. I would just go with file_get_contents()

barki's avatar
Level 1

@Sinnbeck I want, basically, to skip that image where image source not found or forbidden like the above, which php function is meaningful in this scenario, image can be empty string but I dont want the overall process to be stopped

Sinnbeck's avatar

@barki Just dont return then

} catch (\Exception $e) {
        DB::rollBack();
    }

Please or to participate in this conversation.