iwyg / jitimage

Just In Time image manipulation (GD, Imagick, imagemagick) with integration for laravel 4
MIT License
95 stars 8 forks source link

Manipulate file from post request and save it #25

Closed abhimanyu003 closed 10 years ago

abhimanyu003 commented 10 years ago

It will be great if there is ability to manipulate the image file from the the request and then save it to desired folder

Something like this

$file = JitImage::source(Input::file('image'))->cropAndResize(500, 500, 5)->save('somefolder/filename.jpg');
iwyg commented 10 years ago

First of all, I think you shouldn't use the facade class anywhere outside of a template. Secondly moving uploaded files is totally out of the scope of this package. However you could achieve this in several ways using the request::files move method and process the image afterwards (using the underlying library of jitimage), e.g. something like this:

<?php

namespace My\Package;

use \Thapp\JitImage\Driver\DriverInterface;

class Processor
{
    protected $driver;

    public function __construct(DriverInterface $driver)
    {
        $this->driver = $driver;
    }

    public function load($file)
    {
        return $this->driver->load($file);
    }

    public function process($filter, $width = null, $height = null, array $options = [])
    {
        $this->driver->setTargetSize($width, $height);
        $this->driver->filter($filter, $options);
    }

    public function save($target, $deleteSource = true)
    {
        file_put_contents($target, $this->driver->getImageBlob());

        if ((boolean)$deleteSource) {
            @unlink($this->driver->getSource());
        }
        $this->driver->clean();
    }
}

Somewhere in you application logic


use \My\Package\Processor;
use \Thapp\JitImage\Driver\ImagickDriver;
use \Thapp\JitiImage\Driver\ImageSourceLoader;

$processor = new Processor(new ImagickDriver(new ImageSourceLoader));
$processor->load($file);
$processor->process('cropScale', 500, 500, [5]);
$processor->save($target, true);

Just fiddle around.