'imageSX and imageSY VS getimagesize

I need to receive the width and height of an image.

What kind of function do I have to use?

Will imageSX(Y) be quicker than getimagesize()?



Solution 1:[1]

Short Answer:

getimagesize ( string $filename [, array &$imageinfo ] ) should logically be faster.

Long Answer:

getimagesize() gets size, image all at once with 1 single file read, meaning the cost of the function is just 1 file read with some computation, while both imagesx() and imagesy() are 2 individual calls, meaning when whoever code these functions should consider reading the image file and measure the size separately. Hence in your case, if you intend to get both width and height, using imagesx() imagesy() would likely cost you 2 file reads. One thing for sure is that one programmer cannot be sure about the function be called once or more, so what he/she can do is to read the image every time the function is called. With disk I/O being one of the high-cost items in runtime, 2 times is definitely slower than once.

From a memory perspective, both functions take the whole image to RAM so there's not much for the input, but for the output, an array from getimagesize() is definitely larger than 2 int from imagesx() imagesy(). Just in case someone would care about instantaneous memory usage...

Solution 2:[2]

You can use imagesx() if you already have an image resource as defined in their docs.

If you are getting the image from a file, you should consider using imagecreatefromstring() using the image file's data string, as in their docs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chiu Chun Sun
Solution 2 Kyle Emmanuel