Including image tools library

require_once SGL_CORE_DIR . '/Image.php';

and creating image instance.

$image = new SGL_Image();

Note that constructor can accept 2 optional arguments i.e. image name and module name.

$image = new SGL_Image('riga.jpg');

Image name is always set even if we omit it in constructor. In that case it will be autogenerated by


Getting autogenerated image name after creating an instance.

$image = new SGL_Image();
$imageName = $image->getFileName();

As mentioned above a module name can be passed as second optional argument to constructor.

$image = new SGL_Image('riga.jpg', 'mymodule');

Module name affects the path image is stored and looked for. By default, omitting the module name, save path is equal to SGL_UPLOAD_DIR constant value. In case module name is specified the path is as follows

SGL_MOD_DIR . '/' . $moduleName . '/www/images'

Getting current image save path

$image = new SGL_Image();
$savePath = $image->getPath(); // $savePath equals to SGL_UPLOAD_DIR


$image = new SGL_Image(null, 'media');
$savePath = $image->getPath(); // $savePath equals to SGL_MOD_DIR/media/www/images

SGL_Image#getPath() is a special method, which can be called either statically or as instance call. The following example gives the same result as above one.

$savePath = SGL_Image::getPath('media'); // $savePath equals to SGL_MOD_DIR/media/www/images

What for is an ability to specify image path other than SGL_UPLOAD_DIR? In case of Media module it useless, because all images are shown through a wrapper i.e. FileMgr#_cmd_previewMedia(), thus user rights are preserved on it, because it is not possible to access SGL_UPLOAD_DIR folder directly (through HTTP request). If module name is specified symlinking is in action (Media, Ecomm, Cms - these new modules use symlinking to store all their data at one place). So, if you need to build an image gallery module, which does not require setting user permissions on images, worth to try symlinking ability together with image tools. There is one more special method (which can be called both as static or instance call)


which returns URL to image folder.

$image = new SGL_Image(null, 'gallery');
$url = $image->getUrl(); // $url equals to SGL_BASE_URL/gallery/images
$url = SGL_Image::getUrl('gallery'); // $url equals to SGL_BASE_URL/gallery/images

Please note, if module name is not specified in constructor or, in case of static call, as argument for SGL_Image::getUrl(), PEAR_Error object will be returned.

$image = new SGL_Image();
$url = $image->getUrl();
if (PEAR::isError($url)) {
    echo 'module name is not specified';