class jpgimage {

        var $ih;
        var $imagefile;

        function open($filename) {
                $this->ih = imagecreatefromjpeg($filename);

                if ( !$this->ih ) {
                        return false;
                } else {
                        $imagefile = $filename;
                        return true;
                }
        }

        // kicks back image handle for user to mod..
        function get_imagehandle() {
                if ( $this->ih ) {
                        return $this->ih;
                } else {
                        return false;
                }
        }
       
        // returns opened image's width
        function get_width() {
                return imagesx($this->ih);
        }
       
        // Returns opened image's height
        function get_height() {
                return imagesy($this->ih);
        }

        function resize_aspect($max_size) {

                if ( $this->get_width() > $max_size || $this->get_height() > $max_size ) {

                        $new_width = $max_size;
                        $new_height = $max_size;

                        // wider
                        if ( $this->get_width() > $this->get_height() ) {
                                $new_height = ($new_width/$this->get_width()) * $this->get_height();
                        // taller
                        } else {
                                $new_width = ($new_height/$this->get_height()) * $this->get_width();
                        }

                        $dh = imagecreatetruecolor($new_width,$new_height) or die("Cannot create true color ($new_width,$new_height)");
                        imagecopyresampled( $dh, $this->ih, 0,0,0,0,$new_width,$new_height,$this->get_width(),$this->get_height() );

                        // clear current image handle
                        imagedestroy($this->ih);

                        // set new handle to image handle
                        $this->ih = $dh;

                }

        }

        function output($quality = 100) {
                header('Content-type: image/jpeg');
                imagejpeg($this->ih,"",$quality);
        }

        function save($quality = 100) {
                imagejpeg($this->ih,$this->imagefile,$quality);
        }

        function clear() {
                imagedestroy($this->ih);
        }

}