-1

我为拇指图像制作了这段代码。我在本地有带有 php 5.4.4 的 MAMP,并且此代码有效,但是在带有 php 5.3.19 的服务器上不起作用。

它的工作方式类似于 class_ethumb.php?image=demo.png&size=250

<?php

$thumb = new Thumb;
$image = $_GET['image'];
$size  = $_GET['size'];
$thumb->createThumb($image,$size);

class Thumb {
    public function Thumb($image)
    {
        //Define the Default Size
        $this->defaultSize = 100;
    }

    // type of image example: "jpg","png" or "gif"
    public function setType($image)
    {
        $ext = explode(".",$image);
        $num = count($ext)-1;
        $type = $ext[$num];
        $this->type = $type;

    }

    // get the size of source image
    public function getSize($image)
    {
        switch($this->type) {
            case 'jpg':
                $this->source = @imagecreatefromjpeg($image);
                break;
            case 'png':
                $this->source = @imagecreatefrompng($image);
                break;
            case 'gif':
                $this->source = @imagecreatefromgif($image);
                break;
            default:
                die("Invalid file type");
        }
        $this->imgWidth   = imagesx($this->source);
        $this->imgHeight  = imagesy($this->source);
    }

    public function createThumb($image,$size)
    {
        if(file_exists($image) === TRUE) 
        {   
            // set the type of image
            $this->setType($image);
            // get the original size
            $this->getSize($image);

            // if $size exist
            if(!$size) 
            {
                $width  = $this->defaultSize;
                $height = ($this->defaultSize * $this->imgHeight) / $this->imgWidth;
            }
            else // if not, let set defaultSize
            {
                $width  = $size;
                $height = ($size * $this->imgHeight) / $this->imgWidth;
            }

            // create a image from a true color
            $img = imagecreatetruecolor($width,$height);

            //thumb creation
            ImageCopyResized($img,$this->source,0,0,0,0,$width,$height,$this->imgWidth,$this->imgHeight);

            // let's print the thumb
            switch($this->type) {
            case 'jpg':
                Header("Content-type: image/jpeg");
                imageJpeg($img);
                break;
            case 'png':
                Header("Content-type: image/png");
                imagePng($img);
                break;
            case 'gif':
                Header("Content-type: image/gif");
                imageGif($img);
                break;
            }
        }
        else
        {
            die("File doesn't exist");
        }
    }
}
?> 

有什么帮助吗?谢谢

4

1 回答 1

0

您应该更改代码,以便使用__construct而不是那种样式。您似乎也缺少构造函数的参数。此外,您不应该需要?>文件末尾的

<?php

$thumb = new Thumb();
$image = $_GET['image'];
$size  = $_GET['size'];
$thumb->createThumb($image,$size);

class Thumb {
    public function __construct( )
    {
        //Define the Default Size
        $this->defaultSize = 100;
    }
    // ... Rest of your file
}

您也可以摆脱构造函数,并为defaultSize

<?php
class Thumb {

    private $defaultSize = 100;

    // ... Rest of file
}
于 2013-05-29T22:09:44.463 回答