{"id":18103289,"url":"https://github.com/mtgrosser/rszr","last_synced_at":"2025-05-07T15:21:55.900Z","repository":{"id":1617759,"uuid":"43078950","full_name":"mtgrosser/rszr","owner":"mtgrosser","description":"Fast image resizer for Ruby","archived":false,"fork":false,"pushed_at":"2024-02-06T16:22:04.000Z","size":3163,"stargazers_count":33,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-28T16:54:21.821Z","etag":null,"topics":["activestorage","autorotation","image-filtering","image-processing","image-resizer","image-resizing","imlib2","rails","ruby"],"latest_commit_sha":null,"homepage":"https://mtgrosser.github.io/rszr/resizing.html","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mtgrosser.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-24T16:29:14.000Z","updated_at":"2024-12-24T10:02:13.000Z","dependencies_parsed_at":"2023-01-13T11:16:11.602Z","dependency_job_id":null,"html_url":"https://github.com/mtgrosser/rszr","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtgrosser%2Frszr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtgrosser%2Frszr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtgrosser%2Frszr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtgrosser%2Frszr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mtgrosser","download_url":"https://codeload.github.com/mtgrosser/rszr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252902740,"owners_count":21822295,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activestorage","autorotation","image-filtering","image-processing","image-resizer","image-resizing","imlib2","rails","ruby"],"created_at":"2024-10-31T22:11:25.768Z","updated_at":"2025-05-07T15:21:55.879Z","avatar_url":"https://github.com/mtgrosser.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Gem Version](https://badge.fury.io/rb/rszr.svg)](http://badge.fury.io/rb/rszr) [![build](https://github.com/mtgrosser/rszr/actions/workflows/build.yml/badge.svg)](https://github.com/mtgrosser/rszr/actions/workflows/build.yml)\n# Rszr - fast image resizer for Ruby\n\nRszr is an image resizer for Ruby based on the Imlib2 library.\nIt is faster and consumes less memory than MiniMagick, GD2 and VIPS, and comes with an optional drop-in interface for Rails ActiveStorage image processing.\n\n## Installation\n\nIn your Gemfile:\n\n```ruby\ngem 'rszr'\n```\n\n### Imlib2\n\nRszr requires the `Imlib2` library to do the heavy lifting.\n\n#### OS X\n\nUsing homebrew:\n\n```bash\nbrew install imlib2\n```\n\n#### Linux\n\nUsing your favourite package manager:\n\n##### RedHat-based\n\n```bash\nyum install imlib2 imlib2-devel\n```\n\n##### Debian-based\n\n```bash\napt-get install libimlib2 libimlib2-dev\n```\n\n## Usage\n\n```ruby\n# bounding box 400x300\nimage = Rszr::Image.load('image.jpg')\nimage.resize!(400, 300)\n\n# save it\nimage.save('resized.jpg')\n\n# save it as PNG\nimage.save('resized.png')\n\n# save without extension in given format\nimage.save('resized', format: 'png')\n```\n\n### Image info\n```ruby\nimage.width =\u003e 400\nimage.height =\u003e 300\nimage.dimensions =\u003e [400, 300]\nimage.format =\u003e \"jpeg\"\nimage.alpha? =\u003e false\nimage[0, 0] =\u003e \u003cRszr::Color::RGBA @red=38, @green=115, @blue=141, @alpha=255\u003e\nimage[0, 0].to_hex =\u003e \"#26738dff\"\nimage[0, 0].to_hex(alpha: false) =\u003e \"#26738d\"\n```\n\n### Transformations\n\nFor each transformation, there is a bang! and non-bang method.\nThe bang method changes the image in place, while the non-bang method\ncreates a copy of the image in memory.\n\n#### Resizing\n\n```ruby\n# auto height\nimage.resize(400, :auto)\n\n# auto width\nimage.resize(:auto, 300)\n\n# scale factor\nimage.resize(0.5)\n\n# resize to fill\nimage.resize(400, 300, crop: true)\n\n# resize to fill with gravity\n# where gravity in [:n, :nw, :w, :sw, :w, :se, :e, :ne, :center]\nimage.resize(400, 300, crop: gravity)\n\n# save memory, do not duplicate instance\nimage.resize!(400, :auto)\n```\n\nCheck out the [full list and demo of resize options](https://mtgrosser.github.io/rszr/resizing.html)!\n\n#### Other transformations\n\n```ruby\n# crop\nimage.crop(200, 200, 100, 100)\n\n# rotate three times 90 deg clockwise\nimage.turn!(3)\n\n# rotate one time 90 deg counterclockwise\nimage.turn!(-1)\n\n# rotate by arbitrary angle\nimage.rotate(45)\n\n# flip vertically\nimage.flip\n\n# flop horizontally\nimage.flop\n\n# initialize copy\nimage.dup\n```\n\n### Image generation\n\n```ruby\n# generate new image with transparent background\nimage = Rszr::Image.new(500, 500, alpha: true, background: Rszr::Color::Transparent)\n\n# fill image with 50% opacity\nimage.fill!(Rszr::Color::RGBA.new(0, 206, 209, 50))\n\n# define a color gradient\ngradient = Rszr::Color::Gradient.new do |g|\n  g.point 0, 255, 250, 205, 50\n  g.point 0.5, 135, 206, 250\n  g.point 1, Rszr::Color::White\nend\n\n# draw a rectangle and fill it using the gradient with 45°\nimage.rectangle!(gradient.to_fill(45), 100, 100, 300, 300)\n```\n\n### Colors\n\n```ruby\n# pre-defined colors\nRszr::Color::White\nRszr::Color::Black\nRszr::Color::Transparent\n\n# RGB\ncolor = Rszr::Color.rgba(255, 250, 50)\ncolor.red =\u003e 255\ncolor.green =\u003e 250\ncolor.blue =\u003e 50\ncolor.alpha =\u003e 255\ncolor.cyan =\u003e 0\ncolor.magenta =\u003e 5\ncolor.yellow =\u003e 205\n\n# RGBA\nRszr::Color.rgba(255, 250, 50, 255)\n\n# CMY\nRszr::Color.cmya(0, 5, 205)\n\n# CMYA\nRszr::Color.cmya(0, 5, 205, 255)\n```\n\n### Color gradients\n\n```ruby\n# three-color linear gradient with changing opacity\ngradient = Rszr::Color::Gradient.new do |g|\n  g.point 0, 255, 250, 205, 50\n  g.point 0.5, 135, 206, 250\n  g.point 1, Rszr::Color::White\nend\n\n# alternative syntax\ngradient = Rszr::Color::Gradient.new(0 =\u003e \"#fffacd32\", 0.5 =\u003e \"#87cefa\", 1 =\u003e \"#fff\")\n\n# generate fill with 45° angle\nfill = gradient.to_fill(45)\n\n# use as image background\nimage = Rszr::Image.new(500, 500, background: fill)\n```\n\n### Watermarking and image blending\n\n```ruby\n# load logo\nlogo = Rszr::Image.load('logo.png')\n\n# load image\nimage = Rszr::Image.load('image.jpg')\n\n# enable alpha channel\nimage.alpha = true\n\n# blend it onto the image at position (10, 10)\nimage.blend!(logo, 10, 10)\n\n# blending modes:\n# - copy (default)\n# - add\n# - subtract\n# - reshade\nimage.blend(logo, 10, 10, mode: :subtract)\n```\n\n### Filters\n\nFilters also support bang! and non-bang methods.\n\n```ruby\n# sharpen image by pixel radius\nimage.sharpen!(1)\n\n# blur image by pixel radius\nimage.blur!(1)\n\n# brighten\nimage.brighten(0.1)\n\n# darken\nimage.brighten(-0.1)\n\n# contrast\nimage.contrast(0.5)\n\n# gamma\nimage.gamma(1.1)\n\n# convert to grayscale (automatic mode)\nimage.desaturate\n\n# convert to grayscale with mode\nimage.desaturate(:lightness)\nimage.desaturate(:luminosity)\nimage.desaturate(:average)\n```\n\n### Image auto orientation\n\nAuto-rotation is supported for JPEG and TIFF files that include the necessary\nEXIF metadata.\n\n```ruby\n# load and autorotate\nimage = Rszr::Image.load('image.jpg', autorotate: true)\n```\n\nTo enable autorotation by default:\n\n```ruby\n# auto-rotate by default, for Rails apps put this into an initializer\nRszr.autorotate = true\n```\n\n### Creating interlaced PNG and progressive JPEG images\n\nIn order to save interlaced PNGs and progressive JPEGs, set the `interlace` option to `true`:\n\n```ruby\nimage.save('interlaced.png', interlace: true)\n```\n\nSaving progressive JPEG images requires `imlib2` \u003e= 1.8.1.\n\nFor EL8, there are pre-built RPMs provided by the [onrooby repo](http://downloads.onrooby.com/repo/el/8/x86_64/).\n\n## Rails / ActiveStorage interface\n\nRszr provides a drop-in interface to the [image_processing](https://github.com/janko/image_processing) gem.\nIt is faster than both `mini_magick` and `vips` and way easier to install than the latter.\n\n```ruby\n# Gemfile\ngem 'image_processing'\ngem 'rszr'\n\n# config/initializers/rszr.rb\nrequire 'rszr/image_processing'\n\n# config/application.rb\nconfig.active_storage.variant_processor = :rszr\n```\n\nWhen creating image variants, you can use all of Rszr's transformation methods:\n\n```erb\n\u003c%= image_tag user.avatar.variant(resize_to_fit: [300, 200]) %\u003e\n```\n\n## Loading from and saving to memory\n\nThe `Imlib2` library is mainly file-oriented and doesn't provide a way of loading\nthe undecoded image from a memory buffer. Therefore, the functionality is\nimplemented on the Ruby side of the gem, writing the memory buffer to a Tempfile.\nCurrently, this local write cannot be avoided.\n\n```ruby\nimage = Rszr::Image.load_data(binary_data)\n\ndata = image.save_data(format: :jpeg)\n```\n\n## Thread safety\n\nAs of version 0.5.0, Rszr is thread safe through Ruby's global VM lock.\nUse of any previous versions in a threaded environment is discouraged.\n\n## Speed\n\nResizing a 1500x997 JPEG image to 800x532, 500 times:\n![Speed](https://github.com/mtgrosser/rszr/blob/master/benchmark/speed.png)\n\n\nLibrary         | Time\n----------------|-----------\nMiniMagick      | 27.0 s\nGD2             | 28.2 s\nVIPS            | 13.6 s\nRszr            |  7.9 s\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtgrosser%2Frszr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmtgrosser%2Frszr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtgrosser%2Frszr/lists"}