PHP - 复制 $_FILES 超全局

发布于 2024-12-11 04:37:49 字数 5500 浏览 0 评论 0原文

我有一个经常使用的 PHP 函数来处理图像(调整大小、加水印、转换为灰度等)。我对它很满意,而且效果很好。但是,它被设计为与 $_FILES 超全局一起使用,并接受它作为参数。

我遇到过这样的情况:我的服务器上有一个现有的文件目录,我需要以与处理从表单上传到 $_FILES 数组的文件相同的方式处理该文件。

考虑到使用现有函数是最简单的,我一直在寻找一种复制 $_FILES 超全局的方法,这样我就可以将它传递给我的脚本,但我没有找到完成此操作所需的函数/属性。 (尽管乍一看, getimagesizefilesize 函数看起来可能有帮助)。

任何人都可以建议我需要哪些函数/属性来复制 $_FILES 数组吗? (或者完成我想要做的事情的替代方法?)

为了参考,我使用的图像函数在这里:

function resize_upload ($file, $dest, $maxw = 50, $maxh = 50, $grey = false, $wm = false, $mark = "a/i/watermark.png", $opa = 40) {     
    $allowext = array("gif", "jpg", "png", "jpeg", "bmp");      
    $fileext = strtolower(getExtension($file['name'])); 
    if (!in_array($fileext,$allowext)) {
        echo "Wrong file extension.";
        exit();
    }
    list($width, $height, $imgcon) = getimagesize($file['tmp_name']);   
    if ($file['size'] && ($width > $maxw || $height > $maxh)) {     
        if($file['type'] == "image/pjpeg" || $file['type'] == "image/jpeg"){$newimg = imagecreatefromjpeg($file['tmp_name']);}
        elseif($file['type'] == "image/x-png" || $file['type'] == "image/png"){$newimg = imagecreatefrompng($file['tmp_name']);}
        elseif($file['type'] == "image/gif"){$newimg = imagecreatefromgif($file['tmp_name']);}          
        $ratio = $width/$height;
        if ($ratio < 1) { // Width < Height
            $newheight = $maxh;
            $newwidth = $width * ($maxh/$height);
            if ($newwidth > $maxw) {
                $newheight = $newheight * ($maxw/$newwidth);
                $newwidth = $maxw;              
            }
        } elseif ($ratio == 1) { // Width = Height
            if ($maxw < $maxh) {
                $newheight = $maxw;
                $newwidth = $maxw;
            } elseif ($maxw == $maxh) {
                $newheight = $maxh;
                $newwidth = $maxw;
            } elseif ($maxw > $maxh) {
                $newheight = $maxh;
                $newwidth = $maxh;
            }
        } elseif ($ratio > 1) { // Width > Height           
            $newwidth = $maxw;
            $newheight = $height * ($maxw/$width);
            if ($newheight > $maxh) {
                $newwidth = $newwidth * ($maxh/$newheight);
                $newheight = $maxh;
            }
        }       
        if (function_exists(imagecreatetruecolor)) {$resize = imagecreatetruecolor($newwidth, $newheight);}             
        if (($imgcon == IMAGETYPE_GIF)) {
            $trnprt_indx = imagecolortransparent($newimg);
            if ($trnprt_indx >= 0) {
                $trnprt_color = imagecolorsforindex($newimg, $trnprt_indx);
                $trnprt_indx = imagecolorallocate($resize, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                imagefill($resize, 0, 0, $trnprt_indx);
                imagecolortransparent($resize, $trnprt_indx);
            }
        } elseif ($imgcon == IMAGETYPE_PNG) {
            imagealphablending($resize, false);
            $color = imagecolorallocatealpha($resize, 0, 0, 0, 127);
            imagefill($resize, 0, 0, $color);
            imagesavealpha($resize, true);
        }
        imagecopyresampled($resize, $newimg, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        if ($wm) {
            $watermark = imagecreatefrompng($mark);
            $wm_width = imagesx($watermark);
            $wm_height = imagesy($watermark);
            $destx = $newwidth - $wm_width - 5;
            $desty = $newheight - $wm_height - 5;
            imagecopymerge($resize, $watermark, $destx, $desty, 0, 0, $wm_width, $wm_height, $opa);
            imagedestroy($watermark);
        }
        $filename = random_name().".".$fileext;
        if ($grey) {imagefilter($resize, IMG_FILTER_GRAYSCALE);}
        if($file['type'] == "image/pjpeg" || $file['type'] == "image/jpeg"){$new = imagejpeg($resize, $dest."/".$filename, 100);}
        elseif($file['type'] == "image/x-png" || $file['type'] == "image/png"){$new = imagepng($resize, $dest."/".$filename, 0);}
        elseif($file['type'] == "image/gif"){$new = imagegif($resize, $dest."/".$filename);}        
        imagedestroy($resize);
        imagedestroy($newimg);      
        return $filename;
    } elseif ($file['size']) {
        $filename = random_name().".".getExtension($file['name']);
        if ($grey) {
            if($file['type'] == "image/pjpeg" || $file['type'] == "image/jpeg"){$newimg = imagecreatefromjpeg($file['tmp_name']);}
            elseif($file['type'] == "image/x-png" || $file['type'] == "image/png"){$newimg = imagecreatefrompng($file['tmp_name']);}
            elseif($file['type'] == "image/gif"){$newimg = imagecreatefromgif($file['tmp_name']);}
            imagefilter($newimg, IMG_FILTER_GRAYSCALE);
            if($file['type'] == "image/pjpeg" || $file['type'] == "image/jpeg"){imagejpeg($newimg, $dest."/".$filename);}
            elseif($file['type'] == "image/x-png" || $file['type'] == "image/png"){imagepng($newimg, $dest."/".$filename);}
            elseif($file['type'] == "image/gif"){imagegif($newimg, $dest."/".$filename);}
            imagedestroy($newimg);
            return $filename;
        } else {
            $upload = file_upload($file, $dest);
            return $upload;
        }
    }
}

I have a PHP function that I use regularly for working with images (resizing, watermarking, converting to grayscale, etc). I am happy with it and it works well. However, it is designed to work with the $_FILES superglobal, and accepts it as a parameter.

I've run into a situation where I have an existing directory of files on my server that I need to process in the same way as I do for files uploaded from a form into the $_FILES array.

Figuring it would be easiest to work with my existing function, I have been looking for a way to duplicate the $_FILES superglobal, so I can pass it to my script, but I am not finding the functions/properties I need to accomplish this. (Although, at a glance, the getimagesize and filesize functions looks like they may help).

Can anyone advise on what functions/properties I would need to duplicate the $_FILES array? (Or an alternate way to accomplish what I am trying to do?)

For reference's sake, the image function I use is here:

function resize_upload ($file, $dest, $maxw = 50, $maxh = 50, $grey = false, $wm = false, $mark = "a/i/watermark.png", $opa = 40) {     
    $allowext = array("gif", "jpg", "png", "jpeg", "bmp");      
    $fileext = strtolower(getExtension($file['name'])); 
    if (!in_array($fileext,$allowext)) {
        echo "Wrong file extension.";
        exit();
    }
    list($width, $height, $imgcon) = getimagesize($file['tmp_name']);   
    if ($file['size'] && ($width > $maxw || $height > $maxh)) {     
        if($file['type'] == "image/pjpeg" || $file['type'] == "image/jpeg"){$newimg = imagecreatefromjpeg($file['tmp_name']);}
        elseif($file['type'] == "image/x-png" || $file['type'] == "image/png"){$newimg = imagecreatefrompng($file['tmp_name']);}
        elseif($file['type'] == "image/gif"){$newimg = imagecreatefromgif($file['tmp_name']);}          
        $ratio = $width/$height;
        if ($ratio < 1) { // Width < Height
            $newheight = $maxh;
            $newwidth = $width * ($maxh/$height);
            if ($newwidth > $maxw) {
                $newheight = $newheight * ($maxw/$newwidth);
                $newwidth = $maxw;              
            }
        } elseif ($ratio == 1) { // Width = Height
            if ($maxw < $maxh) {
                $newheight = $maxw;
                $newwidth = $maxw;
            } elseif ($maxw == $maxh) {
                $newheight = $maxh;
                $newwidth = $maxw;
            } elseif ($maxw > $maxh) {
                $newheight = $maxh;
                $newwidth = $maxh;
            }
        } elseif ($ratio > 1) { // Width > Height           
            $newwidth = $maxw;
            $newheight = $height * ($maxw/$width);
            if ($newheight > $maxh) {
                $newwidth = $newwidth * ($maxh/$newheight);
                $newheight = $maxh;
            }
        }       
        if (function_exists(imagecreatetruecolor)) {$resize = imagecreatetruecolor($newwidth, $newheight);}             
        if (($imgcon == IMAGETYPE_GIF)) {
            $trnprt_indx = imagecolortransparent($newimg);
            if ($trnprt_indx >= 0) {
                $trnprt_color = imagecolorsforindex($newimg, $trnprt_indx);
                $trnprt_indx = imagecolorallocate($resize, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                imagefill($resize, 0, 0, $trnprt_indx);
                imagecolortransparent($resize, $trnprt_indx);
            }
        } elseif ($imgcon == IMAGETYPE_PNG) {
            imagealphablending($resize, false);
            $color = imagecolorallocatealpha($resize, 0, 0, 0, 127);
            imagefill($resize, 0, 0, $color);
            imagesavealpha($resize, true);
        }
        imagecopyresampled($resize, $newimg, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        if ($wm) {
            $watermark = imagecreatefrompng($mark);
            $wm_width = imagesx($watermark);
            $wm_height = imagesy($watermark);
            $destx = $newwidth - $wm_width - 5;
            $desty = $newheight - $wm_height - 5;
            imagecopymerge($resize, $watermark, $destx, $desty, 0, 0, $wm_width, $wm_height, $opa);
            imagedestroy($watermark);
        }
        $filename = random_name().".".$fileext;
        if ($grey) {imagefilter($resize, IMG_FILTER_GRAYSCALE);}
        if($file['type'] == "image/pjpeg" || $file['type'] == "image/jpeg"){$new = imagejpeg($resize, $dest."/".$filename, 100);}
        elseif($file['type'] == "image/x-png" || $file['type'] == "image/png"){$new = imagepng($resize, $dest."/".$filename, 0);}
        elseif($file['type'] == "image/gif"){$new = imagegif($resize, $dest."/".$filename);}        
        imagedestroy($resize);
        imagedestroy($newimg);      
        return $filename;
    } elseif ($file['size']) {
        $filename = random_name().".".getExtension($file['name']);
        if ($grey) {
            if($file['type'] == "image/pjpeg" || $file['type'] == "image/jpeg"){$newimg = imagecreatefromjpeg($file['tmp_name']);}
            elseif($file['type'] == "image/x-png" || $file['type'] == "image/png"){$newimg = imagecreatefrompng($file['tmp_name']);}
            elseif($file['type'] == "image/gif"){$newimg = imagecreatefromgif($file['tmp_name']);}
            imagefilter($newimg, IMG_FILTER_GRAYSCALE);
            if($file['type'] == "image/pjpeg" || $file['type'] == "image/jpeg"){imagejpeg($newimg, $dest."/".$filename);}
            elseif($file['type'] == "image/x-png" || $file['type'] == "image/png"){imagepng($newimg, $dest."/".$filename);}
            elseif($file['type'] == "image/gif"){imagegif($newimg, $dest."/".$filename);}
            imagedestroy($newimg);
            return $filename;
        } else {
            $upload = file_upload($file, $dest);
            return $upload;
        }
    }
}

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

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

发布评论

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

评论(1

音盲 2024-12-18 04:37:50

$_FILES 数组包含上传文件的嵌套数组。这个嵌套数组有 5 个键。对于每个键,我解释它应该包含什么,以及要使用什么函数:

  • 名称:文件的名称,对此条目使用basename()函数
  • type:文件的 mime 类型,对于设置为“image/png”、“image/jpeg”等的图像
  • tmp_name:实际文件的路径,此处应设置图像的路径
  • 错误:这表示上传时发生错误,根据您的情况,您可以将其设置为 0 表示没有错误
  • 大小:文件的大小(以字节为单位),因此您可以对您的图像使用filesize()函数

示例:

$_FILES = array('image' => array(
    'name' => basename('/path/to/image.png'),
    'type' => 'image/png',
    'tmp_name' => '/path/to/image.png',
    'error' => 0,
    'size' => filesize('/path/to/image.png')
));

如果您想一次处理多个文件,您应该意识到$_FILES数组的结构与您期望的不同这种情况,请参见PHP 文档中的此注释

The $_FILES array contains a nested array for an uploaded file. This nested array has 5 keys. For each key I explain what it should contain, and what function to use:

  • name: the name of the file, use the basename() function for this entry
  • type: the mime type of the file, for images set to 'image/png', 'image/jpeg', etc
  • tmp_name: the path to the actual file, here you should set the path to your images
  • error: this indicates that an error occured with the upload, in your case you can set it to 0 for no error
  • size: the size of the file in bytes, so you can use the filesize() function for your image

An example:

$_FILES = array('image' => array(
    'name' => basename('/path/to/image.png'),
    'type' => 'image/png',
    'tmp_name' => '/path/to/image.png',
    'error' => 0,
    'size' => filesize('/path/to/image.png')
));

If you want to process multiple files at once, you should be aware that the structure of the $_FILES array is different than what you would expect in this case, see this comment in the PHP docs.

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