-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Webp full support #711
Webp full support #711
Changes from 1 commit
7556450
4d7bd09
45c50a7
8516277
1214807
193f333
bbff90d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -87,9 +87,14 @@ public function open($path) | |||||
$path = $loader->getPath(); | ||||||
|
||||||
$data = $loader->getData(); | ||||||
$resource = $this->withoutExceptionHandlers(function () use (&$data) { | ||||||
return @imagecreatefromstring($data); | ||||||
}); | ||||||
|
||||||
if($this->isWebp($data)) { | ||||||
$resource = $this->loadWebp($data); | ||||||
} else { | ||||||
$resource = $this->withoutExceptionHandlers(function () use (&$data) { | ||||||
return @imagecreatefromstring($data); | ||||||
}); | ||||||
} | ||||||
|
||||||
if (!is_resource($resource)) { | ||||||
throw new RuntimeException(sprintf('Unable to open image %s', $path)); | ||||||
|
@@ -208,9 +213,14 @@ private function requireGdVersion($version) | |||||
*/ | ||||||
private function doLoad($string, MetadataBag $metadata) | ||||||
{ | ||||||
$resource = $this->withoutExceptionHandlers(function () use (&$string) { | ||||||
return @imagecreatefromstring($string); | ||||||
}); | ||||||
|
||||||
if($this->isWebp($string)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, you have to choose whether you want to support version that is 10 years old and unsupported since last 5 years or to use the image standard from 2010, early adopted by facebook & google in 2013 :) I'm all up for the latter but I can work on my fork so it's up to you. Do you think that function check is good enough? Shouldn't we post some information that with the newer version of PHP this will be working at least? (Sorry for the other comment, wrong account). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd just do the function exists check. We can add a note to the changelog / release notes that webp support requires PHP 5.4+. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've pushed the new commit with your suggestion in it :) |
||||||
$resource = $this->loadWebp($string); | ||||||
} else { | ||||||
$resource = $this->withoutExceptionHandlers(function () use (&$string) { | ||||||
return @imagecreatefromstring($string); | ||||||
}); | ||||||
} | ||||||
|
||||||
if (!is_resource($resource)) { | ||||||
throw new RuntimeException('An image could not be created from the given input'); | ||||||
|
@@ -239,4 +249,19 @@ private function withoutExceptionHandlers($callback) | |||||
|
||||||
return $result; | ||||||
} | ||||||
|
||||||
private function isWebp(string $data) | ||||||
{ | ||||||
return strncmp( substr( $data, 8, 7 ), "WEBPVP8", 7 ) === 0; | ||||||
} | ||||||
|
||||||
private function loadWebp(string $data) | ||||||
{ | ||||||
$tmpfile = tempnam(sys_get_temp_dir(), 'imaginewebp_'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if that's necessery. Why would we want to configure the temp directory for just loading the file? Also if we want to delete the file we should probably do this somewhere else - we give the resource back and we cannot remove file that is currently opened as resources AFAIK. Perhaps on destructor of the class? To be honest - if it's in /tmp dir then filesystem will clear it eventually so it seemed like a "safe" choice :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've seen crappy hosting providers where the directory returned from About deleting the file: PHP loads the file content in memory, so the file can be safely deleted. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, will fix that in next commit after we resolve the second issue. |
||||||
file_put_contents($tmpfile, $data); | ||||||
$resource = $this->withoutExceptionHandlers(function () use ($tmpfile) { | ||||||
return @imagecreatefromwebp($tmpfile); | ||||||
}); | ||||||
return $resource; | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imagecreatefromwebp
has been introduced in PHP 5.4, but Imagine supports PHP 5.3.2. so what about