Skip to content

Workaround for segmentation fault on PHP 7.3 + opcache #828

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

Merged
merged 3 commits into from
Apr 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions src/Gd/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,26 @@ private function saveOrOutput(Format $format, array $options, $filename = null)
$saveFunction = 'image' . $format->getID();
break;
}
$args = array(&$this->resource, $filename);
$args = array_merge(array(&$this->resource, $filename), $this->finalizeOptions($format, $options));

ErrorHandling::throwingRuntimeException(E_WARNING | E_NOTICE, function () use ($saveFunction, $args) {
if (call_user_func_array($saveFunction, $args) === false) {
throw new RuntimeException('Save operation failed');
}
});
}

/**
* @param \Imagine\Image\Format $format
* @param array $options
*
* @throws \Imagine\Exception\InvalidArgumentException
*
* @return array
*/
private function finalizeOptions(Format $format, array $options)
{
$result = array();
switch ($format->getID()) {
case Format::ID_AVIF:
// ranges from 0 (worst quality, smaller file) to 100 (best quality, larger file). If -1 is provided, the default value is used
Expand All @@ -662,12 +680,12 @@ private function saveOrOutput(Format $format, array $options, $filename = null)
$quality = max(0, min(100, $options['avif_quality']));
}
}
$args[] = $quality;
$args[] = $speed;
$result[] = $quality;
$result[] = $speed;
break;
case Format::ID_BMP:
if (isset($options['compressed'])) {
$args[] = (bool) $options['compressed'];
$result[] = (bool) $options['compressed'];
}
break;
case Format::ID_JPEG:
Expand All @@ -677,7 +695,7 @@ private function saveOrOutput(Format $format, array $options, $filename = null)
}
}
if (isset($options['jpeg_quality'])) {
$args[] = $options['jpeg_quality'];
$result[] = $options['jpeg_quality'];
}
break;
case Format::ID_PNG:
Expand All @@ -690,9 +708,9 @@ private function saveOrOutput(Format $format, array $options, $filename = null)
if ($options['png_compression_level'] < 0 || $options['png_compression_level'] > 9) {
throw new InvalidArgumentException('png_compression_level option should be an integer from 0 to 9');
}
$args[] = $options['png_compression_level'];
$result[] = $options['png_compression_level'];
} else {
$args[] = -1; // use default level
$result[] = -1; // use default level
}
if (!isset($options['png_compression_filter'])) {
if (isset($options['filters'])) {
Expand All @@ -703,13 +721,7 @@ private function saveOrOutput(Format $format, array $options, $filename = null)
if (~PNG_ALL_FILTERS & $options['png_compression_filter']) {
throw new InvalidArgumentException('png_compression_filter option should be a combination of the PNG_FILTER_XXX constants');
}
$args[] = $options['png_compression_filter'];
}
break;
case Format::ID_WBMP:
case Format::ID_XBM:
if (isset($options['foreground'])) {
$args[] = $options['foreground'];
$result[] = $options['png_compression_filter'];
}
break;
case Format::ID_WEBP:
Expand All @@ -722,16 +734,18 @@ private function saveOrOutput(Format $format, array $options, $filename = null)
if ($options['webp_quality'] < 0 || $options['webp_quality'] > 100) {
throw new InvalidArgumentException('webp_quality option should be an integer from 0 to 100');
}
$args[] = $options['webp_quality'];
$result[] = $options['webp_quality'];
}
break;
case Format::ID_XBM:
case Format::ID_WBMP:
if (isset($options['foreground'])) {
$result[] = $options['foreground'];
}
break;
}

ErrorHandling::throwingRuntimeException(E_WARNING | E_NOTICE, function () use ($saveFunction, $args) {
if (call_user_func_array($saveFunction, $args) === false) {
throw new RuntimeException('Save operation failed');
}
});
return $result;
}

/**
Expand Down