예제 #1
0
 /**
  * Hydrate model with the provided $data
  * from file upload.
  *
  * @param  array $data
  * @param  ImageModel $object
  * @return object
  */
 public function hydrate(array $data, $object)
 {
     $size = getImageSize($data['tmp_name']);
     $object->setFileName($data['name'])->setType($data['type'])->setSize($data['size'])->setTempName($data['tmp_name'])->setWidth($size[0])->setHeight($size[1])->setMimeType($size[2])->setError($data['error']);
     return $object;
 }
 /**
  * @param ImageModel $model
  * @return ImageModel
  * @throws UthandoFileManagerException
  */
 public function resizeImage(ImageModel $model) : ImageModel
 {
     $options = $this->getOptions();
     $image = $model->getTempName();
     $oldX = $model->getWidth();
     $oldY = $model->getHeight();
     switch ($model->getMimeType()) {
         case IMAGETYPE_JPEG:
             $imageIn = imageCreateFromJpeg($image);
             break;
         case IMAGETYPE_PNG:
             $imageIn = imageCreateFromPng($image);
             break;
         case IMAGETYPE_GIF:
             $imageIn = imageCreateFromGif($image);
             break;
         default:
             throw new UthandoFileManagerException($this->error(self::MIME_NOT_SUPPORTED, $model->getMimeType()));
     }
     if ($model->getWidth() > $options->getMaxWidth()) {
         $model->setHeight(round($options->getMaxWidth() * $model->getHeight() / $model->getWidth()));
         $model->setWidth($options->getMaxWidth());
     }
     if ($model->getHeight() > $options->getMaxHeight()) {
         $model->setWidth(round($model->getWidth() * $options->getMaxHeight() / $model->getHeight()));
         $model->setHeight($options->getMaxHeight());
     }
     $imageOut = imageCreateTrueColor($model->getWidth(), $model->getHeight());
     imageCopyResampled($imageOut, $imageIn, 0, 0, 0, 0, $model->getWidth(), $model->getHeight(), $oldX, $oldY);
     switch ($model->getMimeType()) {
         case IMAGETYPE_JPEG:
             imageJpeg($imageOut, $image);
             break;
         case IMAGETYPE_PNG:
             imagePng($imageOut, $image);
             break;
         case IMAGETYPE_GIF:
             imageGif($imageOut, $image);
             break;
     }
     imageDestroy($imageIn);
     imageDestroy($imageOut);
     return $model;
 }