/**
* @param string $file Полный путь к исходному файлу в файловой системе
* @param string $uploadDir Подпапка в папке с миниатюрами куда надо поместить изображение
* @param float $width Ширина изображения. Если не указана - будет вычислена из высоты
* @param float $height Высота изображения. Если не указана - будет вычислена из ширины
* @param array $options
* @return string
* @throws CException
*/
public function thumbnail($file, $uploadDir, $width = 0, $height = 0, array $options = ['jpeg_quality' => 90, 'png_compression_level' => 8])
{
if (!$width && !$height) {
throw new CException("Incorrect width/height");
}
$name = $width . 'x' . $height . '_' . basename($file);
$uploadPath = $this->getBasePath() . DIRECTORY_SEPARATOR . $uploadDir;
$thumbFile = $uploadPath . DIRECTORY_SEPARATOR . $name;
if (!file_exists($thumbFile)) {
if (false === YFile::checkPath($uploadPath)) {
throw new CException(Yii::t('YupeModule.yupe', 'Directory "{dir}" is not acceptable for write!', ['{dir}' => $uploadPath]));
}
$img = Imagine::getImagine()->open($file);
$originalWidth = $img->getSize()->getWidth();
$originalHeight = $img->getSize()->getHeight();
if (!$width) {
$width = $height / $originalHeight * $originalWidth;
}
if (!$height) {
$height = $width / $originalWidth * $originalHeight;
}
if ($width / $originalWidth > $height / $originalHeight) {
$box = new Box($width, $originalHeight * $width / $originalWidth);
} else {
$box = new Box($originalWidth * $height / $originalHeight, $height);
}
$img->resize($box)->crop(new Point(max(0, round(($box->getWidth() - $width) / 2)), max(0, round(($box->getHeight() - $height) / 2))), new Box($width, $height))->save($thumbFile, $options);
}
$url = $this->getBaseUrl() . '/' . $uploadDir . '/' . $name;
return $url;
}