Image cropping plugin

When you do Ionic serve, what’s the error exactly? Could you show us plz

About scss, maybe it’s because the cropper.scss isn’t build with your app? To add this scss to my build process I modified my gulpfile.js to explicitly include it, like following:

gulp.task('sass', function(){
return buildSass({
    sassOptions: {
        includePaths: [
            'node_modules/ionic-angular',
            'node_modules/ionicons/dist/scss',
            'node_modules/cropperjs/src/scss'
        ]
    }
});
});

The scss problem was solved doing it as you said, but the problem persists.

Thx for the feedback. Have you installed the typescript definition? Maybe something went wrong there?

typings install dt~cropperjs --save --global

For the record I opened a task about the construct error with the last version of cropperjs

https://github.com/fengyuanchen/cropperjs/issues/94

Seems this issue was already reported with jquery, but I wasn’t able to solve it the same way

https://github.com/fengyuanchen/cropperjs/issues/63

Wait and see

Yeah it was already installed, i’ve followed your “how to guide” step by step, i don’t know what might be happening… i’ve googled the error and i can’t seem to find anything like it or any other person with the same error, so it’s pretty frustrating.

Any help would be awesome, i really need to use this plugin

image

Ummm, but maybe

  1. Just to be sure, could you double check that the file under typings/globals/cropperjs/index.d.ts look like the following one. You have an error on line 14 so I guess the import of cropper already failed.

    https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/cropperjs/cropperjs.d.ts

  2. I saw in your code a declaration difference. Don’t think it’s the reason but I don’t see any other difference

    let cropper:cropperjs.Cropper = new Cropper(…

  3. You have your project on Github? Could clone it to have a look if you want, but not sure I could help, but I could try

Ok guys, I was able, with help of @fengyuanchen from cropperjs, to update my project to the last version of cropperjs and solve the issue we had with that last version.

Here how I have proceeded:

  1. If you already have cropperjs in your project remove it

    sudo npm remove cropperjs --save

  2. Install the last version of cropperjs

    sudo npm install cropperjs --save

  3. If you don’t have it yet, install the typescript definition (if you had cropperjs in your project before, skip)

    typings install dt~cropperjs --save --global

  4. IMPORTANT STEP The typescript definition isn’t compatible with the last version of cropperjs, therefore you have to edit manually. Edit file “typings/globals/cropperjs/index.d.ts” and replace following line of code (at the end of the file)

    export = Cropper;

with

export default Cropper;

Note: This is the fix to the issue we discussed before with the last version of the library.

5 Update or add to your gulp file a task to include the cropperjs stylesheet in your build. For those who had cropperjs before in their project, note the new subpath “/dist” instead of “/src/scss”

gulp.task('sass', function(){
return buildSass({
    sassOptions: {
        includePaths: [
            'node_modules/ionic-angular',
            'node_modules/ionicons/dist/scss',
            'node_modules/cropperjs/dist'
        ]
    }
});

});

6 cropperjs isn’t shipped anymore with a scss file but with a less file. that’s why you can’t include the source stylesheet anymore, but, luckily, scss let you include css file. Therefore, update or add in your app.core.scss following import:

 @import "cropper";

Note: Yes that’s right, without any extension to “cropper”. Explicitly works like this for me.

7 The typescript code remain the same as described above except the import of the library which should now be done like following:

 import Cropper from 'cropperjs';

Note: As you see, no more “* as” in the import. This was indicated by the creator of the library, see https://github.com/fengyuanchen/cropperjs/issues/94#issuecomment-249396533 for mor details

That’s it. Hope it will work for you too.

P.S.: about an update of the definition file, I opened following issue:

 https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11482
3 Likes

Thanks for the detailed recipe, works great with the latest version 0.8.1

1 Like

Used to work like a charm with beta and typing, but how can I do that now with RC0 and @types please. thank you

Hey @youssef512, the only things I changed to migrate that Image cropping to RC.0 are the followings:

-Install cropperjs with @types like following (don’t have the command in my history but that should work)

sudo npm install @types/cropperjs --save

-Modifiy typescript file import and class variable declaration:

import Cropper from 'cropperjs'; // Note no more import *

...

   private cropper:Cropper; // Note no more package reference like cropperjs.Cropper

That worked for me. Let me knows if I could help further.

Thanks a lot it seems to work, only thing is intellij can’t resolve cropperjs which I find really weird, I think it’s because of rollerjs that is not taken into account in the paths by intellij.
For me to make the css work (since now I don’t use gulp any more), I had to modify: “node_modules/@ionic/app-scripts/config/sass.config.js” and add ‘node_modules/cropperjs/dist’ inside includePaths Array.
as for “ionicons” intellij can’t resolve sass files two (and I hate the red flags XD).
Edit:
-even better create a folder config and put in it a copy on of the file sass.config.js
-edit package.json and add:
“config”: {
“ionic_sass”: “./config/sass.config.js”
},

1 Like

@reedrichards How did you manage to add the cropperjs css into varibles.scss in RC0 ?

I have try this
@import "cropper"; but i get error File to import not found or unreadable: cropper

Have you override the sass.config.js script to copy the needed css?

In package.json add a config to your own local sass.config.js

"config": {
    "ionic_sass": "./scripts/sass.config.js"
},

then create your own local script

module.exports = {
      includePaths: [
        'node_modules/ionic-angular/themes',
        'node_modules/ionicons/dist/scss',
       'node_modules/cropperjs/dist'
  ]
};

finally add your import

@import "cropper";

in one of your scss file. I added it personally in app.component.scss

Note: When you gonna run ionic serve, if you modify something to a scss, the live reload my produce an error that the reload weren’t able to find that included css file. There is an open issue about that in the @scripts project of Ionic

2 Likes

Thank you , it work great ! :smiley:

1 Like

@reedrichards, thanks for your awesome responses on this thread. It helped many people. Can you share your code, if possible. I am trying to use cropper with Ionic2 and Meteor. As suggested by you, I am doing the same, but the image cropper does not work for me. I am able to take image and show it on UI, cropper also shows the image cropper section (image for cropping) but cropping control does not show, so on clicking / dragging on image, nothing happens. I think I am also not able to add cropper CSS to my project CSS with @import so that may also be an issue. Do you know how to add CSS to SCSS file.

If you can share your ful code snippet or create plunkr that will be great and will help many more.

If nothing happens and if your image is stuck that is most probably due because of the css you are right. Have you seen that post about the deployment of the css ?

If yes and it still doesn’t work, if you want I could checkout your project and have a quick look.

Otherwise sure I could post my code here no prob let me know.

P.S.: Don’t really have time right now to create a plunkr project sorry…releasing a beta version right now

1 Like

@reedrichards, yes it was CSS issue as we suspected. Once I added cropper’s CSS to my meteor project, I could see the cropper image. Next step is how to get the cropped image as base64 URI. I assume this.cropper.getCroppedCanvas() should give me that.

Thanks for your help.

Good to hear you solved that. Yep getCroppedCanvas(), something like

let croppedImgURI:string = this.cropper.getCroppedCanvas({
        width: 1080,
        height: 1080
    }).toDataURL('image/jpeg', (90 / 100)); // 90 / 100 = photo quality

Thanks for that tip. I am stuck with another issue related to including script. I wonder if you can help me with this.

I want to import jquery and signalr and it seems just installing npm modules and adding to declaration.d.ts file the name of the module does not work.

Would you know how to add to the export script so I have a specific order of modules? In my case, jquery first and then signalr. This should be similar to how the script above is added, but I don’t know where in package.json to do this.

Thanks for any help. Even for taking time to read.

Sure. Maybe would be good to open a new post about your issue where you could display what commands you run and how does your code looks like.

Just post then here a link to that post, I gonna follow :wink: