Home » PHP » Pimcore » Upload image through Pimcore command in the asset folder

Upload image through Pimcore command in the asset folder

Pimcore

Create the command folder in the AppBundle folder. Than create the ImageUploadCommand.php

/var/www/html/proj/src/AppBundle/Command/ImageUploadCommand.php

<?php

namespace AppBundle\Command;

use Pimcore\Console\AbstractCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use AppBundle\Service\UploadImage;

class ImageUploadCommand extends AbstractCommand
{
    protected function configure()
    {
        $this
            ->setName('imageupload:command')
            ->setDescription('Image upload command');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $obj = new UploadImage();
        $imgUpload = $obj->uploadImage();       
        $this->dump('File Uploaded');
    }
}

Create the file in the services folder. /var/www/html/proj/src/AppBundle/Service/UploadImage.php

<?php

/**
 * Service to import UploadImage.
 *
 * @author
 */

namespace AppBundle\Service;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject;
use Pimcore\File;
use AppBundle\Command\Pimcore\Model;
use Pimcore\Tool;
use Pimcore\Model\Element;

class UploadImage {
    public function uploadImage() {        
        $image = $filePath."image.png";
        $image_parts = pathinfo($image);       
        $newAsset = new \Pimcore\Model\Asset();
        $newAsset->setFilename($image_parts['filename'].".". $image_parts['extension']);
        $newAsset->setData(file_get_contents($image));
        $newAsset->setParent(\Pimcore\Model\Asset::getByPath("/"));
        $newAsset->save(); 
    }
}

Run the below command on the terminal .

bin/console imageupload:command

Check Also

Pimcore

How can replace all images with the same name in the assets folder

Put the below function in your class file and run the static route. /** * …

Leave a Reply

Your email address will not be published. Required fields are marked *