Example #1
0
 /**
  * Ресайз картинки уже добавленной в webdav.
  *
  * @param string $to           Куда сохранить и с каким именем
  * @param int    $newWidth     Ширина в пикселах
  * @param int    $newHeight    Высота в пикселах
  * @param string $option       Тип ресайза: portrait - приоритет высоты
  *                             landscape - приоритет ширины
  *                             crop - вырезание из центра картинки
  *                             auto - автоматический выбор
  *                             cropthumbnail - уменьшить и верезать по центру
  * @param bool   $savePngAlpha Сохранить альфа канал для PNG файлов
  *
  * @return bool true - рейсайз сделан, false - не сделан
  */
 public function resizeImage($to, $newWidth, $newHeight, $option = 'auto', $savePngAlpha = false, $table = null)
 {
     if (!in_array($this->image_size['type'], array(1, 2, 3))) {
         // Недопустимый формат файла
         return false;
     }
     $tmp_name = uniqid();
     $file = "/tmp/{$tmp_name}";
     file_put_contents($file, file_get_contents(WDCPREFIX_LOCAL . '/' . $this->path . $this->name));
     /*switch($this->image_size['type']) {
                 case 1:
                     $img = imagecreatefromgif($file);
                     break;
                 case 2:
                     $img = imagecreatefromjpeg($file);
                     break;
                 case 3:
                     $img = imagecreatefrompng($file);
                     break;
                 default:
                     $img = false;
                     break;
             }
     
             if(!$img) {
                 // Ошибка открытия файла
                 return false;
             }
     
             // Расчитываем новые значения ширины и высоты картинки
             switch ($option) {
                 case 'portrait':
                     $optimalWidth = $this->_getImageSizeByFixedHeight($newHeight);
                     $optimalHeight= $newHeight;
                     break;
                 case 'landscape':
                     $optimalWidth = $newWidth;
                     $optimalHeight= $this->_getImageSizeByFixedWidth($newWidth);
                     break;
                 case 'auto':
                     $optionArray = $this->_getImageSizeByAuto($newWidth, $newHeight);
                     $optimalWidth = $optionArray['optimalWidth'];
                     $optimalHeight = $optionArray['optimalHeight'];
                     break;
                 case 'crop':
                     $optionArray = $this->_getImageOptimalCrop($newWidth, $newHeight);
                     $optimalWidth = $optionArray['optimalWidth'];
                     $optimalHeight = $optionArray['optimalHeight'];
                     break;
             }
     
             if ($this->image_size['type'] && $savePngAlpha) {
                 $imageResized = $this->imageResizeAlpha($img, $optimalWidth, $optimalHeight);
             } else {
                 $imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
                 imagecopyresampled($imageResized, $img, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->image_size['width'], $this->image_size['height']);
             }
     
             if ($option == 'crop') {
                 $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
                 $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
                 $crop = $imageResized;
                 $imageResized = imagecreatetruecolor($newWidth , $newHeight);
                 imagecopyresampled($imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight);
             }
     
             // Сохраняем измененную картинку
             $file = $file.'_resized';
             switch($this->image_size['type']) {
                 case 1:
                     $ext = 'gif';
                     imagegif($imageResized, $file);
                     break;
                 case 2:
                     $ext = 'jpg';
                     imagejpeg($imageResized, $file, $this->quality);
                     break;
                 case 3:
                     $ext = 'png';
                     $scaleQuality = round(($this->quality/100) * 9);
                     $invertScaleQuality = 9 - $scaleQuality;
                     imagepng($imageResized, $file, $invertScaleQuality);
                     break;
             }*/
     switch ($option) {
         case 'portrait':
             $optimalWidth = $this->_getImageSizeByFixedHeight($newHeight);
             $optimalHeight = $newHeight;
             break;
         case 'landscape':
             $optimalWidth = $newWidth;
             $optimalHeight = $this->_getImageSizeByFixedWidth($newWidth);
             break;
         case 'auto':
             $optionArray = $this->_getImageSizeByAuto($newWidth, $newHeight);
             $optimalWidth = $optionArray['optimalWidth'];
             $optimalHeight = $optionArray['optimalHeight'];
             break;
         case 'crop':
             $optionArray = $this->_getImageOptimalCrop($newWidth, $newHeight);
             $optimalWidth = $optionArray['optimalWidth'];
             $optimalHeight = $optionArray['optimalHeight'];
             break;
     }
     $imagick = new Imagick($file);
     if ($option == 'cropthumbnail') {
         $imagick->cropThumbnailImage($newWidth, $newHeight);
     } elseif ($option == 'crop') {
         $cropStartX = $optimalWidth / 2 - $newWidth / 2;
         $cropStartY = $optimalHeight / 2 - $newHeight / 2;
         $imagick->cropImage($newWidth, $newHeight, $cropStartX, $cropStartY);
     } else {
         if ($this->image_size['type'] == 1) {
             // GIF пытаемся сохранить анимацию при уменьшении изображения
             $imagick = $imagick->coalesceImages();
             do {
                 $imagick->scaleImage($optimalWidth, $optimalHeight, true);
             } while ($imagick->nextImage());
             //$imagick->optimizeImageLayers();
         } else {
             $imagick->scaleImage($optimalWidth, $optimalHeight, true);
         }
     }
     //освобождаем память и сохраняем
     $imagick = $imagick->deconstructImages();
     $imagick->writeImages($file, true);
     $tFile = new self();
     if ($table) {
         $tFile->table = $table;
     } else {
         $tFile->table = $this->table;
     }
     $tFile->tmp_name = $file;
     $tFile->original_name = $this->original_name;
     $tFile->size = filesize($file);
     $tFile->path = dirname($to) . '/';
     $tFile->name = basename($to);
     $tFile->_upload($file);
     return $tFile;
 }