resize($image, $width, $height, $method, $extension); } catch (\Exception $e) { http_response_code(400); } exit; } protected function resize($image, $width, $height, $method, $extension) { $resizeMethod = (isset(ThumbGenerator::$aliasCallback[$method])) ? ThumbGenerator::$aliasCallback[$method] : 'resize'; $thumbGenerator = new ThumbGenerator(); $filename = $image . '.' . $extension; $info = pathinfo($filename); if (!ThumbGenerator::getPathAllowed($info['dirname'])) { throw new \Exception('Path is not allowed "' . $info['dirname'] . '"'); } if (!ThumbGenerator::getExtAllowed($info['extension'])) { throw new \Exception('Extension is not allowed "' . $info['extension'] . '"'); } $webPath = Yii::getAlias('@webroot'); $src = $webPath . '/' . $filename; if (!file_exists($src)) { $info = pathinfo(ThumbGenerator::NO_IMAGE_BASIC); $extension = $info['extension']; $image = $info['filename']; $src = $webPath . '/' . ThumbGenerator::NO_IMAGE_BASIC; } $dst = Yii::getAlias(ThumbGenerator::FS_THUMBNAIL_ALIAS) . "/{$image}_{$width}x{$height}{$method}.{$extension}"; if (!file_exists($dst)) { $dstDir = dirname($dst); if (!file_exists($dstDir)) { mkdir($dstDir, 0777, true); } $thumbGenerator->$resizeMethod($src, $dst, ['width' => $width, 'height' => $height]); } $params = getimagesize($src); header('Content-Type: ' . $params['mime']); header('Content-Length: ' . filesize($dst)); if (ob_get_length()) { ob_clean(); flush(); } readfile($dst); exit; } }