public function insertFileAction() { $file = new File(); $file->setDescription($this->app->request->post('description')); try { $file = $this->app->dataAccessorFactory->getDataAccessor(EntityNames::FILE)->insert($file); } catch (EntityNotInsertedException $enie) { ResponseFactory::createErrorJsonResponse($this->app, HttpStatusCodes::CONFLICT, $enie->getMessage()); return; } ResponseFactory::createJsonResponse($this->app, $file); }
/** * Creates a Thumbnail if file type is supported. * Supported types are: JPEG, PNG, GIF, WBMP, and GD2. * * @param File $fileObject FileObject to add properties * @param string $httpPathToMediaDir Http path to file * @param string $pathToMediaDirectory LOCAL Path to file (without backslash at the end) * @param string $fileName Name of the image * @param string $fileExtension Fileextension * @param integer $width Width of thumbnail, if omitted, size will be proportional to height * @param integer $heigt Height of thumbnail, if omitted, size will be proportional to width * * @throws ThumbnailCreationFailedException If thumbnail creation fails * @throws InvalidArgumentException If both, height and width, are omitted or the file format is not supported */ private static function createThumbnailWithGd(File &$fileObject, $httpPathToMediaDir, $pathToMediaDirectory, $fileName, $fileExtension, $width = null, $height = null) { if ($width === null && $height === null) { throw new InvalidArgumentException('Either width or height must be provided'); } if ($fileExtension == 'jpg' || $fileExtension == 'jpeg' && function_exists('imagecreatefromjpeg')) { $img = imagecreatefromjpeg(sprintf('%s/%s.%s', $pathToMediaDirectory, $fileName, $fileExtension)); } else { if ($fileExtension == 'png' && function_exists('imagecreatefrompng')) { $img = imagecreatefrompng(sprintf('%s/%s.%s', $pathToMediaDirectory, $fileName, $fileExtension)); } else { if ($fileExtension == 'gif' && function_exists('imagecreatefromgif')) { $img = imagecreatefromgif(sprintf('%s/%s.%s', $pathToMediaDirectory, $fileName, $fileExtension)); } else { throw new ThumbnailCreationFailedException('No thumbnail could be created for the file format'); } } } // image is corrupt, in the wrong file type or contains wrong data if ($img === false) { throw new ThumbnailCreationFailedException('No thumbnail could be created for the file format'); } $imgWidth = imagesx($img); $imgHeight = imagesy($img); $fileObject->setDimensions(sprintf('%sx%s', $imgWidth, $imgHeight)); // calculate thumbnail dimensions if ($width && !$height) { $newHeight = floor($imgHeight * ($width / $imgWidth)); $newWidth = $width; } else { if (!$width && $height) { $newHeight = $height; $newWidth = floor($imgWidth * ($height / $imgHeight)); } } // create a new temporary image $tmpImg = imagecreatetruecolor($newWidth, $newHeight); // check resource identifier if ($tmpImg === false) { throw new ThumbnailCreationFailedException('Thumbnail creation failed on temporary image'); } // copy and resize old image into new image $ret = imagecopyresized($tmpImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $imgWidth, $imgHeight); if ($ret === false) { throw new ThumbnailCreationFailedException('Thumbnail creation failed on resizing'); } // save thumbnail into a file, set image quality to 100 $ret = imagejpeg($tmpImg, sprintf('%s/%sx%s-thumbnail-%s.%s', $pathToMediaDirectory, $newWidth, $newHeight, $fileName, $fileExtension), 100); if ($ret === false) { throw new ThumbnailCreationFailedException('Thumbnail creation failed on saving thumbnail'); } $fileObject->setThumbnailLink(sprintf('%s/%sx%s-thumbnail-%s.%s', $httpPathToMediaDir, $newWidth, $newHeight, $fileName, $fileExtension)); $fileObject->setLocalThumbnailPath(sprintf('%s/%sx%s-thumbnail-%s.%s', $pathToMediaDirectory, $newWidth, $newHeight, $fileName, $fileExtension)); // free memory imagedestroy($tmpImg); }
public function update(File $file) { $this->setName($file->getName()); $this->setDescription($file->getDescription()); $this->setLink($file->getLink()); $this->setLocalPath($file->getLocalPath()); $this->setThumbnailLink($file->getThumbnailLink()); $this->setLocalThumbnailPath($file->getLocalThumbnailPath()); $this->setCategory($file->getCategory()); $this->setExtension($file->getExtension()); $this->setSize($file->getSize()); $this->setDimensions($file->getDimensions()); $this->setAuthor($file->getAuthor()); $this->setCreationDate($file->getCreationDate()); }
public function deleteFileOnDisk(File $file) { $ret = true; if (file_exists($file->getLocalPath())) { $ret = @unlink($file->getLocalPath()); } $retThumbnail = true; if (file_exists($file->getLocalThumbnailPath())) { $retThumbnail = @unlink($file->getLocalThumbnailPath()); } if (!$ret) { throw new \Exception(sprintf('Failed to delete file %s with path %s', $file->getName(), $file->getLocalPath())); } if (!$retThumbnail) { throw new \Exception(sprintf('Failed to delete file %s with path %s', $file->getName(), $file->getLocalThumbnailPath())); } }