: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
return exif_imagetype($filename);
public static function isPNG8($path) {
public static function scaleImageWebp($group, $imageUrlOrPath, $options) {
$options = array_merge(array(
if (strpos($imageUrlOrPath, Filesystem::getBasePath()) === 0) {
$imageUrl = Url::pathToUri($imageUrlOrPath);
$imageUrl = ResourceTranslator::toUrl($imageUrlOrPath);
if (!extension_loaded('gd') || $options['mode'] === 'scale' && $options['scale'] <= 0) {
return Filesystem::pathToAbsoluteURL($imageUrl);
$options['quality'] = max(0, min(100, $options['quality']));
$originalImageUrl = $imageUrl;
if (substr($imageUrl, 0, 2) == '//') {
$imageUrl = parse_url(Url::getFullUri(), PHP_URL_SCHEME) . ':' . $imageUrl;
$imageUrl = Url::relativetoabsolute($imageUrl);
$imagePath = Filesystem::absoluteURLToPath($imageUrl);
$cache = new CacheImage($group);
if ($imagePath == $imageUrl) {
// The image is not local
if (!$options['remote']) {
return $originalImageUrl;
$pathInfo = pathinfo(parse_url($imageUrl, PHP_URL_PATH));
if (isset($pathInfo['extension'])) {
$extension = self::validateGDExtension($pathInfo['extension']);
$extension = self::checkMetaExtension($imageUrl, $extension);
if (!$extension || (strtolower($extension) === 'webp' && !function_exists('imagecreatefromwebp')) || !ini_get('allow_url_fopen')) {
return $originalImageUrl;
return ResourceTranslator::urlToResource(Filesystem::pathToAbsoluteURL($cache->makeCache('webp', array(
$imageType = @self::exif_imagetype($imagePath);
if (!function_exists('imagecreatefromwebp')) {
return $originalImageUrl;
return $originalImageUrl;
return ResourceTranslator::urlToResource(Filesystem::pathToAbsoluteURL($cache->makeCache('webp', array(
public static function _scaleRemoteImageWebp($targetFile, $extension, $imageUrl, $options) {
return self::_scaleImageWebp($targetFile, $extension, $imageUrl, $options);
public static function _scaleImageWebp($targetFile, $extension, $imagePath, $options) {
$options = array_merge(array(
$targetDir = dirname($targetFile);
if ($extension == 'png') {
$image = @imagecreatefrompng($imagePath);
if (!imageistruecolor($image)) {
imagepalettetotruecolor($image);
imagealphablending($image, true);
imagesavealpha($image, true);
} else if ($extension == 'jpg') {
$image = @imagecreatefromjpeg($imagePath);
if (function_exists("exif_read_data")) {
$exif = @exif_read_data($imagePath);
$rotated = self::getOrientation($exif, $image);
} else if ($extension == 'webp') {
//@TODO: should we need to care about rotation?
$image = @imagecreatefromwebp($imagePath);
if (!imageistruecolor($image)) {
imagepalettetotruecolor($image);
imagealphablending($image, true);
imagesavealpha($image, true);
$originalWidth = imagesx($image);
$originalHeight = imagesy($image);
switch ($options['mode']) {
$targetWidth = round($originalWidth * $options['scale']);
$targetHeight = round($originalHeight * $options['scale']);
$targetWidth = $options['width'];
$targetHeight = $options['height'];
if ((isset($rotated) && $rotated) || $originalWidth != $targetWidth || $originalHeight != $targetHeight) {
$newImage = imagecreatetruecolor($targetWidth, $targetHeight);
if ($extension == 'png') {
imagesavealpha($newImage, true);
imagealphablending($newImage, false);
$transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
imagefilledrectangle($image, 0, 0, $targetWidth, $targetHeight, $transparent);
list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = self::imageMode($targetWidth, $targetHeight, $originalWidth, $originalHeight, 'cover', $options['focusX'], $options['focusY']);
imagecopyresampled($newImage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
if (!Filesystem::existsFolder($targetDir)) {
Filesystem::createFolder($targetDir);
imagewebp($newImage, $targetFile, $options['quality']);
throw new Exception('Unable to scale image: ' . $imagePath);
public static function checkMetaExtension($imageUrl, $originalExtension) {
if (strpos($imageUrl, 'dst-jpg') !== false) {
} else if (strpos($imageUrl, 'dst-png') !== false) {
} else if (strpos($imageUrl, 'dst-webp') !== false) {
// not Instagram or Facebook url
return $originalExtension;