技术背景: 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');
}
类上传助手:
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 %}