本文整理匯總了PHP中Symfony\Component\HttpFoundation\File\File::getMimeType方法的典型用法代碼示例。如果您正苦於以下問題:PHP File::getMimeType方法的具體用法?PHP File::getMimeType怎麽用?PHP File::getMimeType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Symfony\Component\HttpFoundation\File\File
的用法示例。
在下文中一共展示了File::getMimeType方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: getMimeType
/**
* Get mime type of file.
*
* @return string
*/
public function getMimeType()
{
if ($this->file instanceof UploadedFile) {
return $this->file->getClientMimeType();
}
return $this->file->getMimeType();
}
示例2: isValid
public function isValid($value, Constraint $constraint)
{
if (null === $value || '' === $value) {
return true;
}
if (!is_scalar($value) && !$value instanceof FileObject && !(is_object($value) && method_exists($value, '__toString()'))) {
throw new UnexpectedTypeException($value, 'string');
}
if ($value instanceof FileObject && null === $value->getPath()) {
return true;
}
$path = $value instanceof FileObject ? $value->getPath() : (string) $value;
if (!file_exists($path)) {
$this->setMessage($constraint->notFoundMessage, array('{{ file }}' => $path));
return false;
}
if (!is_readable($path)) {
$this->setMessage($constraint->notReadableMessage, array('{{ file }}' => $path));
return false;
}
if ($constraint->maxSize) {
if (ctype_digit((string) $constraint->maxSize)) {
$size = filesize($path);
$limit = $constraint->maxSize;
$suffix = ' bytes';
} else {
if (preg_match('/^(\\d+)k$/', $constraint->maxSize, $matches)) {
$size = round(filesize($path) / 1000, 2);
$limit = $matches[1];
$suffix = ' kB';
} else {
if (preg_match('/^(\\d+)M$/', $constraint->maxSize, $matches)) {
$size = round(filesize($path) / 1000000, 2);
$limit = $matches[1];
$suffix = ' MB';
} else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
}
}
}
if ($size > $limit) {
$this->setMessage($constraint->maxSizeMessage, array('{{ size }}' => $size . $suffix, '{{ limit }}' => $limit . $suffix, '{{ file }}' => $path));
return false;
}
}
if ($constraint->mimeTypes) {
if (!$value instanceof FileObject) {
$value = new FileObject($value);
}
if (!in_array($value->getMimeType(), (array) $constraint->mimeTypes)) {
$this->setMessage($constraint->mimeTypesMessage, array('{{ type }}' => '"' . $value->getMimeType() . '"', '{{ types }}' => '"' . implode('", "', (array) $constraint->mimeTypes) . '"', '{{ file }}' => $path));
return false;
}
}
return true;
}
示例3: resize
/**
* Resize image
*
* @param int $width Thumbnail width
* @param int $height Thumbnail height
* @param string $fileName Source file name
* @return \Response Image content
*/
public function resize($width, $height, $fileName)
{
$destFile = $this->createThumb($width, $height, $fileName);
$fileContent = $this->getFileContent($destFile);
$file = new SymfonyFile($destFile);
return response()->make($fileContent, 200, ['content-type' => $file->getMimeType()]);
}
示例4: create
/**
* @param File $file
*
* @return NotImageGivenException
*/
public static function create(File $file)
{
$msg = sprintf('File "%s" is not an image, expected mime type is image/*, given - ', $file->getPathname(), $file->getMimeType());
$me = new static($msg);
$me->failedFile = $file;
return $me;
}
示例5: missingMethod
/**
* @param array $parameters
*
* @return mixed|void
*/
public function missingMethod($parameters = array())
{
if (is_array($parameters)) {
$parameters = implode('/', $parameters);
}
# If parameters is a file
if ($file = $this->path($parameters)) {
$response = \with(new AssetCollection(array(new FileAsset($file))))->dump();
} elseif ($aParameters = json_decode($parameters, TRUE)) {
array_walk($aParameters, function (&$item) {
$item = new FileAsset($this->path($item));
});
$response = \with(new AssetCollection($aParameters))->dump();
}
if (isset($response)) {
$headers = array();
if (preg_match('/.css/i', $parameters)) {
$headers['Content-Type'] = 'text/css';
} elseif (preg_match('/.js/i', $parameters)) {
$headers['Content-Type'] = 'text/javascript';
} elseif (preg_match('/.html|.htm|.php/i', $parameters)) {
$headers['Content-Type'] = 'text/html';
} else {
$file = new File($file);
$mime = $file->getMimeType();
if ($mime) {
$headers['Content-Type'] = $mime;
} else {
$headers['Content-Type'] = 'text/html';
}
}
return Response::make($response, 200, $headers);
}
return Response::make('', 404);
}
示例6: downloadAction
/**
* @Route("/download/{resource}", name="bkstg_resource_download_resource")
* @ParamConverter("resource", class="BkstgResourceBundle:Resource")
*/
public function downloadAction(Resource $resource)
{
$file = new File($resource->getAbsolutePath());
$headers = array('Content-Type' => $file->getMimeType(), 'Content-Disposition' => 'attachment; filename="' . $resource->getPath() . '"');
$filename = $resource->getAbsolutePath();
return new HttpFoundation\Response(file_get_contents($filename), 200, $headers);
}
示例7: createImage
/**
* Given a single File, assuming is an image, create a new
* Image object containing all needed information.
*
* This method also persists and flush created entity
*
* @param File $file File where to get the image
*
* @return ImageInterface Image created
*
* @throws InvalidImageException File is not an image
*/
public function createImage(File $file)
{
$fileMime = $file->getMimeType();
if ('application/octet-stream' === $fileMime) {
$imageSizeData = getimagesize($file->getPathname());
$fileMime = $imageSizeData['mime'];
}
if (strpos($fileMime, 'image/') !== 0) {
throw new InvalidImageException();
}
$extension = $file->getExtension();
if (!$extension && $file instanceof UploadedFile) {
$extension = $file->getClientOriginalExtension();
}
/**
* @var ImageInterface $image
*/
$image = $this->imageFactory->create();
if (!isset($imageSizeData)) {
$imageSizeData = getimagesize($file->getPathname());
}
$name = $file->getFilename();
$image->setWidth($imageSizeData[0])->setHeight($imageSizeData[1])->setContentType($fileMime)->setSize($file->getSize())->setExtension($extension)->setName($name);
return $image;
}
示例8: testGetMimeTypeUsesMimeTypeGuessers
public function testGetMimeTypeUsesMimeTypeGuessers()
{
$file = new File(__DIR__ . '/Fixtures/test.gif');
$guesser = $this->createMockGuesser($file->getPathname(), 'image/gif');
MimeTypeGuesser::getInstance()->register($guesser);
$this->assertEquals('image/gif', $file->getMimeType());
}
示例9: setFileAttribute
public function setFileAttribute(\Symfony\Component\HttpFoundation\File\File $file)
{
$this->attributes['file'] = $file;
$this->original_name = $file instanceof UploadedFile ? $file->getClientOriginalName() : $file->getFilename();
$this->size = $file->getSize();
$this->content_type = $file->getMimeType();
}
示例10: testCreateImagesFromApplicationOctetStream
/**
* @param $imageFileObject
* @param $finalMimeType
*
* @dataProvider imagesAsOctetStreamProvider
*/
public function testCreateImagesFromApplicationOctetStream(File $imageFileObject, $finalMimeType)
{
$this->assertEquals('application/octet-stream', $imageFileObject->getMimeType());
$image = $this->imageManager->createImage($imageFileObject);
$this->assertInstanceOf('Elcodi\\Component\\Media\\Entity\\Interfaces\\ImageInterface', $image);
$this->assertEquals($finalMimeType, $image->getContentType());
}
示例11: getResponse
/**
* @param Closure $callback
* @param InterventionRequest $interventionRequest
* @return Response
*/
public function getResponse(Closure $callback, InterventionRequest $interventionRequest)
{
try {
$this->cacheFile = new File($this->cacheFilePath);
$response = new Response(file_get_contents($this->cacheFile->getPathname()), Response::HTTP_OK, ['Content-Type' => $this->cacheFile->getMimeType(), 'Content-Disposition' => 'filename="' . $this->realImage->getFilename() . '"', 'X-Generator-Cached' => true]);
$response->setLastModified(new \DateTime(date("Y-m-d H:i:s", $this->cacheFile->getMTime())));
} catch (FileNotFoundException $e) {
if (is_callable($callback)) {
$image = $callback($interventionRequest);
if ($image instanceof Image) {
$this->saveImage($image);
$this->cacheFile = new File($this->cacheFilePath);
if (null !== $this->dispatcher) {
// create the ImageSavedEvent and dispatch it
$event = new ImageSavedEvent($image, $this->cacheFile);
$this->dispatcher->dispatch(ImageSavedEvent::NAME, $event);
}
// send HTTP header and output image data
$response = new Response(file_get_contents($this->cacheFile->getPathname()), Response::HTTP_OK, ['Content-Type' => $image->mime(), 'Content-Disposition' => 'filename="' . $this->realImage->getFilename() . '"', 'X-Generator-First-Render' => true]);
$response->setLastModified(new \DateTime('now'));
} else {
throw new \RuntimeException("Image is not a valid InterventionImage instance.", 1);
}
} else {
throw new \RuntimeException("No image handle closure defined", 1);
}
}
$this->initializeGarbageCollection();
return $response;
}
示例12: thumb
public function thumb(Request $request, $id)
{
$photo = Photo::findOrFail($id);
$file = new File($photo->path);
$photo->makeThumbnail();
$headers = array('Content-Type: ' . $file->getMimeType());
return response()->download($photo->path . '.' . $file->guessExtension(), $photo->name, $headers);
}
示例13: getMimeType
/**
* Returns the mime type of the file.
*
* The mime type is guessed using the functions finfo(), mime_content_type()
* and the system binary "file" (in this order), depending on which of those
* is available on the current operating system.
*
* @returns string The guessed mime type, e.g. "application/pdf"
*/
public function getMimeType()
{
$mimeType = parent::getMimeType();
if (null === $mimeType) {
$mimeType = $this->mimeType;
}
return $mimeType;
}
示例14: __construct
/**
* Constructor.
*
* @param File $file A File instance
*/
public function __construct(File $file)
{
if ($file instanceof UploadedFile) {
parent::__construct($file->getPathname(), $file->getClientOriginalName(), $file->getClientMimeType(), $file->getClientSize(), $file->getError(), true);
} else {
parent::__construct($file->getPathname(), $file->getBasename(), $file->getMimeType(), $file->getSize(), 0, true);
}
}
示例15: getMimeType
/**
* Returns the mime type of the file.
*
* The mime type is guessed using the functions finfo(), mime_content_type()
* and the system binary "file" (in this order), depending on which of those
* is available on the current operating system.
*
* @returns string The guessed mime type, e.g. "application/pdf"
*/
public function getMimeType()
{
$mimeType = parent::getMimeType();
if (is_null($mimeType)) {
$mimeType = $this->mimeType;
}
return $mimeType;
}