function addImage( &$img, $x, $y, $w = 0, $h = 0, $quality = 75 )
    {
        // add a new image into the current location, as an external object
        // add the image at $x,$y, and with width and height as defined by $w & $h

        // note that this will only work with full colour images and makes them jpg images for display
        // later versions could present lossless image formats if there is interest.

        // there seems to be some problem here in that images that have quality set above 75 do not appear
        // not too sure why this is, but in the meantime I have restricted this to 75.
        if ( $quality > 75 )
        {
            $quality = 75;
        }

        // if the width or height are set to zero, then set the other one based on keeping the image
        // height/width ratio the same, if they are both zero, then give up :)
        $imageWidth = imagesx( $img );
        $imageHeight = imagesy( $img );

        if ( $w <= 0 && $h <= 0 )
        {
            return;
        }
        if ( $w == 0 )
        {
            $w = $h / $imageHeight * $imageWidth;
        }
        if ( $h == 0 )
        {
            $h = $w * $imageHeight / $imageWidth;
        }

        // gotta get the data out of the img..

        // so I write to a temp file, and then read it back.. soo ugly, my apologies.
        $tmpDir = '/tmp';
        $tmpName = tempnam( $tmpDir, 'img' );
        imagejpeg( $img, $tmpName, $quality );

        $fp = fopen( $tmpName, 'rb' );
        if ( $fp )
        {
            $data = '';
            while ( !feof( $fp ) )
            {
                $data .= fread( $fp, 1024 );
            }
            fclose( $fp );
        }
        else
        {
            $error = 1;
            $errormsg = 'trouble opening file';
        }
        unlink( $tmpName );
        $this->addJpegImage_common( $data, $x, $y, $w, $h, $imageWidth, $imageHeight );
    }