'ar', 'cropCenter' => 'cc' ]; public static $aliasCallback = [ 'ar' => 'adaptiveResize', 'cc' => 'cropFromCenter', ]; // список каталогів з яких дозволено ресайз (включаючи підпапки) (with trailing "/") public static $allowedSrcPaths = [ 'userfiles/', 'images/', ]; public static $allowedExtensions = ['jpg', 'jpeg', 'png', 'bmp', 'gif']; public static $thumbParams = ['width' => 50, 'height' => 50, 'method' => self::METHOD_INNER]; public function resize($srcName, $dstName, $options = array()) { $thumb = \PhpThumbFactory::create($srcName); $thumb->resize($options['width'], $options['height']); $thumb->save($dstName); } public function adaptiveResize($srcName, $dstName, $options = array()) { $thumb = \PhpThumbFactory::create($srcName); $thumb->adaptiveResize($options['width'], $options['height']); $thumb->save($dstName); } public function cropFromCenter($srcName, $dstName, $options = array()) { $thumb = \PhpThumbFactory::create($srcName); $thumb->cropFromCenter($options['width'], $options['height']); $thumb->save($dstName); } public static function getImage($srcName, $width, $height, $method = null) { $dstFile = self::prepareThumbName($srcName, $width, $height, $method); $dstUrl = self::getImageUrl($dstFile); $directory = Yii::getAlias(self::FS_FILE_ALIAS) . '/'; $dstName = $directory . $dstFile; if (!file_exists($dstName)) { if ($srcName) { $srcName = $directory . $srcName; } if (!file_exists($srcName)) { $srcName = Yii::getAlias(self::FS_BASE_IMAGE_ALIAS) . '/' . self::NO_IMAGE_BASIC; $dstFile = self::prepareThumbName(self::NO_IMAGE_BASIC, $width, $height, $method); $dstUrl = self::getImageUrl($dstFile); $dstName = $directory . $dstFile; } if (!file_exists($dstName)) { $thumb = \PhpThumbFactory::create($srcName); if ($method == 'inner') { $thumb->adaptiveResize($width, $height); } else if ($method == 'cropCenter') { $thumb->cropFromCenter($width, $height); } else { $thumb->resize($width, $height); } $thumb->save($dstName); } } return $dstUrl; } /** * @param string $filename filename inside "web/userfiles" folder * @param int $width * @param int $height * @param null|string $method ("inner"|"cropCenter") * * @return string * @throws \yii\base\InvalidParamException */ public static function prepareThumbName($filename, $width, $height, $method = null) { if (null == ($filename = self::normalizeName($filename))) { $filename = self::NO_IMAGE_BASIC; } $info = pathinfo($filename); $methodAlias = ($method && isset(self::$methodAlias[$method])) ? self::$methodAlias[$method] : ''; return (empty($info['dirname']) || '.' === $info['dirname'] ? 'userfiles' : $info['dirname']) . '/' . $info['filename'] . '_' . $width . 'x' . $height . $methodAlias . (empty($info['extension']) ? '' : '.' . $info['extension']); } public static function getThumbnailUrl($filename, $width, $height, $method = null) { $thumbName = self::prepareThumbName($filename, $width, $height, $method); return yii::getAlias(static::URL_THUMBNAIL_ALIAS) . '/' . $thumbName; } public static function getImageUrl($filename) { if (null == ($filename = self::normalizeName($filename))) { $filename = self::NO_IMAGE_BASIC; } return yii::getAlias('@web') . '/' . $filename; } /** * @param $filename * * @return null|string */ public static function normalizeName($filename) { if ($filename) { $info = pathinfo($filename); if (empty($info['dirname']) || '.' === $info['dirname']) { $filename = 'userfiles/' . $filename; $info['dirname'] = 'userfiles'; } if (self::getExtAllowed($info['extension']) && self::getPathAllowed($info['dirname'])) { return $filename; } } return null; } public static function getExtAllowed($ext) { return in_array($ext, self::$allowedExtensions); } /** * @param string $sourcePath relative file path ("userfiles", "images/", ...) * @return bool */ public static function getPathAllowed($sourcePath) { $sourcePath = ('/' === substr($sourcePath, -1)) ? $sourcePath : $sourcePath . '/'; foreach (self::$allowedSrcPaths as $allowedPath) { if (0 === strpos($sourcePath, $allowedPath)) { return true; } } return false; } }