-2

I am using the following code to save an image from a URL but sometimes the image URL is bad and there is no image there, OR there is an issue with the image and it saves a zero size file.

<?php 
file_put_contents ("/var/www/html/images/" . $character . ".jpg", 
    file_get_contents($image));

I need to try and find a way to stop this happening as this creates a problem (saving zero size files).

I have tried this, but it still seems to be happening:

$filesize = file_put_contents ("/var/www/html/images/" . $character . ".jpg", 
    file_get_contents($image));

if (($filesize < 10) || ($filesize == "")) {
    echo "Error";
}

Could anyone recommend a more reliable way to do this?

Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
omega1
  • 1,031
  • 4
  • 21
  • 41
  • Is totally unclear what you are asking. Please, edit your question in order to clarify it. – felipsmartins Feb 25 '20 at 07:20
  • I am asking if there is a better way to detect if the image I am saving has a filesize of zero bytes. – omega1 Feb 25 '20 at 08:38
  • Why do you do not google for: "filesize php"? The first result link to official PHP doc filesize() function – felipsmartins Feb 25 '20 at 21:37
  • @felipsmartins Thank you for your unhelpful comments, I know how to Google and clearly others understood my question. I know how to get the filesize in PHP, I was asking if I could get it before I saved it. Have a nice day. – omega1 Feb 26 '20 at 07:20
  • `$foo = file_get_contents(); if(strlen($foo)) { file_put_contents('somefile', $foo);` …? – CBroe Mar 02 '20 at 09:56

2 Answers2

3

Imagick package has methods for doing this

Imagick::getImageGeometry() - returns width and height of an image, or throws an exception.

function isValidImage($filename)
{
    if (!fileexists($filename) return false;
    if (filesize($filename) == 0) return false;

    $image = new imagick($filename);
    $img=$image->getImageGeometry();

    return ($img['width'] > 0 && $img['height'] > 0);
}

EDIT: I have updated my answer with more checks

Eriks Klotins
  • 4,042
  • 1
  • 12
  • 26
  • sorry, but what does it have to do with the question? – felipsmartins Feb 25 '20 at 07:12
  • "Issue with an image" is a bit vague. I have updated my answer with more checks – Eriks Klotins Feb 25 '20 at 07:16
  • This solution will require [ImageMagick](https://www.php.net/manual/en/book.imagick.php) to be installed which does not usually happen in a default PHP installation. – apokryfos Feb 25 '20 at 07:27
  • Yes! Reliably checking if a file contains image data requires parsing the file. – Eriks Klotins Feb 25 '20 at 07:32
  • That is not proper way in order to validate/detect whether file is image or not. It feels wrong and it's not idiomatic. It would recommend to use *fileinfo* instead – felipsmartins Feb 25 '20 at 21:43
  • Look at this post here: https://www.php.net/manual/en/function.finfo-file.php#75275 "Tempting as it may seem to use finfo_file() to validate uploaded image files (Check whether a supposed imagefile really contains an image), the results cannot be trusted." – Eriks Klotins Feb 26 '20 at 08:15
1

I have tried to get an image size by URL. I have get_headers() function to get the image size. Here is an example which is given below:

function checkImageSize($imageUrl){
   if(!empty($imageUrl)){
       $file_headers = @get_headers($imageUrl, 1); // it gives all header values .
       // for image size, we use **Content-Length** for size.
       $sizeInKB = round($file_headers['Content-Length'] / 1024, 2));           
       return $sizeInKB;
   } else {
       return 0;
   }
}
$imageSize =checkImageSize($imageUrl);
if($imageSize<=$conditionalSize){
     // upload code
} else {
     // error msg
}
Mohit Sidoliya
  • 164
  • 1
  • 6