Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uploaded images have size zero #127

Open
pizzetto opened this issue Jun 13, 2017 · 2 comments
Open

Uploaded images have size zero #127

pizzetto opened this issue Jun 13, 2017 · 2 comments

Comments

@pizzetto
Copy link

pizzetto commented Jun 13, 2017

I have an issue uploading images with GridFS, here is my code

import { MongoObservable } from 'meteor-rxjs';
import { UploadFS } from 'meteor/jalik:ufs';
import { Meteor } from 'meteor/meteor';
import gm from 'gm';
import { Picture, DEFAULT_PICTURE_URL } from '../models';

export interface PicturesCollection<T> extends MongoObservable.Collection<T> {
    getPictureUrl(selector?: Object | string): string;
}

export const Pictures =
    new MongoObservable.Collection<Picture>('pictures') as PicturesCollection<Picture>;

export const PicturesStore = new UploadFS.store.GridFS({
    collection: Pictures.collection,
    name: 'pictures',
    chunkSize: 1024*255,
    filter: new UploadFS.Filter({
      minSize: 1,
      maxSize: 1024 * 10000, // 10MB,
      contentTypes: ['image/*'],
      extensions: ['jpg', 'png', 'gif']
    }),
    permissions: new UploadFS.StorePermissions({
        insert: picturesPermissions,
        update: picturesPermissions,
        remove: picturesPermissions
    }),
    // Transform file when reading
    transformRead(from, to, fileId, file, request) {
        from.pipe(to); // this returns the raw data
    },
    transformWrite(from, to, fileId, file) {
      let gm = Npm.require('gm');
      if (gm) {
        gm(from)
            .resize(400, 400)
            .gravity('Center')
            .extent(400, 400)
            .quality(75)
            .stream().pipe(to);
      } else {
          console.error("gm is not available", file);
      }
    }
});

Pictures.getPictureUrl = function (selector) {
    const picture = this.findOne(selector) || {};
    return picture.url || DEFAULT_PICTURE_URL;
};

function picturesPermissions(userId: string): boolean {
    return Meteor.isServer || !!userId;
}

actually the picture is being saved on Mongo, but with size zero.
If I remove "transformWrite" everything works like a charm. Maybe is a problem related to gm, but i did not found any solution.

Note: using localFS produce the same issue.

I have installed gm with

meteor npm install -g gm

Thanks

@macman31
Copy link

Installing gm is not enough, you also have to install the graphicsmagick package on your OS.
For example on Debian/Ubuntu: apt update && apt install graphicsmagick, or through your graphical package manager.

@jalik
Copy link
Owner

jalik commented Jun 21, 2017

To complete @macman31 's anwer, gm is a node package that expose an API (methods) to do some processing on images (resize, crop, compress...), but it does nothing on its own, instead it requires graphicsmagick which is a system package that you can install with apt-get on Debian based OSes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants