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

How to embed local file images to a blog post model #1545

Closed
jacargentina opened this issue Jul 21, 2015 · 11 comments
Closed

How to embed local file images to a blog post model #1545

jacargentina opened this issue Jul 21, 2015 · 11 comments

Comments

@jacargentina
Copy link
Contributor

I've searched for an example, but every of those just have 1 image using CloudinaryImage

What i wanted is to have a Blog model with its Html field, but attach some "Local" images to it, and be able to embed those on the html body

Any advice is welcome!

@jeffreypriebe
Copy link
Contributor

There isn't a great option, but there is a basic one. No upload, simply linking an existing image that is on the web somewhere.

The basic option is adding the TinyMCE images plugin . That will get you something like this:
Keystone Images Plugin Button in action

In your keystone.js:

keystone.init({
...
  'wysiwyg additional plugins': 'images',
  'wysiwyg additional buttons': 'images',
...
});

(If you already have plugins or buttons, that should be a comma-delimited string, e.g. 'first-plugin, images'. See docs.)
(Dimensions won't truly resize the image for you, but just input those dimensions to the image tag that it inserts. However dimensions do get auto-populated by the size of the image.)

@jeffreypriebe
Copy link
Contributor

There are several efforts to provide more file upload options. None have been completely folded into Keystone yet. Most require various levels of core integration and need core code shuffled.

@jacargentina
Copy link
Contributor Author

Yeah, i think may be i should wait for those efforts to be merged. Or stick to a "own served image" somehow with the "images" plugin you first mentioned.

@jacargentina
Copy link
Contributor Author

@jeffreypriebe I've thought of writing some plugin for tinymce to serve image ulrs from my own keystonejs server... but...

http://www.tinymce.com/wiki.php/Plugin:image

It seems it is already there? I mean, i can use the image_list option

http://www.tinymce.com/wiki.php/Configuration:image_list, under Example of JSON url with images
i read it would be easy to provide an endpoint from keystonejs itself, to return a list from a given directory for example?

@jacargentina jacargentina reopened this Jul 22, 2015
@jeffreypriebe
Copy link
Contributor

That image-list is interesting. I don't see anything in the source that would suggest that it would work but the idea of passing it a script/url that provides images to pick from is enticing.
Is there a demo of that behavior somewhere?

@jacargentina
Copy link
Contributor Author

No demo. Just you have to configure keysonejs like this:

keystone.init({
 ...
  'wysiwyg additional plugins': 'image',
  'wysiwyg additional buttons': 'image',
  'wysiwyg additional options': {
    'image_list' : '/images',
  }
 ...
});

And then implement the /images endpoint like this (in routes/index.js)

// Bind Routes
exports = module.exports = function(app) {
  ....
 app.get('/images', function(req, res, next) {
     // Just an example. Ideally an array would be fetched from disk, database, etc
     res.json([
             {title: 'Dog', value: 'mydog.jpg'},
             {title: 'Cat', value: 'mycat.gif'}
        ]);
 });

  ....
}

@jacargentina
Copy link
Contributor Author

Anyway, i've come to a simple implementation, by using available things like data uri for images https://en.wikipedia.org/wiki/Data_URI_scheme and TinyMCE support for drag/drop or paste images.

So i dont need to save the images on its own files; also, a blog post is somehow like a unit (text + embeded images), somebody can point me disavantages/cons of this model, but i prefer looking at advantages/pros; and also it's VERY easy to forget the problem, and start USING it!

I've simply done this:

Enable pasted images as data-uris on TinyMCE (disabled by default)
keystone.init({
 ...
  'wysiwyg additional plugins': 'paste',
  'wysiwyg additional options': {
    'paste_data_images': true
  }
 ...
});

@jeffreypriebe
Copy link
Contributor

So the image selected gets inserted in the content directly as the data/base64. From the last keystone init code, seems like that behavior is a TinyMCE option.
It would be good to document this as it is another reasonable interim solution (beats the imageupload IMO, and only lacks the upload ability of cloudinary images).

I can't think of any good arguments against the insertion of image data directly.
One might argue image cache reuse (if pointing at an external image, a browser can cache across multiple pages) but that seems unlikely for most images that someone would put into content.

On the plus side, it has a strong performance point of saving another HTTP request (Yahoo! goes as far as recommending inline images with data:URL use for exactly this reason.)

Feels like a clever judo solution. Nicely done.

@jacargentina
Copy link
Contributor Author

Nice... Yahoo supporting... me! ;)
Hope this serves to somebody using keystone too.

@tobeatiger
Copy link

Hi @jacargentina
Just have the same issue and tried your solution, it works great for small images, but if you paste/drag a big image (say more than 1M), the whole content will broken after successfully saved. Not sure it's the limitation of mongoDB or the TinyMCE. Did you have the same issue?

@jacargentina
Copy link
Contributor Author

@tobeatiger never. Maybe i've not used anything so big yet.

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