gallery script - filetypes不仅仅是:jpg

时间:2015-11-29 22:53:49

标签: php jpeg gallery

我有这个脚本。这是我的图库脚本的一部分。以下代码是* .php文件,其中上传时处理图像。任何人都可以帮我如何将此代码更改为版本,用户不仅可以上传jpg文件,还可以上传png,gif等等吗?

if (!defined('InGallery')) exit('Access denied');

class Image {
    public $ImageId = null;
    public $Name = null;
    public $Details = null;
    public $Date = null;

    private $_Elements = array();
    private $_Comments = array();

    function __construct($id) {      
        try {
            if (is_int($id)) {
                $this->ImageId = $id;
                /** @var PDO */
                global $Connection;
                $query = $Connection->prepare("SELECT Name, Details, Date FROM gall_images WHERE ImageId = :id");
                $query->bindValue(":id", $this->ImageId, PDO::PARAM_INT);
                $query->execute();
                $result = $query->fetch(PDO::FETCH_ASSOC);
                $query->closeCursor();

                $this->Name = $result['Name'];
                $this->Details = $result['Details'];
                $this->Date = $result['Date']; 
            }
            else if (is_array($id)) {
                $this->Name = $id['Name'];
                $this->Details = $id['Details'];
                $this->Date = time(); 
            }       
        } catch (Exception $exc) {
            echo "[ERROR]Connection to the database.";
        }
    }

    function GetThumb() {
        return PATH."/images/thumbs/{$this->ImageId}.jpg";
    }

    function GetOriginal() {
        return PATH."/images/full/{$this->ImageId}.jpg";
    }

    function GetDate() {
        return date('d-m-Y H:i', $this->Date);
    }

    function GetComments() {
        try {
            include_once dirname(__FILE__).'/class_comment.php';
            /** @var PDO */
            global $Connection;
            $query = $Connection->prepare("SELECT CommentId FROM gall_comments WHERE ImageId = :id ORDER BY Date DESC");
            $query->bindValue(":id", $this->ImageId, PDO::PARAM_INT);
            $query->execute();          

            foreach ($query as $value) {
                $this->_Comments[] = new Comment((int)$value['CommentId']);
            }

            $query->closeCursor();
        } catch (Exception $exc) {
            echo "[ERROR]Comments error.";
        }

        return $this->_Comments;
    }

    function GetElements() {
        try {
            include_once dirname(__FILE__).'/class_element.php';
            /** @var PDO */
            global $Connection;
            $query = $Connection->prepare("SELECT ElementId FROM gall_elements WHERE ImageId = :id ORDER BY gall_elements.Order");
            $query->bindValue(":id", $this->ImageId, PDO::PARAM_INT);
            $query->execute();          

            foreach ($query as $value) {
                $this->_Elements[] = new Element((int)$value['ElementId']);
            }

            $query->closeCursor();
        } catch (Exception $exc) {
            echo "[ERROR]Elements error.";
        }

        return $this->_Elements;
    }

    function Modify() {
        try {
            include_once dirname(__FILE__).'/class_element.php';
            /** @var PDO */
            global $Connection;
            $query = $Connection->prepare("UPDATE gall_images SET Name = :Name, Details = :Details, Date = :Date WHERE ImageId = :ImageId");

            $query->bindValue(":Name", $this->Name, PDO::PARAM_STR);
            $query->bindValue(":Details", $this->Details, PDO::PARAM_STR);
            $query->bindValue(":Date", $this->Date, PDO::PARAM_INT);
            $query->bindValue(":ImageId", $this->ImageId, PDO::PARAM_INT);
            $query->execute();
            $query->closeCursor();

            return 1;
        } catch (Exception $exc) {
            echo "[ERROR]Elements error.";
        }
    }

    function Remove() {
        try {
            foreach ($this->GetComments() as $value) {
                $value->Remove();
            }

            foreach ($this->GetElements() as $value) {
                $value->Remove();
            }

            /** @var PDO */
            global $Connection;
            $query = $Connection->prepare("DELETE FROM gall_images WHERE ImageId = :ImageId");
            $query->bindValue(":ImageId", $this->ImageId, PDO::PARAM_INT);
            $query->execute();
            $query->closeCursor();

            $path = dirname(__FILE__).'/../images/full/'.$this->ImageId.'.jpg';
            $pathThumbs = dirname(__FILE__).'/../images/thumbs/'.$this->ImageId.'.jpg';
            @unlink($path);
            @unlink($pathThumbs);

            return 1;
        } catch (Exception $exc) {
            echo "[ERROR]Remove";
        }
    }

    function Add() {
        try {      
            /** @var PDO */
            global $Connection;
            $query = $Connection->prepare("INSERT INTO gall_images VALUES(NULL, :Name, :Details, :Date)");
            $query->bindValue(":Name", $this->Name, PDO::PARAM_STR);
            $query->bindValue(":Details", $this->Details, PDO::PARAM_STR);
            $query->bindValue(":Date", $this->Date, PDO::PARAM_INT);
            $query->execute();
            $this->ImageId = $Connection->lastInsertId();
            $query->closeCursor();

            return 1;
        } catch (Exception $exc) {
            echo "[ERROR]Add";
        }
    }

    function CreateThumb() {
        $path = dirname(__FILE__).'/../images/full/'.$this->ImageId.'.jpg';
        $pathThumbs = dirname(__FILE__).'/../images/thumbs/'.$this->ImageId.'.jpg';
        if (file_exists($path) && is_readable($path)) {      
            $info = pathinfo($path);
            if (strtolower($info['extension']) == 'jpg') {
                $img = imagecreatefromjpeg($path);
                $width = imagesx($img);
                $height = imagesy($img);

                $max_width = 170;
                $max_height = 215;

                $scale = max($max_width/$width, $max_height/$height);

                $new_width  = ceil($scale*$width);
                $new_height = ceil($scale*$height);


                //$new_width = $ThumbWidth;
                //$new_height = floor($height * ( $new_width / $width ));

                $tmp_img = imagecreatetruecolor($new_width, $new_height);
                imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                imagejpeg($tmp_img, $pathThumbs);
            }
        }
        else {
            throw new Exception("Cannot load image {$this->_ImageName}");
        }
    }
}

0 个答案:

没有答案