mardi 26 septembre 2017

Laravel 5.4 GIF Images are saved "statically"

I am working on this Laravel 5.4 Social Network Script and when I post something the avatar is being displayed "animated" but when it is being saved it is converted to an static image.

I have this in the controller TimelineController@createPost:

...
use App\Media;
use App\Post;
use Intervention\Image\Facades\Image;
...
public function createPost(Request $request)
{
    $input = $request->all();

    $post = Post::create($input);
    ...
    if ($request->file('post_images_upload_modified')) {
        foreach ($request->file('post_images_upload_modified') as $postImage) {
            $strippedName = str_replace(' ', '', $postImage->getClientOriginalName());
            $photoName = date('Y-m-d-H-i-s').$strippedName;

            $avatar = Image::make($postImage->getRealPath());
            $avatar->save(storage_path().'/uploads/users/gallery/'.$photoName, 60);

            $media = Media::create([
                  'title'  => $photoName,
                  'type'   => 'image',
                  'source' => $photoName,
                ]);

            $post->images()->attach($media);
        }
    }
    ...
}
...

It uses GD Library to process the images internally.

The file Image.php on /vendor/intervention/image/src/Intervention/Image/:

  • @method \Intervention\Image\Image make(mixed $source) Universal factory method to create a new image instance from source, which can be a filepath, a GD image resource, an Imagick object or a binary image data.
/**
 * Starts encoding of current image
 *
 * @param  string  $format
 * @param  integer $quality
 * @return \Intervention\Image\Image
 */
public function encode($format = null, $quality = 90)
{
    return $this->driver->encode($this, $format, $quality);
}

/**
 * Saves encoded image in filesystem
 *
 * @param  string  $path
 * @param  integer $quality
 * @return \Intervention\Image\Image
 */
public function save($path = null, $quality = null)
{
    $path = is_null($path) ? $this->basePath() : $path;

    if (is_null($path)) {
        throw new Exception\NotWritableException(
            "Can't write to undefined path."
        );
    }

    $data = $this->encode(pathinfo($path, PATHINFO_EXTENSION), $quality);
    $saved = @file_put_contents($path, $data);

    if ($saved === false) {
        throw new Exception\NotWritableException(
            "Can't write image data to path ({$path})"
        );
    }

    // set new file info
    $this->setFileInfoFromPath($path);

    return $this;
}

The stored image is a GIF but resized despite it is not specified:

enter image description here

Do you know how to make the image animated when saved?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire