Home » Php » php – How to get the size of the image?

php – How to get the size of the image?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have this array wanna get the size of the image:

$main_image = $product->images()->where('main_image', 1)->first();

if($main_image){
    $url = asset('uploads/products/'. $main_image['image']);

    $image[] = [
        "name"  => $main_image->image,
        "type"  => FileUploader::mime_content_type($main_image->image_path),

        "size"  => filesize($url),

        "file"  => $main_image->image_path,
        "local" => $main_image->image_path,
    ];
}

I got this error:

filesize(): stat failed for http://myapp.test/uploads/products/83QhiRFarGpV.jpeg 

Even my image exists in the folder!:

enter image description here

I tried all the options in this one but still same error at all!

How to&Answers:

Your code is using asset() to generate the $url:

http://myapp.test/uploads/products/83QhiRFarGpV.jpeg

The filesize() function works with local files, not URLs.

filesize($main_image->image_path) should work since you are using it in the line above to get the file type.

You probably also have another issue with

   "file"  => $main_image->image_path,
   "local" => $main_image->image_path,

Where you probably want the file to be $url instead.