Giter Site home page Giter Site logo

Height and Width speed up about dragonfly HOT 7 CLOSED

markevans avatar markevans commented on August 20, 2024
Height and Width speed up

from dragonfly.

Comments (7)

markevans avatar markevans commented on August 20, 2024

not really - it takes as long as imagemagick takes to resize the image (with "convert"), then analyse it with "identify". where are you doing it?

from dragonfly.

gerrit avatar gerrit commented on August 20, 2024

I've got similar issues where I have a page that contains a large number of variable-height images whose dimensions need to be in the HTML for clientside layout javascript to work. The are all resized to a known width though, so I can use the aspect ratio of the original image (fast to get as it's just a call to identify) to calculate the height in my code.

If both dimensions of the resized image are known as for the OP, we could even skip the call to identify entirely.

It would be nice if this kind of optimisation could be pushed down into dragonfly so that for simple geometry strings where at least one dimension is known (which I imagine to be the vast majority of cases) it can transparently give a faster answer.

from dragonfly.

slbug avatar slbug commented on August 20, 2024

i think best way is add image caching(save to some folder all converted images with metadata). and add ability to rebuild (or clear) this cache.

from dragonfly.

markevans avatar markevans commented on August 20, 2024

gerrit - I think the best thing is to add a column on your model
"<attachment_name>_aspect_ratio"

You shouldn't be doing any calls to identify when rendering the html page I think.

Given that you know the width, you can just get the height of the thumbnails from (width/aspect_ratio).round

Unless you can think of a simple way of doing it, I don't think it should be up to dragonfly to do this because it would greatly complicate the analyser, which by design is supposed to be very simple.

from dragonfly.

gerrit avatar gerrit commented on August 20, 2024

Have a look at some code where I'm doing this: https://github.com/gerrit/radiant-assets-extension/blob/f6136d7716b9e5dabee4b10376fcc200866fd427/app/models/asset_tags.rb#L177-189

So, pretty much as you're describing it.

If you don't want to change the analyser interface (understandable as it's a documented extension point), the optimisation could take place in Job::OverrideInstanceMethods where we could simply define width/height methods.

from dragonfly.

markevans avatar markevans commented on August 20, 2024

yeh that could be a place, though width and height are specific to the imagemagick configuration and shouldn't be hard-coded onto there, so we'd have to find a way to add it as part of the configuration.

if it's ok I'm not going to work on this because adding the xxx_aspect_ratio column should work ok and I don't want to add to much magic/complexity.

Feel free to fork it and add it yourself though and if you can find a clean way of doing it we can add it to core. I'm going to close this issue if that's ok - thanks for the suggestions!

from dragonfly.

mattlenz avatar mattlenz commented on August 20, 2024

Just wanted to share a fairly straightfoward solution for those Googling.

Rather than trying to parse the geometry string yourself and calculating dimensions based on aspect ratios (messy), you can lean on Dragonfly's convenient methods and cache the result. Thus there'll only ever be a single request to the slow method. You could even call the method beforehand (ieafter_create) to avoid any slow request on the user's dime.

Here's what I arrived at (in Rails, I'm sure you can adapt this).

Memoize dragonfly image dimensions
https://gist.github.com/3375756

class User < ActiveRecord::Base
  image_accessor :avatar

  serialize :avatar_geometries, Hash

  def avatar_dimensions(geometry)
    unless avatar_geometries.has_key?(geometry)
      geometries[geometry] = {
        thumb = avatar.thumb(geometry)

        width: thumb.width,
        height: thumb.height
      }

      save
    end

    OpenStruct.new(geometries[geometry])
  end
end

An OpenStruct is returned for convenience, ie user.avatar_dimensions('200x300').width

from dragonfly.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.