liip_imagine与vich_uploader没有创建缓存

时间:2016-10-03 16:23:40

标签: php symfony liipimaginebundle vichuploaderbundle

我正在研究一个由两个内部项目组成的Symfony项目。一个项目和MARKETPLACE项目。 以下是文件在网络中的结构。

web/
---one/     #this one is being called from subdomain one.domain.com
------/app_dev.php
---marketplace/   #this one is being called from the main domain domain.com
------/app_dev.php

现在我有第三个文件夹

---/images

我希望允许用户从ONE上传图像并在MARKETPLACE中显示 这是我的配置

的应用程序/配置/束/ liip_imagine.yml

# LiipImagineBundle
liip_imagine:
    resolvers:
        default:
            web_path:
                web_root: %kernel.root_dir%/../../web/images
                cache_prefix: media/cache
    filter_sets:
        cache: ~
        image_xlarge:
            filters:
                thumbnail: { size: [1080, 708], mode: outbound }
        image_large:
            filters:
                thumbnail: { size: [535, 351], mode: outbound }
        thumb_large:
            filters:
                thumbnail: { size: [400, 262], mode: outbound }
        thumb_medium:
            filters:
                thumbnail: { size: [264, 173], mode: outbound }
        thumb_small:
            filters:
                thumbnail: { size: [250, 164], mode: outbound }
        thumb_xsmall:
            filters:
                thumbnail: { size: [175, 115], mode: outbound }
        square_large:
            filters:
                thumbnail: { size: [500, 500], mode: outbound }
        square_medium:
            filters:
                thumbnail: { size: [250, 250], mode: outbound }
        square_small:
            filters:
                thumbnail: { size: [100, 100], mode: outbound }
        square_xsmall:
            filters:
                thumbnail: { size: [50, 50], mode: outbound }

的应用程序/配置/束/ vich_uploader.yml

# VichUploaderBundle
vich_uploader:
    db_driver: orm
    mappings:
        library_media:
            uri_prefix:         /media/library
            upload_destination: %kernel.root_dir%/../../web/images/media/library
            inject_on_load:     false

这是图像的模型

的src / CoreBundle /型号/ MediaItemModel

namespace CoreBundle\Models;

use CoreBundle\Entity\ServiceCategory;
use CoreBundle\Entity\ServiceProvider;
use CoreBundle\Entity\ServiceProviderUserTypeEnum;
use CoreBundle\Entity\MediaItem;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class MediaItem
 *
 * @package CoreBundle\Models
 */
class MediaItemModel extends AbstractModel
{

    private $liipImagineController;
    private $liipImagineCacheManager;

    /**
     * @param $liipImagineController
     */
    public function setLiipController($liipImagineController)
    {
        $this->liipImagineController = $liipImagineController;
    }

    /**
     * @param $liipImagineCacheManager
     */
    public function setLiipCacheManager($liipImagineCacheManager)
    {
        $this->liipImagineCacheManager = $liipImagineCacheManager;
    }

    /**
     * Get path for media item file
     *
     * @param MediaItem $mediaItem
     * @param $size
     * @return string
     */
    public function getMediaItemFile(MediaItem $mediaItem, $size)
    {
        $fileName = '/../../web/images/media/library/' . $mediaItem->getName();
        if ($mediaItem->getServiceProvider()) {
            $fileName = '/../../web/images/media/serviceprovider/' . $mediaItem->getId() . '/' . $mediaItem->getName();
        }

        $this->liipImagineController
            ->filterAction(new Request(), $fileName, $size);

        $this->liipImagineCacheManager->getBrowserPath($fileName, $size);

        $result = '/../../web/images/media/cache/' . $size . '/media/library/' . $mediaItem->getName();

        if ($mediaItem->getServiceProvider()) {
            $result = '/../../web/images/media/cache/' . $size . '/media/serviceprovider/' . $mediaItem->getId() . '/' . $mediaItem->getName();
        }
//         $result = '/../../web/images/media/serviceprovider/' . $mediaItem->getId() . '/' . $mediaItem->getName();
        return $result;
    }

    /**
     * Get all items for ServiceCategory
     *
     * @param ServiceCategory $serviceCategory
     * @return array
     */
    public function getAllForServiceCategory(ServiceCategory $serviceCategory)
    {
        return $this->repository
            ->createQueryBuilder('i')
            ->where('i.serviceCategory = :serviceCategory')
            ->setParameter('serviceCategory', $serviceCategory)
            ->getQuery()
            ->getResult();
    }

    /**
     * Get medias for service provider
     *
     * @param ServiceProvider $serviceProvider
     *
     * @return ArrayCollection
     */
    public function getAllForServiceProvider(ServiceProvider $serviceProvider)
    {
        return $this->repository
            ->createQueryBuilder('m')
            ->where('m.serviceProvider = :serviceProvider')
            ->setParameter('serviceProvider', $serviceProvider)
            ->getQuery()
            ->getResult();
    }

    /**
     * Get general media items
     *
     * @param ServiceCategory $serviceCategory
     *
     * @return ArrayCollection
     */
    public function getGeneralMediaItems(ServiceCategory $serviceCategory = null)
    {
        $query = $this->repository
            ->createQueryBuilder('m')
            ->where('m.serviceProvider IS NULL');

        if ($serviceCategory) {
            $query->andWhere('m.serviceCategory = :serviceCategory')
                ->setParameter('serviceCategory', $serviceCategory);
        }

        return $query->getQuery()->getResult();
    }

    /**
     * Post new image to ServiceProvider
     *
     * @param ServiceProvider $serviceProvider
     * @param $file
     * @param string $rootDir
     *
     * @return MediaItem
     */
    public function postImage(ServiceProvider $serviceProvider, $file, $rootDir)
    {
        if ($file && $file->getPathName()) {
            $newFileName = $file->getClientOriginalName();

            $image = new MediaItem();
            $image->setServiceProvider($serviceProvider);
            $image->setTitle($newFileName);
            $image->setName($newFileName);
            $this->em->persist($image);
            $this->em->flush();

            $newPath = '/media/serviceprovider/' . $image->getId();

            if (!is_dir($rootDir . '/../../web/images' . $newPath)) {
                mkdir($rootDir . '/../../web/images' . $newPath, 0777, true);
            }

            move_uploaded_file($file->getPathName(), $rootDir . '/../../web/images' . $newPath . '/' . $newFileName);

            return $image;
        }
    }

}

具有与所有

相关的功能的第三个文件
/**
     * @Route\Get("/item/{mediaItemId}/{size}",
     *     defaults={"size" = "original"},
     *     options={"expose"=true},
     *     requirements={
     *     "size": "image_xlarge|image_large|thumb_large|thumb_medium|thumb_small|thumb_xsmall|square_large|square_medium|square_small|square_xsmall"
     * }))
     *
     * @ParamConverter("mediaItem", class="CoreBundle:MediaItem", options={"id" = "mediaItemId"})
     *
     * @param MediaItem $mediaItem
     * @param string $size
     *
     * @return Response
     */
    public function getMediaItemAction(MediaItem $mediaItem, $size)
    {
        if ($mediaItem->getServiceProvider()) {
            $this->denyAccessUnlessGranted('view', $mediaItem->getServiceProvider());
        }

        $filePath = $this->get('media_item_model')->getMediaItemFile($mediaItem, $size);
        $filePath = $this->get('kernel')->getRootDir() . '/../../web/images/' . $filePath;
        $headers = array(
            'Content-Type' => 'image/jpeg',
        );
        return new BinaryFileResponse($filePath, 200, $headers);
    }

现在的问题如下 如果我将ONE项目隔离到一个正常的symfony项目并重新设置文件夹指向web文件夹而不是图像,然后尝试上传图像,可通过connect_api_media_getgeneralmedias访问,我可以看到缩略图和一切就像这个domain.com/api/ 330 / thumb_medium 但是2个项目设置发生了什么,是创建了media / serviceprovider / 330 / image.jpeg,但是没有缓存或库文件夹,所以我出于某种原因无法真正使用liib库来使用缩略图功能。 / p>

任何想法为什么会发生这种情况?

PS。权限都很好 PS。 gd库已安装并正常工作

如需更多代码,请告知我们。

2 个答案:

答案 0 :(得分:2)

getMediaItemFile中,同时添加liip_imagine.data.managerliip_imagine.filter.manager作为依赖项,然后尝试:

if (!$this-> liipImagineCacheManager->isStored($filePath, $size)) {
    $binary = $this->dataManager->find($size, $filePath);

    $filteredBinary = $this->filterManager->applyFilter($binary, $size);

    // This should store the thumbnail in web/images/media/cache
    $this->liipImagineCacheManager->store($filteredBinary, $filePath, $filterName);
}

// or similar
return $this->liipImagineCacheManager->resolve($filePath, $filterName);

这只是让您更明确地存储到liip缓存文件夹,例如:在web/images文件夹中。

(小的,无关的 - 您可以考虑将getMediaItemFile分解为您调用以解析图像的服务。它在实体模型类中有点不合适。)

答案 1 :(得分:1)

对于每个来到这里的人,我终于可以找到问题所在。 问题分为两个主要问题,首先liip_imagine配置文件应如下所示:

liib_imagine.yml

# LiipImagineBundle
liip_imagine:
    resolvers:
        default:
            web_path:
                web_root: %kernel.root_dir%/../../web/images
                cache_prefix: media/cache
    loaders:
        default:
            filesystem:
                data_root: %kernel.root_dir%/../../web/images
    filter_sets:
        image_xlarge:
            filters:
                thumbnail: { size: [1080, 708], mode: outbound }
        image_large:
            filters:
                thumbnail: { size: [535, 351], mode: outbound }
        thumb_large:
            filters:
                thumbnail: { size: [400, 262], mode: outbound }
        thumb_medium:
            filters:
                thumbnail: { size: [264, 173], mode: outbound }
        thumb_small:
            filters:
                thumbnail: { size: [250, 164], mode: outbound }
        thumb_xsmall:
            filters:
                thumbnail: { size: [175, 115], mode: outbound }
        square_large:
            filters:
                thumbnail: { size: [500, 500], mode: outbound }
        square_medium:
            filters:
                thumbnail: { size: [250, 250], mode: outbound }
        square_small:
            filters:
                thumbnail: { size: [100, 100], mode: outbound }
        square_xsmall:
            filters:
                thumbnail: { size: [50, 50], mode: outbound }

注意加载器中的data_root>文件系统。 第二是" ../"由于某种原因,函数getMediaItemAction没有接受我添加" ../../"它,所以我不得不摆脱它,问题解决了,现在缓存正在运行。

getMediaIteamAction函数

/**
     * Get path for media item file
     *
     * @param MediaItem $mediaItem
     * @param $size
     * @return string
     */
    public function getMediaItemFile(MediaItem $mediaItem, $size)
    {
       $fileName = '/media/library/' . $mediaItem->getName();
        if ($mediaItem->getServiceProvider()) {
            $fileName = '/media/serviceprovider/' . $mediaItem->getId() . '/' . $mediaItem->getName();
        }

        $this->liipImagineController
            ->filterAction(new Request(), $fileName, $size);

        $this->liipImagineCacheManager->getBrowserPath($fileName, $size);

        $result = '/media/cache/' . $size . '/media/library/' . $mediaItem->getName();

        if ($mediaItem->getServiceProvider()) {
            $result = '/media/cache/' . $size . '/media/serviceprovider/' . $mediaItem->getId() . '/' . $mediaItem->getName();
        }

        return $result;
    }

注意" ../../"现已删除

关于@Camerogn Hurd对这个问题的另一个答案他提到的功能可能会解决一些问题,因为它完全有效地强制创建缓存,但你还需要摆脱" ../&# 34;因为捆绑不接受这个。