PHP Classes

File: pimg.php

Recommend this page to a friend!
  Classes of Tony Bogdanov   PIMG   pimg.php   Download  
File: pimg.php
Role: Class source
Content type: text/plain
Description: Core class
Class: PIMG
Process images using multiple operations
Author: By
Last change: 1.1
Date: 13 years ago
Size: 8,755 bytes
 

Contents

Class file image Download
<?php
/* This is the core 'image' class which handles image operations with the GD library */
class pimg_image
{
   
/* Defaults */
   
public $defaultWidth = 100;
    public
$defaultHeight = 100;
    public
$defaultFormat = 'jpeg';
   
   
/* Resources */
   
private $image;
    private
$width;
    private
$height;
    private
$log;
   
   
   
   
/***** LOADING SUB MODULES *****/
   
   
   
   
public function __call($mod, $args)
    {
       
// Check if the module exists
       
$mod = basename($mod);
       
$modFile = dirname(__FILE__) . '/' . $mod . '.pimg.php';
       
$mod = 'pimg_' . $mod;
        if (
file_exists($modFile))
        {
           
// Load the module and return an instance
           
require_once($modFile);
            if (!
class_exists($mod))
                exit(
'PIMG module <b>' . $mod . '</b> does not exist or cannot be loaded!');
            else {
               
$mod = new $mod($this);
                return
call_user_func_array(array($mod, 'init'), $args);
            }
        } else
            exit(
'PIMG module <b>' . $mod . '</b> does not exist or cannot be loaded!');
    }
   
   
   
   
/***** INSTATIATING AN IMAGE *****/
   
   
   
    /*
        Create the image instance and load it with the width and height
        @param: $image - the input image to be created, can be string, encoded base64 string, local file, url or scan matrix
        @param: $width - the width of the image processed - used only if there is no image input.
                        - if there is no image, an empty transparent one is created and this width is set to it
                        - if no width is specified, the default one is used
                        - if image input is set, then image width is used
        @param: $height - the height of the image processed - used only if there is no image input.
                        - if there is no image, an empty transparent one is created and this height is set to it
                        - if no height is specified, the default one is used
                        - if image input is set, then image height is used
    */
   
function __construct($image = null, $width = null, $height = null)
    {
       
// Resources
       
$this -> image = $this -> newImage($image, $width, $height);
       
$this -> width = imagesx($this -> image);
       
$this -> height = imagesy($this -> image);
    }
   
   
   
   
/*
        Create a new image (redirection of __construct)
        @result: returns the handle of the new image resource
    */
   
public function newImage($image = null, $width = null, $height = null)
    {
       
// Check if a string is passed as an image
       
$img = @imagecreatefromstring($image);
       
       
$makeFromScan = false;
        if (!
$img)
        {
           
// Check if the image is a string encoded with base64
           
$img = @imagecreatefromstring(base64_decode($image));
           
            if (!
$img)
            {
               
// Check if the image is a local file or an url
               
$img = @imagecreatefromstring(implode('', file($image)));
               
                if (!
$img)
                {
                   
// Check if the image is a scan matrix
                   
if (is_array($image))
                    {
                       
$width = count($image);
                        if (
is_array($image[0]))
                        {
                           
$height = count($image[0]);
                           
$img = @imagecreatetruecolor($width, $height);
                           
                           
// First we need to fill the image with transparency in order for the imagesetpixel to handle transparency
                           
$makeFromScan = true;
                        }
                    }
                   
                   
// There is no valid image passed, generate an empty, transparent one and pass a warning
                   
$this -> setDebug('Image input is invalid, generating empty one', 'notice');
                   
                    if ((int)
$width <> 0 && (int) $height <> 0)
                    {
                       
$img = imagecreatetruecolor($width, $height);
                    } else {
                       
$img = imagecreatetruecolor($this -> defaultWidth, $this -> defaultHeight);
                    }
                   
                   
$filltransparent = true;
                }
            }
        }
       
       
// Keep image transparent
       
imagealphablending($img, true);
       
imagesavealpha($img, true);
       
       
// Fill with transparency if image is empty
       
if (isset($filltransparent))
        {
           
imagealphablending($img, false);
           
$color = imagecolorallocatealpha($img, 0, 0, 0, 127);
           
imagefill($img, 0, 0, $color);
           
imagealphablending($img, true);
        }
       
        if (
$makeFromScan)
        {
           
// Copy the matrix
           
for($i = 0; $i < $width; $i++)
                for(
$j = 0; $j < $height; $j++)
                   
imagesetpixel($img, $i, $j, $image[$i][$j] -> color() -> allocate());
        }
       
       
// Return the image handle
       
return $img;
    }
   
   
   
   
/*
        Destroy the image instance when the class is unset
    */
   
function __destruct()
    {
        @
imagedestroy($this -> image);
    }
   
   
   
   
/***** UPDATING THE IMAGE HANDLE *****/
   
   
   
    /*
        Updates / replaces the pimg with a different one (used in resizing / merging etc.)
        @param: $pimg - new pimg handle
        @result: returns na instance of the updated pimg image class
    */
   
public function update($img, $width, $height)
    {
       
$this -> image = $img;
       
$this -> width = $width;
       
$this -> height = $height;
    }
   
   
   
   
/***** GETTING IMAGE INFO *****/
   
   
   
    /*
        Sets or gets the width color value
        @param: $width
        @result: color handle or width value
    */
   
public function width($width = null)
    {
        if (isset(
$width))
        {
           
$this -> width = $width;
            return
$this;
        }
        else
            return
$this -> width;
    }
   
   
   
   
/*
        Sets or gets the height color value
        @param: $height
        @result: color handle or height value
    */
   
public function height($height = null)
    {
        if (isset(
$height))
        {
           
$this -> height = $height;
            return
$this;
        }
        else
            return
$this -> height;
    }
   
   
   
   
/*
        Returns image handle (resource pointer)
        @result: returns image handle
    */
   
public function handle()
    {
        return
$this -> image;
    }
   
   
   
   
/***** DEBUG *****/
   
   
   
    /*
        Sets debug log
        @param: $msg - the message to add to the debug list
        @param: $type - the type of the error - can be any string (human readable)
    */
   
public function setDebug($msg, $type = 'notice', $caller = __CLASS__)
    {
       
$exp = explode(' ', microtime());
       
$exp = explode('.', $exp[0]);
       
$milliseconds = substr($exp[1], 0, 3);
       
       
$this -> log .= date('d.m.Y - H:i:s.') . $milliseconds . ' @ {' . $caller . '} <b>' . strtoupper(substr($type, 0, 1)) . substr($type, 1) . '</b>: ' . $msg . '<br />';
       
        if (
$type == 'error')
        {
           
$this -> showDebug();
            exit;
        }
    }
   
   
   
   
/*
        Clears the debug log
    */
   
public function clearDebug()
    {
       
$this -> log = '';
    }
   
   
   
   
/*
        Displays debug log
        @result: displays the debug log so far
    */
   
public function showDebug()
    {
        echo
$this -> log;
    }
   
   
   
   
/***** RENDERING *****/
   
   
   
    /*
        Render the final image
        @param: $format - the format for rendering, can be any supported by GD library: jpeg / gif / png etc.
                        - jpg as format is invalid becouse there is imagejpeg function but no imagejpg!
        @param: $location - the location where the file should be stored, if set to null the image will be send to the buffer
        @param: $quality - the quality of the rendered image (only for jpeg format)
    */
   
public function render($format = null, $location = null, $quality = 75)
    {
       
// Check if the format is supported
       
$function = 'image' . $format;
       
        if (
function_exists($function))
        {
            if (!empty(
$location))
            {
               
$function($this -> image, $location, $quality);
            } else {
               
header('Content-type: image/' . $format);
               
$function($this -> image);
            }
        } else {
           
// Format not supported, use default one
           
$this -> setDebug('Format <b>' . $format . '</b> not supported, using default one: <b>' . $this -> defaultFormat . '</b>', 'warning');
           
           
$function = 'image' . $this -> defaultFormat;
           
            if (!empty(
$location))
            {
               
$function($this -> image, $location, $quality);
            } else {
               
header('Content-type: image/' . $this -> defaultFormat);
               
$function($this -> image);
            }
        }
    }
   
   
   
   
/*
        !USES BUFFER CACHING
        Renders the image in the PNG format, but returns the contents instead of outputing it to the buffer.
        This functionality is useful for 'copying' an image (used in pimg_merge). The rendered format is PNG becouse it's the best
        format for keeping all other formats' qualities (lossless, semi-transparent, etc.).
        @result: returns the content of a rendered png image as a string
    */
   
public function renderString()
    {
       
ob_start();
       
imagepng($this -> image);
        return
ob_get_clean();
    }
   
   
       
   
/*
        Renders the image only if no debug log is set
        @param: $format - see render function
        @param: $location - see render function
        @param: $quality - see render function
        @result: displays debug if there is any, otherwise calls render function
    */
   
public function renderDebug($format = null, $location = null, $quality = 75)
    {
        if (empty(
$this -> log))
        {
           
$this -> render($format, $location, $quality);
        } else {
           
$this -> showDebug();
        }
    }
}
?>