Glenn
Glenn

Reputation: 35

How do I get my php file to read these images correctly?

I've created this to get the size of several images I have in a directory....

$image = file_get_contents("$fileLocation");
$source = imagecreatefromstring($image);

$width = imagesx($source);
$height = imagesy($source);
echo "   width : ", $width, " $nbsp; height : ", $height, 

When it gets to an image with a name that has a space in the name, it gives me an error....

Warning: imagecreatefromstring(): Data is not in a recognized format in /setupdb.php on line 23

Warning: imagesx() expects parameter 1 to be resource, boolean given in /setupdb.php on line 25

Warning: imagesy() expects parameter 1 to be resource, boolean given in /setupdb.php on line 26

What do I need to add to get it to read them better?

Upvotes: 2

Views: 66

Answers (1)

HelgeB
HelgeB

Reputation: 262

The error is not about spaces in the filename but about unrecognized image formats. Filenames with spaces for supported image formats will work without errors or warnings (Except you are not reading them correctly and complete into the variable $fileLocation but that part is missing in your code!). You can check the supported image formats of your php installation by running var_dump(gd_info());.

I would suggest to change your code like this to identify all the files that are not supported images in your directory:

$image = file_get_contents("$fileLocation");
$source = imagecreatefromstring($image);

if ($source){
    $width = imagesx($source);
    $height = imagesy($source);
    echo "width : " . $width . " height : ". $height;
} else {
    echo $fileLocation . " is not a recognized image format";
}

Upvotes: 1

Related Questions