tinyimg 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 974d95b03df2d7a90c238049b4083912464b8616
4
- data.tar.gz: ee647517f46360743859de3c5aca8b02be3ee59c
3
+ metadata.gz: d6c767cf7d39a737f942dc653bed088967678c01
4
+ data.tar.gz: 872f0520067cb5ff2d529f3d5595a5f48771e578
5
5
  SHA512:
6
- metadata.gz: fe80301f446341e671ec3d116f912fea97744817c1ddf4d9923b7c95e50c32d5df3b1e9dde81da975e318b1de49042ee516f37adce89b15eb7a89b518c9bf31e
7
- data.tar.gz: 4fffdcd8475a7f547ec3fdd1d9856ce078cbbda8920f2ac357237e6f750ca9c6488e52056c7727eb2a5c1cc33c6b628d50806ffe5083334ad2606c9a13220e26
6
+ metadata.gz: ebdfdef6986a4d77a18d62de00d391751d78c51530d86bf63f6f69c66574c45ad82d1b1df603add2375168f12ae02f4d954217083f4111593c4cea0bf1f922bf
7
+ data.tar.gz: 53babb3f77c2c543d48f5c6bb24cbf9a058ec598fc56075d5afdae04417e0ab49adcbba1980ba07a938cf4aae98c3aa239004a95970e0c8362fcb6ff050dd7f3
data/README.md CHANGED
@@ -55,6 +55,14 @@ image.crop!(x: 20, y: 20, width: 50, height: 50)
55
55
  image.crop!(width: image.height)
56
56
  ```
57
57
 
58
+ Set the transparent color of your PNG image if you're about to save it to a JPEG, as JPEG doesn't support
59
+ transparency:
60
+
61
+ ```ruby
62
+ # Make the transparent color red
63
+ image.fill_transparent_color!(255, 0, 0)
64
+ ```
65
+
58
66
  Then get an image back:
59
67
 
60
68
  ```ruby
@@ -71,17 +79,18 @@ image.height # => 80
71
79
  image.dimensions # => [120, 80]
72
80
  ```
73
81
 
74
- You can also use the non-! versions of the operation methods: `resize_exact`, `resize`, `resize_to_fit`, `resize_to_fill` and `crop`.
75
- These create a new image in memory and return it, leaving the old image untouched. This is useful if you want
82
+ You can also use the non-! versions of the operation methods: `resize_exact`, `resize`, `resize_to_fit`, `resize_to_fill`, `crop`
83
+ and `fill_transparent_color`. These create a new image in memory and return it, leaving the old image untouched. This is useful if you want
76
84
  to resize an original image to multiple sizes. Using these methods will take more memory.
77
85
 
78
86
  ## Examples
79
87
 
80
- Take an uploaded file that's a PNG or JPEG, save the original as a JPEG, then resize to create a thumbnail and save that too:
88
+ Take an uploaded file that's a PNG or JPEG, make the transparent color white, save the original as a JPEG, then resize to create a thumbnail and save that too:
81
89
 
82
90
  ```ruby
83
91
  Tinyimg
84
92
  .from_io(params[:uploaded_file])
93
+ .fill_transparent_color!(255, 255, 255)
85
94
  .save("#{path}/full_size.jpg")
86
95
  .resize_to_fit!(100, 100)
87
96
  .save("#{path}/thumbnail.jpg")
@@ -96,4 +105,4 @@ user.update!(thumbnail_image: data)
96
105
 
97
106
  ## Author and licence
98
107
 
99
- Copyright 2015 Roger Nesbitt. Licenced under the MIT licence.
108
+ Copyright 2015-2016 Roger Nesbitt. Licenced under the MIT licence.
@@ -240,6 +240,40 @@ VALUE resize_exact_bang(VALUE self, VALUE width_value, VALUE height_value)
240
240
  return self;
241
241
  }
242
242
 
243
+ VALUE fill_transparent_color_bang(VALUE self, VALUE red_value, VALUE green_value, VALUE blue_value)
244
+ {
245
+ gdImagePtr image_in, image_out;
246
+ int red, green, blue;
247
+ int color;
248
+
249
+ Check_Type(red_value, T_FIXNUM);
250
+ Check_Type(green_value, T_FIXNUM);
251
+ Check_Type(blue_value, T_FIXNUM);
252
+
253
+ red = FIX2INT(red_value);
254
+ green = FIX2INT(green_value);
255
+ blue = FIX2INT(blue_value);
256
+
257
+ if (red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255) {
258
+ rb_raise(get_error_class(self), "red, green and blue must be integers between 0 and 255");
259
+ }
260
+
261
+ image_in = get_image_data(self);
262
+ image_out = gdImageCreateTrueColor(gdImageSX(image_in), gdImageSY(image_in));
263
+
264
+ color = gdImageColorAllocate(image_out, red, green, blue);
265
+ gdImageFilledRectangle(image_out, 0, 0, gdImageSX(image_in), gdImageSY(image_in), color);
266
+
267
+ gdImageCopy(
268
+ image_out, image_in, 0, 0, 0, 0,
269
+ gdImageSX(image_in), gdImageSY(image_in)
270
+ );
271
+
272
+ set_image_data(self, image_out);
273
+
274
+ return self;
275
+ }
276
+
243
277
  VALUE internal_crop_bang(VALUE self, VALUE x_value, VALUE y_value, VALUE width_value, VALUE height_value)
244
278
  {
245
279
  gdImagePtr image_in, image_out;
@@ -292,6 +326,7 @@ void Init_tinyimg()
292
326
  rb_define_class_under(cTinyimg, "Error", rb_const_get(rb_cObject, rb_intern("StandardError")));
293
327
 
294
328
  rb_define_method(cTinyimg, "resize_exact!", resize_exact_bang, 2);
329
+ rb_define_method(cTinyimg, "fill_transparent_color!", fill_transparent_color_bang, 3);
295
330
  rb_define_method(cTinyimg, "to_jpeg", to_jpeg, -1);
296
331
  rb_define_method(cTinyimg, "to_png", to_png, -1);
297
332
  rb_define_private_method(cTinyimg, "initialize_copy", initialize_copy, -1);
@@ -76,6 +76,14 @@ class Tinyimg
76
76
  internal_crop!(x, y, width, height)
77
77
  end
78
78
 
79
+ # Implemented in C
80
+ # def fill_transparent_color!(red, green, blue)
81
+ # end
82
+
83
+ def fill_transparent_color(red, green, blue)
84
+ dup.fill_transparent_color!(red, green, blue)
85
+ end
86
+
79
87
  def save(filename)
80
88
  if respond_to?(:save_to_file)
81
89
  save_to_file(filename)
@@ -159,6 +159,20 @@ RSpec.describe Tinyimg do
159
159
  end
160
160
  end
161
161
 
162
+ describe "#fill_transparent_color" do
163
+ it "returns an image of the same size without crashing (we can't test this easily)" do
164
+ result = sample.fill_transparent_color(255, 0, 0)
165
+ expect(result.dimensions).to eq [200, 153]
166
+ end
167
+ end
168
+
169
+ describe "#fill_transparent_color!" do
170
+ it "returns an image of the same size without crashing (we can't test this easily)" do
171
+ sample.fill_transparent_color!(255, 0, 0)
172
+ expect(sample.dimensions).to eq [200, 153]
173
+ end
174
+ end
175
+
162
176
  describe "#save" do
163
177
  it "saves a JPEG" do
164
178
  begin
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = 'tinyimg'
3
- gem.version = '0.1.2'
3
+ gem.version = '0.1.3'
4
4
  gem.summary = "Tiny and fast JPEG/PNG cropping and resizing"
5
5
  gem.description = "Convert between JPEG/PNG, crop and resize images, either all in memory or via disk. Only requires libgd to function."
6
6
  gem.has_rdoc = false
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tinyimg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roger Nesbitt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-19 00:00:00.000000000 Z
11
+ date: 2016-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  version: '0'
65
65
  requirements: []
66
66
  rubyforge_project:
67
- rubygems_version: 2.4.8
67
+ rubygems_version: 2.5.1
68
68
  signing_key:
69
69
  specification_version: 4
70
70
  summary: Tiny and fast JPEG/PNG cropping and resizing