Search code examples
phpphp-gd

How to solve imagesx() imagexy() error


I am trying to get width and height of image by doing something like this.

$width  = imagesx("abc.jpg");
$height = imagesy("abc.jpg");

Even when I only have this two lines in the file with no linkage to any other files, I still got this error. The image is in server and I have no idea what is wrong. Anyone can help, please? Thank you.

Warning: imagesx(): supplied argument is not a valid Image resource in ..
Warning: imagesy(): supplied argument is not a valid Image resource in ..


Solution

  • You need to create an image resource, as imagesy() expect it as first parameter. That can be created with imagecreatefromjpeg() from your filename:

    $image = imagecreatefromjpeg("abc.jpg");
    if ($image) {
        $height = imagesy($image);
        imagedestroy($image);
    }
    

    Alternatively if you only need to get image width an height, you can make use of the getimagesize function:

    list($width, $height) = getimagesize("abc.jpg");
    

    It accepts the filename right ahead and does not require to create a gd image resource.