/**
  * Crop an image file to a square thumbnail.
  * The thumbnail will be saved with the suffix "<width>_thumb_square"
  * @param File $basefile the file to crop.
  * @param number $maxDimension limit maximum with/height.
  * @return string the thumbnail's url or null if an error occured.
  */
 public static function getSquareThumbnailUrlFromFile($basefile = null, $maxDimension = 1000)
 {
     if ($basefile === null) {
         return;
     }
     $suffix = $maxDimension . '_thumb_square';
     $originalFilename = $basefile->getStoredFilePath();
     $previewFilename = $basefile->getStoredFilePath($suffix);
     // already generated
     if (is_file($previewFilename)) {
         return $basefile->getUrl($suffix);
     }
     // Check file exists & has valid mime type
     if ($basefile->getMimeBaseType() != "image" || !is_file($originalFilename)) {
         return "";
     }
     $imageInfo = @getimagesize($originalFilename);
     // check valid image dimesions
     if (!isset($imageInfo[0]) || !isset($imageInfo[1])) {
         return "";
     }
     // Check if image type is supported
     if ($imageInfo[2] != IMAGETYPE_PNG && $imageInfo[2] != IMAGETYPE_JPEG && $imageInfo[2] != IMAGETYPE_GIF) {
         return "";
     }
     $dim = min($imageInfo[0], $imageInfo[1], $maxDimension);
     ImageConverter::Resize($originalFilename, $previewFilename, array('mode' => 'force', 'width' => $dim, 'height' => $dim));
     return $basefile->getUrl($suffix);
 }
Example #2
0
 /**
  * Handles a single upload by given CUploadedFile and returns an array
  * of informations.
  *
  * The 'error' attribute of the array, indicates there was an error.
  *
  * Informations on error:
  *       - error: true
  *       - errorMessage: some message
  *       - name: name of the file
  *       - size: file size
  *
  * Informations on success:
  *      - error: false
  *      - name: name of the uploaded file
  *      - size: file size
  *      - guid: of the file
  *      - url: url to the file
  *      - thumbnailUrl: url to the thumbnail if exists
  *
  * @param type $cFile
  * @return Array Informations about the uploaded file
  */
 protected function handleFileUpload($cFile, $object = null, $publicAccess = false)
 {
     $output = array();
     $file = new File();
     $file->setUploadedFile($cFile);
     $file->public_access = $publicAccess;
     if ($object != null) {
         $file->object_id = $object->getPrimaryKey();
         $file->object_model = get_class($object);
     }
     if ($file->validate() && $file->save()) {
         $output['error'] = false;
         $output['guid'] = $file->guid;
         $output['name'] = $file->file_name;
         $output['title'] = $file->title;
         $output['size'] = $file->size;
         $output['mimeIcon'] = HHtml::getMimeIconClassByExtension($file->getExtension());
         $output['mimeBaseType'] = $file->getMimeBaseType();
         $output['mimeSubType'] = $file->getMimeSubType();
         $output['url'] = $file->getUrl("", false);
         $output['thumbnailUrl'] = $file->getPreviewImageUrl(200, 200);
     } else {
         $output['error'] = true;
         $output['errors'] = $file->getErrors();
     }
     $output['name'] = $file->file_name;
     $output['size'] = $file->size;
     $output['deleteUrl'] = "";
     $output['deleteType'] = "";
     $output['thumbnailUrl'] = "";
     return $output;
 }