Пример #1
0
 /**
  * Обработка создания превью картинки, сохранение в той же директории, что и оригинал с
  * постфиксом $postfix и регистрации превью, если она до этого не существовала
  * @param integer $w Ширина превью, если ноль, то ширина растягивается пропорционально высоте.
  * @param integer $h Высота превью, если ноль, то высота растягивается пропорционально широте.
  * @param string cropType - если не пустое, то картинка не растягивается, а вырезается под нужный размер. В этом случае нулей в ширине и высоте быть не может.
  * Возможные варианты: top|left(обрезается по верху, а если она растянута по ширине - то по центру)|right
  * @param string $postfix Постфикс, добавляется к имени файла
  * @param integer $quality Качество, в процентах
  * @param $resize Нужно ли масштабировать
  * @return File Превью
  */
 public function getPreview($w, $h, $postfix = '_p', $cropType = null, $quality = 80, $resize = false)
 {
     //Если не изображение
     if (!$this->getIsImage()) {
         return null;
     }
     if (!is_numeric($quality)) {
         $quality = 80;
     }
     //if ($f->getStatusProcess() == 1) return null;
     $root = Yii::getPathOfAlias('webroot') . DIRECTORY_SEPARATOR;
     $fp = $this->file_path;
     $fotobig = $root . $fp;
     if (empty($fp) || !file_exists($fotobig)) {
         return null;
     }
     // если размеры меньше, то не увеличиваем
     $img = new ImageUtils();
     $a = $img->info($fotobig);
     if ($w > 0 && $a['width'] <= $w && empty($cropType)) {
         $w = 0;
     }
     if ($h > 0 && $a['height'] <= $h && empty($cropType)) {
         $h = 0;
     }
     $previewAfter = HFile::addPostfix($fotobig, $postfix);
     $need2register = !file_exists($previewAfter);
     if ($w == 0) {
         $w = null;
     }
     if ($h == 0) {
         $h = null;
     }
     $img = new ImageUtils();
     // нужно ли проверять размер превью
     $needResize = false;
     if (file_exists($previewAfter)) {
         if ($resize) {
             $prevInfo = $img->info($previewAfter);
             $rh = $h;
             $rw = $w;
             //Вписывает изображение в прямоугольник $w x $h
             if ($resize === 'auto') {
                 if ($w > 0 && $h > 0) {
                     $kw = $w / $a['width'];
                     $kh = $h / $a['height'];
                     $rh = round($kw * $a['height']);
                     $rw = round($kw * $a['width']);
                     if ($rh > $h) {
                         $rh = round($kh * $a['height']);
                         $rw = round($kh * $a['width']);
                         $w = null;
                     } else {
                         $h = null;
                     }
                 }
             }
             $needResize = $rw != null && $prevInfo['width'] != $rw || $rh != null && $prevInfo['height'] != $rh;
         }
     }
     // Создание превью
     if (!file_exists($previewAfter) || $needResize) {
         // 0-0
         if ($h == null && $w == null) {
             return $this;
         } else {
             //if (DA_CONTROL_PROCESS_FILE) $f->updateStatusProcess(1);
             if (!$img->open($fotobig)) {
                 return null;
             }
             // 1-1
             if (!empty($cropType)) {
                 $img->crop($w, $h, true, $cropType);
                 // 1-0
             } else {
                 if ($h == null) {
                     $k = $w / $img->width;
                     $h = round($img->height * $k);
                     $img->resize($w, $h);
                     // 0-1
                 } else {
                     if ($w == null) {
                         $k = $h / $img->height;
                         $w = round($img->width * $k);
                         $img->resize($w, $h);
                         // 1-1
                     } else {
                         $img->crop($w, $h, true, 'center');
                     }
                 }
             }
             $img->save($previewAfter, $quality);
             //if (DA_CONTROL_PROCESS_FILE) $f->updateStatusProcess(0);
         }
     }
     //if ($object == null) $object = $f->getIdObjectOwner();
     //if ($id == null) $id = $f->getIdInstanceOwner();
     $previewFile = new File();
     $previewFile->id_object = $this->id_object;
     $previewFile->id_instance = $this->id_instance;
     $previewFile->id_file_type = self::FILE_IMAGE;
     $previewFile->id_tmp = $this->id_tmp;
     $previewFile->id_parameter = $this->id_parameter;
     $previewFile->id_property = $this->id_property;
     $previewFile->file_path = mb_substr($previewAfter, mb_strlen($root));
     // регистрация
     if ($this->id_object != null && $previewAfter != null && $need2register && file_exists($previewAfter)) {
         $previewFile->id_parent_file = $this->id_file;
         $previewFile->insert();
         chmod($previewAfter, 0777);
     }
     return $previewFile;
 }