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

发布于 2024-08-01 18:31:56 字数 1709 浏览 7 评论 0原文

下面是我为我的 php 照片上传脚本制作的水印功能。 我很好奇是否有更好的方法来执行检查文件类型的部分,请注意我必须使用该部分代码两次

<?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;
}
?>

Below is a watermark function I made for my php photo upload script. I am curious if there is a better way of doing the parts that check for the file type, notice I had to use that part of code 2 times

<?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;
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

银河中√捞星星 2024-08-08 18:31:56

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

PHP 并不总是支持所有这些图像类型,因此您可能需要使用 function_exists() 在打电话给他们之前检查一下。 调用不存在的函数是 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;
}

如果您安装了 exif 扩展,则可以使用 exif_imagetype() 自动检测图像的类型。

另一个更优雅但也包含更多代码的选项是使用多态性:

<?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;
    }
}

我意识到您可能更喜欢第一个。 :)

You could use dynamic function calls, as illustrated below. This code is subtly different from yours since it returns if an invalid image type is provided rather than assume that it's jpeg. If you insist on that behavior it should be easy enough to change, tough.

It's not always the case that all these image types are supported by PHP so you might want to use function_exists() to check for that before calling them. Calling a non-existant function is a fatal error in 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;
}

If you have the exif extension installed you could use exif_imagetype() to automatically detect the type of the image.

Another option that's a bit more elegant, but also contains more code is to use polymorfism:

<?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;
    }
}

I realize that you'll probably prefer the first one. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文