技术上下文: Symfony 4.4,liipimagine。
我正在开发一款网络应用程序,主要用于智能手机。在我的应用程序中,用户可以为他的个人资料上传一张图片。
基本上,我的上传很好。我唯一的问题是当人们想从iphone上传图片时:我不能呈现.heic文件,因为这是新的格式。
我希望我能转换heic文件,或者如果有方法显示它。
以下是我现有的代码:
控制器:
/** @var FormInterface $form */
$form = $this->createForm(UsersType::class, $currentUser);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$uploadedFile = $form['imageFilename']->getData();
if ($uploadedFile) {
$newFilename = $uploaderHelper->uploadUserImage($uploadedFile, $currentUser);
$currentUser->setImageFilename($newFilename);
$entityManager->persist($currentUser);
$entityManager->flush();
}
return $this->redirectToRoute('users_participants_list');
}类UploadHelper:
class UploaderHelper
{
const USER_IMAGE = 'user_image';
/** @var string $uploadsPath */
private $uploadsPath;
/**
* @var RequestStackContext
*/
private $requestStackContext;
/**
* UploaderHelper constructor.
* @param string $uploadsPath
* @param RequestStackContext $requestStackContext
*/
public function __construct(string $uploadsPath, RequestStackContext $requestStackContext)
{
$this->uploadsPath = $uploadsPath;
$this->requestStackContext = $requestStackContext;
}
/**
* @param UploadedFile $uploadedFile
* @param Users $user
* @return string
*/
public function uploadUserImage(UploadedFile $uploadedFile, Users $user): string
{
/** @var string $destination */
$destination = $this->uploadsPath . '/' . self::USER_IMAGE;
/** @var string $originalFilename */
$originalFilename = pathinfo($uploadedFile->getClientOriginalName(), PATHINFO_FILENAME);
if ($uploadedFile->guessExtension() == 'heic') {
// We need to convert the image
}
/** @var string $newFilename */
$newFilename = strtolower($user->getName()).'_'.strtolower($user->getSurname()).'-'.uniqid().'.'.$uploadedFile->guessExtension();
$uploadedFile->move($destination, $newFilename);
// $this->correctImageOrientation($destination);
return $newFilename;
}
/**
* @param string $path
* @return string
*/
public function getPublicPath(string $path): string
{
return $this->requestStackContext->getBasePath() . '/uploads/' . $path;
}配置liip:
liip_imagine:
# valid drivers options include "gd" or "gmagick" or "imagick"
driver: "imagick"
filter_sets:
squared_thumbnail_small:
filters:
thumbnail:
size: [200, 200]视图中的显示:
{% if currentUser.imageFilename %}
<img src="{{ uploaded_asset(currentUser.imagePath)|imagine_filter('squared_thumbnail_small') }}" alt="{{ currentUser.name }} {{ currentUser.surname }}">#}
{% endif %}发布于 2020-01-16 12:03:50
您似乎可以在不转换文件的情况下处理这个问题,可以使用VichUploaderBundle上传文件https://symfony.com/doc/current/bundles/EasyAdminBundle/integration/vichuploaderbundle.html,并且可以在实体类中验证这个问题链接中提到的文件。
/**
* @param ExecutionContextInterface $context
*/
public function validate(ExecutionContextInterface $context)
{
if (! in_array($this->file->getMimeType(), array(
'image/jpeg',
'image/gif',
'image/png',
'image/heif',
'image/heic',
'image/heif-sequence',
'image/heic-sequence',
))) {
$context
->buildViolation('Wrong file type (mp4,mov,avi)')
->atPath('fileName')
->addViolation()
;
}
} 如何将mimeType断言与VichUploader一起使用?
希望它能帮到你。
https://stackoverflow.com/questions/59747804
复制相似问题