RsImage
Image generation and manipulation
Image Generator
The image generator is an abstraction for php-gd images with some operations on it. It does not only support basic features like resizing or moving an image, it also supports adding and subtracting (WiP) of images (layers).
By now it is not possible to use mask-layers. It is planed to implement this via the subLayer() method. The layer will the be substracted from the image, this means, solid parts are cut from the image while transparent parts stay.
Internal structure
By now there are only three conrete and one abstract class. Each concrete class has its own constructor for easier use.
- Redspark_RsImage: An abstract image class with methods and attributes working on the image ressource.
- Redspark_RsImage_Empty: A simple implementation for creating a new, empty image.
- Redspark_RsImage_Text: Allows you to create text with imagemagick.
- Redspark_RsImage_File: Load an image directly from a given file.
Working with images
<?php
/* Load button image */
$btn = new Redspark_RsImage_File($path_to_button_file);
/* Load some fancy add-on */
$addon = new Redspark_RsImage_File($path_to_addon_file);
/* Scale-up the addon to the size of the button */
$addon->resize($btn->getWidth(),$btn->getHeight(),Redspark_RsImage::RESIZE_SCALE)
/* Lay addon over the button */
$btn->addLayer($addon);
/* print the image */
echo $btn->getSource(Redspark_RsImage::TYPE_PNG);
?>
Creating a text image
Text creation is very simple. you can either use the open-source font "Vera", delivered with RedSpark or specify the path to another ttf file.
<?php
/* Create text layer */
$txt = new Redspark_RsImage_Text($this->_text, $options);
/* Create text layer with red, big text (20px) in my font */
$options = Array(
Redspark_RsImage_Text::INIT_OPTION_FONT_FILE => 'fonts/MyFont.ttf',
Redspark_RsImage_Text::INIT_OPTION_FONT_SIZE => 20,
Redspark_RsImage_Text::INIT_OPTION_FONT_COLOR => Array(255,0,0,0),
);
$txt = new Redspark_RsImage_Text($this->_text, $options);
?>
Overlapping images
If you want to overlap images, you first have to make sure, the underlying image has the right size. Otherwise the layer will be cutted to the size of the background.
<?php
/* Create background */
$img = new Redspark_RsImage_File('bg.png');
/* Create layer */
$img2 = new Redspark_RsImage_File('layer1.png');
/* Add the layer to the background */
$img->addLayer($img2);
/* print the image */
echo $btn->getSource(Redspark_RsImage::TYPE_PNG);
?>
ToDos
- Implement all croping algorithms.
- Test crop algorithm in all 3 forms square, wide, high.
- More error handling (libs not installed, width <=0)
- subLayer() for transparency masks