liip_imagine with vich_uploader not creating cache

僤鯓⒐⒋嵵緔 提交于 2020-02-02 06:37:13

问题


I am working on a Symfony project which consists of two inner projects. ONE project and MARKETPLACE project. Here is how the files strucure in the web.

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

now i have a third folder

---/images

i want to allow the user to upload images from the ONE and to be displayed in the MARKETPLACE here is my config

apps/config/bundles/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 }

apps/config/bundles/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

this is the model for the images

src/CoreBundle/Models/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;
        }
    }

}

third file with function related to all of this

/**
     * @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);
    }

now the problem is as follows if i isolated the ONE project into one normal symfony project and remade the folders to point toward the web folder instead of images and then try to upload image its accessible via connect_api_media_getgeneralmedias i can see the thumbnails and everything just like this domain.com/api/330/thumb_medium but what's happening with the 2 projects setup is that the media/serviceprovider/330/image.jpeg is created but there is no cache or library folder so i can't really use the liib library for some reason to use the thumbnails function.

any ideas why is this happening ?

ps. permissions all good ps. gd library installed and working

for any more code please let me know.


回答1:


In getMediaItemFile, add the liip_imagine.data.manager, and liip_imagine.filter.manager as dependencies, too, and try:

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);

This just lets you be more explicit about storing to the liip cache folders, e.g. in your web/images folder.

(Small, unrelated point - you may consider breaking out that getMediaItemFile into a service that you call to resolve images. It's a bit out of place in an entity model class.)




回答2:


For everyone who comes here i could finally find the problem. The problem is separated into two main problems first the liip_imagine config file should be as follows:

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 }

notice the data_root in the loaders > file system. second is the "../" issue with this bundle for some reason the function getMediaItemAction didn't accept that i add "../../" to it so i had to get rid of it and the problem were solved and now the cache is working.

getMediaIteamAction function

/**
     * 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;
    }

notice that the "../../" is now removed

also regarding to @Camerogn Hurd the another answer to this question the function he mentioned might fix some issues because its totally working also to force create the cache but you also need to get rid of "../" since the bundle doesn't accept this.



来源:https://stackoverflow.com/questions/39836143/liip-imagine-with-vich-uploader-not-creating-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!