imagecreatefrompng可以安全地获取外部图像吗


Is imagecreatefrompng safe to fetch external image?

我想知道函数imagecreatefrompng(及其亲属)是否安全?

与中一样,如果我在哪里使用此函数获取外部图像文件,并且图像包含恶意代码,则此代码会在我的服务器上执行吗?

示例:

 <?php
if($_GET['pass'] != 'IamAW') die();
// Accepted Filetypes
$Accepted = array('png');
$File = $_GET['file'];
$Filetype = end(explode('.', $File));
if(!in_array($Filetype, $Accepted)) {
die('Fil-type ikke valid');
}
$im = imagecreatefrompng($File);

header('Content-Type: image/'. $Filetype);
imagepng($im);
imagedestroy($im);
?>

这是可行的,但为什么还要麻烦呢?

你正在吸取一个远程PNG,在内存中解压缩为原始格式,然后重新压缩为PNG,而没有对图像做任何操作。如果它最终成为一个"大"PNG(例如,1280x1024 32位PNG需要5兆字节的内存。

你最好做:

echo file_get_contents($remote_image_url);

为您节省解压缩/重新压缩时间。