Class: Tinyimg
- Inherits:
-
Object
- Object
- Tinyimg
- Defined in:
- lib/tinyimg.rb,
ext/tinyimg/tinyimg.c
Defined Under Namespace
Classes: Image
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
Instance Method Summary collapse
- #crop(**kwargs) ⇒ Object
- #crop!(x: 0, y: 0, width: nil, height: nil) ⇒ Object
- #dimensions ⇒ Object
-
#fill_transparent_color(red, green, blue) ⇒ Object
Implemented in C def fill_transparent_color!(red, green, blue) end.
- #fill_transparent_color!(red_value, green_value, blue_value) ⇒ Object
- #resize(*args) ⇒ Object
- #resize!(*args) ⇒ Object
- #resize_exact(width, height) ⇒ Object
- #resize_exact!(width_value, height_value) ⇒ Object
- #resize_to_fill(new_width, new_height) ⇒ Object
- #resize_to_fill!(new_width, new_height) ⇒ Object
-
#resize_to_fit(new_width, new_height) ⇒ Object
Implemented in C def resize_exact!(width, height) end.
- #resize_to_fit!(new_width, new_height) ⇒ Object
- #save(filename) ⇒ Object
- #to_jpeg(*args) ⇒ Object
- #to_png(*args) ⇒ Object
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
4 5 6 |
# File 'lib/tinyimg.rb', line 4 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
4 5 6 |
# File 'lib/tinyimg.rb', line 4 def width @width end |
Class Method Details
.from_file(filename) ⇒ Object
8 9 10 |
# File 'lib/tinyimg.rb', line 8 def self.from_file(filename) new(:filename, filename) end |
.from_io(io) ⇒ Object
12 13 14 |
# File 'lib/tinyimg.rb', line 12 def self.from_io(io) from_string(io.read) end |
.from_string(data) ⇒ Object
16 17 18 |
# File 'lib/tinyimg.rb', line 16 def self.from_string(data) new(:string, data) end |
Instance Method Details
#crop(**kwargs) ⇒ Object
68 69 70 |
# File 'lib/tinyimg.rb', line 68 def crop(**kwargs) dup.crop!(**kwargs) end |
#crop!(x: 0, y: 0, width: nil, height: nil) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/tinyimg.rb', line 72 def crop!(x: 0, y: 0, width: nil, height: nil) width ||= self.width height ||= self.height internal_crop!(x, y, width, height) end |
#dimensions ⇒ Object
20 21 22 |
# File 'lib/tinyimg.rb', line 20 def dimensions [width, height] end |
#fill_transparent_color(red, green, blue) ⇒ Object
Implemented in C def fill_transparent_color!(red, green, blue) end
83 84 85 |
# File 'lib/tinyimg.rb', line 83 def fill_transparent_color(red, green, blue) dup.fill_transparent_color!(red, green, blue) end |
#fill_transparent_color!(red_value, green_value, blue_value) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'ext/tinyimg/tinyimg.c', line 249
VALUE fill_transparent_color_bang(VALUE self, VALUE red_value, VALUE green_value, VALUE blue_value)
{
gdImagePtr image_in, image_out;
int red, green, blue;
int color;
Check_Type(red_value, T_FIXNUM);
Check_Type(green_value, T_FIXNUM);
Check_Type(blue_value, T_FIXNUM);
red = FIX2INT(red_value);
green = FIX2INT(green_value);
blue = FIX2INT(blue_value);
if (red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255) {
rb_raise(get_error_class(self), "red, green and blue must be integers between 0 and 255");
}
image_in = get_image_data(self);
image_out = gdImageCreateTrueColor(gdImageSX(image_in), gdImageSY(image_in));
color = gdImageColorAllocate(image_out, red, green, blue);
gdImageFilledRectangle(image_out, 0, 0, gdImageSX(image_in), gdImageSY(image_in), color);
gdImageCopy(
image_out, image_in, 0, 0, 0, 0,
gdImageSX(image_in), gdImageSY(image_in)
);
set_image_data(self, image_out);
return self;
}
|
#resize(*args) ⇒ Object
24 25 26 |
# File 'lib/tinyimg.rb', line 24 def resize(*args) dup.resize!(*args) end |
#resize!(*args) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/tinyimg.rb', line 28 def resize!(*args) case args.map(&:class) when [Integer, Integer] resize_exact!(*args) when [Hash] width, height = convert_hash_to_exact_dimensions(args.first) resize_exact!(width, height) else raise Error, "#resize and #resize! accept either (width, height) or a hash with :width and/or :height keys" end end |
#resize_exact(width, height) ⇒ Object
40 41 42 |
# File 'lib/tinyimg.rb', line 40 def resize_exact(width, height) dup.resize_exact!(width, height) end |
#resize_exact!(width_value, height_value) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'ext/tinyimg/tinyimg.c', line 217
VALUE resize_exact_bang(VALUE self, VALUE width_value, VALUE height_value)
{
gdImagePtr image_in, image_out;
int width, height;
Check_Type(width_value, T_FIXNUM);
Check_Type(height_value, T_FIXNUM);
width = FIX2INT(width_value);
height = FIX2INT(height_value);
if (width <= 0 || height <= 0) {
rb_raise(get_error_class(self), "width and height must both be positive integers");
}
image_in = get_image_data(self);
image_out = gdImageCreateTrueColor(width, height);
set_alpha(image_out);
gdImageCopyResampled(
image_out, image_in, 0, 0, 0, 0,
gdImageSX(image_out), gdImageSY(image_out),
gdImageSX(image_in), gdImageSY(image_in)
);
set_image_data(self, image_out);
retrieve_image_dimensions(self);
return self;
}
|
#resize_to_fill(new_width, new_height) ⇒ Object
58 59 60 |
# File 'lib/tinyimg.rb', line 58 def resize_to_fill(new_width, new_height) dup.resize_to_fill!(new_width, new_height) end |
#resize_to_fill!(new_width, new_height) ⇒ Object
62 63 64 65 66 |
# File 'lib/tinyimg.rb', line 62 def resize_to_fill!(new_width, new_height) resize_to_fit_or_fill!(new_width, new_height) do |old_ratio, new_ratio| old_ratio < new_ratio end end |
#resize_to_fit(new_width, new_height) ⇒ Object
Implemented in C def resize_exact!(width, height) end
48 49 50 |
# File 'lib/tinyimg.rb', line 48 def resize_to_fit(new_width, new_height) dup.resize_to_fit!(new_width, new_height) end |
#resize_to_fit!(new_width, new_height) ⇒ Object
52 53 54 55 56 |
# File 'lib/tinyimg.rb', line 52 def resize_to_fit!(new_width, new_height) resize_to_fit_or_fill!(new_width, new_height) do |old_ratio, new_ratio| old_ratio > new_ratio end end |
#save(filename) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/tinyimg.rb', line 87 def save(filename) if respond_to?(:save_to_file) save_to_file(filename) else data = case determine_by_extension(filename) when :jpeg then to_jpeg when :png then to_png end File.write(filename, data) end self end |
#to_jpeg(*args) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'ext/tinyimg/tinyimg.c', line 145
VALUE to_jpeg(int argc, VALUE *argv, VALUE self)
{
gdImagePtr image;
char *image_data;
int size, quality;
VALUE quality_value;
rb_scan_args(argc, argv, "01", &quality_value);
if (NIL_P(quality_value)) {
quality = -1;
}
else {
Check_Type(quality_value, T_FIXNUM);
quality = FIX2INT(quality_value);
}
if (quality < -1 || quality > 100) {
rb_raise(get_error_class(self), "Quality must be between 0 and 100, or -1 for default");
}
image = get_image_data(self);
image_data = (char *) gdImageJpegPtr(image, &size, quality);
if (!image_data) {
rb_raise(get_error_class(self), "Unknown error occurred while trying to build a JPEG");
}
VALUE output = rb_str_new(image_data, size);
gdFree(image_data);
return output;
}
|
#to_png(*args) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'ext/tinyimg/tinyimg.c', line 178
VALUE to_png(int argc, VALUE *argv, VALUE self)
{
gdImagePtr image;
char *image_data;
int size;
VALUE compression_value;
int compression;
rb_scan_args(argc, argv, "01", &compression_value);
image = get_image_data(self);
if (NIL_P(compression_value)) {
image_data = (char *) gdImagePngPtr(image, &size);
}
else {
Check_Type(compression_value, T_FIXNUM);
compression = FIX2INT(compression_value);
if (compression < 0 || compression > 9) {
rb_raise(get_error_class(self), "Compression must be between 0 and 9");
}
#ifdef HAVE_GDIMAGEPNGPTREX
image_data = (char *) gdImagePngPtrEx(image, &size, compression);
#else
image_data = (char *) gdImagePngPtr(image, &size);
#endif
}
if (!image_data) {
rb_raise(get_error_class(self), "Unknown error occurred while trying to build a PNG");
}
VALUE output = rb_str_new(image_data, size);
gdFree(image_data);
return output;
}
|