marc1706 / fast-image-size Goto Github PK
View Code? Open in Web Editor NEWfast-image-size is a PHP library that does almost everything PHP's getimagesize() does but without the large overhead of downloading the complete file.
License: MIT License
fast-image-size is a PHP library that does almost everything PHP's getimagesize() does but without the large overhead of downloading the complete file.
License: MIT License
Larger TIFF files can have the size info further back than currently expected header size. As a result of that, no valid size info is found and this PHP debug warning is generated:
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/marc1706/fast-image-size/lib/Type/TypeTif.php on line 80: unpack(): Type v: not enough input, need 2, have 0
It appears that the loaded images remain in memory even when the FastImageSize
object is unset.
PHP 8.1.13
Zend Engine v4.1.13
Zend OPcache v8.1.13
Loaded extensions:
zend_extension=opcache.so
extension=pdo.so
extension=pdo_pgsql.so
extension=pgsql.so
extension=xml.so
extension=calendar.so
extension=ctype.so
extension=curl.so
extension=dom.so
extension=exif.so
extension=ffi.so
extension=fileinfo.so
extension=ftp.so
extension=gd.so
extension=gettext.so
extension=iconv.so
extension=imagick.so
extension=intl.so
extension=mbstring.so
extension=phar.so
extension=posix.so
extension=readline.so
extension=shmop.so
extension=simplexml.so
extension=sockets.so
extension=sysvmsg.so
extension=sysvsem.so
extension=sysvshm.so
extension=tokenizer.so
extension=xmlreader.so
extension=xmlwriter.so
extension=xsl.so
extension=zip.so
Code example that demonstrates the leak:
for ($i = 0; $i < ENOUGH_TO_EXCEED_RAM_LIMIT; $i++) {
$mem = memory_get_usage();
$FastImageSize = new \FastImageSize\FastImageSize();
$path = "path/to/image.jpg";
$imageSize = $FastImageSize->getImageSize($filePath);
unset($FastImageSize);
unset($imageSize);
echo "leak: " . memory_get_usage() - $mem . " bytes<br>";
}
This code terminates with the following fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 790528 bytes) in /var/www/vendor/marc1706/fast-image-size/lib/FastImageSize.php on line 189
I'm running into issues parsing image data when it's on localhost.
$FastImageSize = new \FastImageSize\FastImageSize();
$imageSize = $FastImageSize->getImageSize('http://0.0.0.0:8000/files/1461.png');
echo $imageSize;
The image can be loaded in the browser via that path, but for some reason it can't be found by fast-image-size.
Add builds for PHP 7.3 and remove the support for HHVM. HHVM will stop supporting PHP so there is no point in further supporting it.
Travis CI should be replaced by running the CI on GitHub Actions.
This package has a hidden dependency that is actually not needed.
mbstring functions are used but seems to be not needed at all.
They occured only at this line.
fast-image-size/lib/FastImageSize.php
Line 81 in 5f7e837
Replacing this line by something like this would remove dependency to mbstring
$className = '\FastImageSize\Type\Type' . ucfirst($imageType);
Since the content of $imageType is defined from class parameter, there should be no need to rely on mbstring functions.
Either that or update composer.json to require ext-mbstring module, so that at least mbstring dependency is not hidden.
Using the library as part of phpBB 3.2 on PHP 5.6.
One of the users complained that their files could not be uploaded.
I investigated the issue and found that FastImage Size apparently fails to get image size for their photos.
Interestingly, the core PHP function, getimagesize(), works fine with these files.
Links to files:
http://filedistr.ru/2018/IMG_20180720_103159.jpg
http://filedistr.ru/2018/IMG_20180724_181130.jpg
I fixed the issue on my site by adding the following lines to function TypeJpeg::getSize()
After:
// Look through file for SOF marker
$size = $this->getSizeInfo();
Added:
if (empty($size)) {
$info = getimagesize($filename);
if (!empty($info) && is_array($info)) {
$size = array(
'width' => $info[0],
'height' => $info[1],
);
}
}
This is not necessarily the best way to fix it, this is just so that you have an idea.
This should take care of using the correct method for retrieving remote files (e.g. curl vs. fopen functions) as well as enabling the use of seekable streams in types.
I'm using FastImageSize in phpBB 3.2.0 !
When uploading TIFF images in a post I get an error for failing to get the dimensions of the file. However when I change the TIF_HEADER_SIZE
number e.g. to 512000 (in lib\Type\TypeTif.php
) then it works.
When you pass a known file type to the method, it uses the file extension instead of the type passed as the parameter.
This is a problem with uploaded files in windows, because when they are uploaded, they have the extension .tmp, and the method uses this extension as type.
Error is reported @ phpBB: https://tracker.phpbb.com/browse/PHPBB3-16742
But someone told me to report it here...
I'm receiving this error in a topic with an attachment.
It's strange, because it's only that 1 topic that gives this error...
All others with attachments are fine...
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/marc1706/fast-image-size/lib/FastImageSize.php on line 189: file_get_contents(https://www.phpbbnl.be/download/file.php?id=15&mode=view): Failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden [phpBB Debug] PHP Warning: in file [ROOT]/vendor/marc1706/fast-image-size/lib/FastImageSize.php on line 189: file_get_contents(https://www.phpbbnl.be/download/file.php?id=16&mode=view): Failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
You can see the error here: https://www.phpbbnl.be/viewtopic.php?f=5&t=8
I use PHP 8.0.3
The error is gone with PHP 7.4
=====
PHP: 8.0.3 - FPM Served By nginx
MySQL(i) 10.5.9-MariaDB
Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz (6 core(s))
CentOS Linux 7.9.2009 (Core)
Plesk Obsidian v18.0.34
If we have an url image name with space not encoded, for es.
$FastImageSize->getImageSize('https://example.com/some random image.jpg');
the function will not work because the file_get_contents do not accept url not encoded.
As a fix, I propose to automatically encode the filename in the getImage function.
$filename = urldecode($filename);
$filename = dirname($filename) . '/'. rawurlencode(basename($filename));
$this->data = @file_get_contents($filename, null, null, $offset, $length);
See issue descibed here:
Roave/BackwardCompatibilityCheck#336
Also see the recommended way of handling autoloading for tests:
https://getcomposer.org/doc/04-schema.md#autoload-dev
The EXIF IFD image size is unfortunately not a reliable source of the image size. Therefore, it should be replaced by a solid way of handling both images with and without exif data.
Some images saved with newer versions of Photoshop no longer carry the JFIF data but only the EXIF data including EXIF IFD. As a result of that, image size info needs to be extracted from EXIF instead of the JFIF header.
I have some jpeg images not working with library.One of the images I'm using is attached to the issue.
The issue seems to be :
$this->data = $this->fastImageSize->getImage($filename, 0, self::JPEG_MAX_HEADER_SIZE, false);
on file
https://github.com/marc1706/fast-image-size/blame/master/lib/Type/TypeJpeg.php
it returns an empty object even if the image is a jpeg.
I already tried to change extension of file from "jpg" to "jpeg". I tried aslo to expand max header size, but with no results.
The latest fixes, including the casing issue I previously reported (thank you for fixing that!), are not in a tagged release. The AMP library, which depends on yours, pulls 1.*
: https://github.com/Lullabot/amp-library/blob/bdf10c4470e7503d3bd187ba3afee7ecea008a7f/composer.json#L17
Please tag a newer 1.*
release so that composer will pull in the latest fixes. Thanks again!
The WebP seems to become a good alternative to JPEG or PNG, so even though not all browsers support it yet there are some sites out there that can already send them to the browser (mostly by looking at the Accept-Header).
I assume that the support is going to grow more and more so maybe you can add support for it?
Some files still use variables that are not camelCase. This should be changed.
Jpeg type calls strlen() in for loop.
At this line https://github.com/marc1706/fast-image-size/blob/master/lib/FastImageSize.php#L81 ucfirst function is used to generate class name. ico, iff word cant be converted correcly by ucfirst, and it cause a class not found error. They are converted to ico, iff (as same name).
mb_convert_case function works fine.
May be we should use like this.
mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
I am going to send a PR. Do you have any comment for this issue.
https://github.com/Lullabot/amp-library package uses your package. 3-5 monts ago, it used to work without any problem.
Thanks.
EXIF data might contain a thumbnail and more info. However, under certain circumstance, it's possible that the EXIF part contains the SOF0 marker. Therefore, we need to skip over the length of the EXIF when determining image size.
http://stackoverflow.com/questions/2564393/jpg-segment-length-encoding
In this directory: https://github.com/marc1706/fast-image-size/tree/bafc33cc5315e5636674d3639bda98a07d33e7b9/tests/fixture
There are files JPG
and PNG
as well as jpg
and png
. This is an issue for case-insensitive systems like Windows and OS X/macOS (by default). I noticed when when using unison
to sync files between a VM and a host machine. Unison kept reporting skipped files:
skipped: vendor/marc1706/fast-image-size/tests/fixture/jpg (Two or more files on a case-sensitive system have names identical except for case. They cannot be synchronized to a case-insensitive file system. (File 'vendor/marc1706/fast-image-size/tests/fixture/jpg'))
skipped: vendor/marc1706/fast-image-size/tests/fixture/png (Two or more files on a case-sensitive system have names identical except for case. They cannot be synchronized to a case-insensitive file system. (File 'vendor/marc1706/fast-image-size/tests/fixture/png'))
In checking my host machine files, I noticed that I didn't have any files jpg
or png
at all. On the VM, where the repo was initially checked out/installed by composer, I see jpg
and JPG
. The VM is case-sensitive, so it works fine. The host machine is not.
Is there a way you can change this so that you don't have file name conflicts on case-insensitive file systems?
Travis supports testing on both PHP 7.1 and nightly builds.
I get this error:
PHP Notice: Uninitialized string offset: 25214 in ....lib/Type/TypeJpeg.php on line 128
I use this code:
$FastImageSize = new \FastImageSize\FastImageSize();
$imageSize = $FastImageSize->getImageSize('http://www.techspot.com/articles-info/1121/images/P34WS-12.jpg');
print_r($imageSize);
The same error you can get with these images:
http://www.techspot.com/articles-info/1121/images/P34WS-4.jpg
http://www.techspot.com/articles-info/1121/images/P34WS-17.jpg
Could it be, that the function @file_get_contents has a problem with some redirected links?
That's a problem in phpBB, if a link to an image is redirected from http to https. The server answers with a 301 or 302, i.e. permanently redirected and after this the image data are transfered.
In this example https://www.stummiforum.de/viewtopic.php?f=14&t=161634&start=25#p1899025 the image link is redirected to https if you call it with http. This server strangely answers first with "Please enable Javascript".
The result of @file_get_contents are the first answer of the server, which isn't the image. So the library tries to find a image size in this data which results in an error:
"It was not possible to determine the dimensions of the image. Please verify that the URL you entered is correct."
I think, there is a javascript for redirect and @file_get_contents get this script result and not an image.
Can this problem be confirmed?
Hello.
I've faced an issue when image size can't be obtained for images with wrong extension (for example jpeg image format with PNG extension). Yes, I know it is not normal to have image with wrong extension, but sometimes it happens.
I found this issue parsing "Lullabot\AMP" issue (laravel). It doesn't convert "img" tag to "amp-img" when image size is undefined. So I have a solution, but I think there could be better one.
So my simple solution is for class "FastImageSize", function "getImageSize" you can add this to end of else statement:
if(!(sizeof($this->size) > 1)) { $this->data = ''; $this->getImagesizeUnknownType($file); }
Thanks.
I ran into a circumstance where a file URL had an all caps file extension i.e. '.JPG' instead of expected '.jpg', but the regex in FastImageSize::getImageSize() only matches lower-case. I propose matching upper-case letters as well, then passing the resulting matched extension through 'strtolower()' before sending to FastImageSize::getImageSizeByExtension().
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.