这个水印功能可以改进吗(PHP)

这个水印功能可以改进吗(PHP),php,watermark,Php,Watermark,下面是我为php照片上传脚本制作的水印函数。我很好奇是否有更好的方法来检查文件类型的部分,注意我不得不使用这部分代码2次 <?PHP function watermark($source_file,$source_width,$source_height,$image_type) { //first image below will be large then small in 1line if/else $watermarksize = ($source_width &g

下面是我为php照片上传脚本制作的水印函数。我很好奇是否有更好的方法来检查文件类型的部分,注意我不得不使用这部分代码2次

<?PHP
function watermark($source_file,$source_width,$source_height,$image_type) {
    //first image below will be large then small in 1line if/else
    $watermarksize = ($source_width > 300) ? '../images/fpwatermark.gif' : '../images/fpwatermark.gif';
    //now add the watermark to the image.
    $watermark = imagecreatefromgif($watermarksize);
    switch ($image_type) {
        case 'gif':
            $image = imagecreatefromgif($source_file);
            break;
        case 'jpg':
            $image = imagecreatefromjpeg($source_file);
            break;
        case 'png':
            $image = imagecreatefrompng($source_file);
            break;
        default:
            $image = imagecreatefromjpeg($source_file);
            break;
    }
    //get the dimensions of the watermark
    list($water_width, $water_height) = getimagesize($watermarksize);
    // Water mark process
    $x = $source_width - $water_width - 8; //horizontal position
    $y = $source_height - $water_height - 8; //vertical positon
    // imagesy($image) can be the source images width
    imagecopymerge($image, $watermark, $x, $y, 0, 0, $water_width, $water_height, 65);
    switch ($image_type) {
        case 'gif':
            imagegif($image, $source_file, 90);
            break;
        case 'jpg':
            imagejpeg($image, $source_file, 90);
            break;
        case 'png':
            imagepng($image, $source_file, 90);
            break;
        default:
            imagejpeg($image, $source_file, 90);
            break;
    }
    imagedestroy($image);
    return $source_file;
}
?>

您可以使用动态函数调用,如下所示。这段代码与您的代码略有不同,因为如果提供了无效的图像类型,而不是假定它是jpeg,它将返回。如果你坚持这种行为,那么改变应该很容易,但很难

并非所有这些图像类型都受PHP支持,因此您可能希望在调用它们之前使用来检查它们。调用不存在的函数在PHP中是一个致命错误

<?PHP
function watermark($source_file,$source_width,$source_height,$image_type) {
    $validTypes = array("gif" => "gif", "jpg" => "jpeg", "jpeg" => "jpeg", "png" => "png");
    if (!array_key_exists($image_type, $validTypes)) {
        trigger_error("Not a valid image type", E_USER_WARNING);
        return NULL;
    }

    $inFunc = "imagecreatefrom" . $validTypes[$image_type];
    $outFunc = "image" . $validTypes[$image_type];

    //first image below will be large then small in 1line if/else
    $watermarksize = ($source_width > 300) ? '../images/fpwatermark.gif' : '../images/fpwatermark.gif';
    //now add the watermark to the image.
    $watermark = imagecreatefromgif($watermarksize);

    // open the image using the assigned function
    $image = $inFunc($source_file);

    //get the dimensions of the watermark
    list($water_width, $water_height) = getimagesize($watermarksize);
    // Water mark process
    $x = $source_width - $water_width - 8; //horizontal position
    $y = $source_height - $water_height - 8; //vertical positon
    // imagesy($image) can be the source images width
    imagecopymerge($image, $watermark, $x, $y, 0, 0, $water_width, $water_height, 65);

    // save the image
    $outFunc($image, $source_file, 90);

    imagedestroy($image);
    return $source_file;
}

谢谢,看起来好多了!此外,我将把文件类型从它在anks上使用的页面传递到函数中,是的,这让我很困惑,但很高兴看到并作为将来的参考,谢谢ImageCreateFromString(file_get_contents($source_file));
<?php

interface Codec {
    public function open($file);
    public function save($img);
}

class JPEGCodec implements Codec {
    public function open($file) { return imagecreatefromjpeg($file); }
    public function save($img, $out_file) { imagejpeg($img, $out_file, 90); }
}

class PNGCodec implements Codec {
    public function open($file) { return imagecreatefrompng($file); }
    public function save($img, $out_file) { imagepng($img, $out_file, 9); }
}

class GIFCodec implements Codec {
    public function open($file) { return imagecreatefromgif($file); }
    public function save($img, $out_file) { imagegif($img, $out_file); }
}

class WatermarkException extends Exception {}

class Watermark
{
    private $_codecs = array();

    public function __construct()
    {
        $this->_codecs["jpg"] = $this->_codecs["jpeg"] = new JPEGCodec();
        $this->_codecs["png"] = new PNGCodec();
        $this->_codecs["gif"] = new GIFCodec();
    }

    function watermark($source_file,$source_width,$source_height,$image_type) {
        if (!array_key_exists($image_type, $this->_codecs)) {
            throw new WatermarkException("Not a valid image type");
        }

        $codec = $this->_codecs[$image_type];

        //first image below will be large then small in 1line if/else
        $watermarksize = ($source_width > 300) ? '../images/fpwatermark.gif' : '../images/fpwatermark.gif';
        //now add the watermark to the image.
        $watermark = imagecreatefromgif($watermarksize);

        // load image
        $image = $codec->open($source_file);

        //get the dimensions of the watermark
        list($water_width, $water_height) = getimagesize($watermarksize);
        // Water mark process
        $x = $source_width - $water_width - 8; //horizontal position
        $y = $source_height - $water_height - 8; //vertical positon
        // imagesy($image) can be the source images width
        imagecopymerge($image, $watermark, $x, $y, 0, 0, $water_width, $water_height, 65);

        // save image
        $codec->save($image, $source_file);

        imagedestroy($image);
        return $source_file;
    }
}