2011-04-06 26 views
0

我有一个关于如何在移动文件时过滤图片的问题。我使用uploadify上传图片。我所做的是,在将图像移动到目录之前,代码过滤器会将图像转换为灰度。PHP imagefilter和uploadify

这里是我的代码

if (!empty($_FILES)) { 
    $tempFile = $_FILES['Filedata']['tmp_name']; 
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/'; 
    $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name']; 

    $newImg = imagefilter($tempFile, IMG_FILTER_GRAYSCALE); // This is what I insert 

    move_uploaded_file($newImg,$targetFile); 
    echo "1"; 
} 

的代码是uploadify.php,我只是插入一个过滤器,使其灰阶。请帮帮我。提前致谢。

+0

您的问题是什么? – 2011-04-06 08:30:20

+0

关于如何在将图像移动到目录之前将图像制作成灰度图 – Jorge 2011-04-06 08:32:01

+0

'imagefilter()'适用于需要使用适当的'imagecreatefrom *()'函数首先初始化的图像资源。有关示例,请参见[imagefilter']手册(http://www.php.net/imagefilter)。 – 2011-04-06 08:39:01

回答

0

Imagefilter可以处理图像资源,而不是文件,也可以是布尔而不是新图像。这可能是值得通过the documentation读书,但您需要更改您的代码的东西沿着这些线路

if (!empty($_FILES)) { 
    $tempFile = $_FILES['Filedata']['tmp_name']; 
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/'; 
    $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name']; 

    // Create an image resource - exact method will depend on the image type (PNG, JPEG, etc) 
    $im = imagecreatefrompng($tempFile); 

    // Apply your filter 
    imagefilter($im, IMG_FILTER_GRAYSCALE); 

    // Save your changes 
    imagepng($im, $tempFile); 

    move_uploaded_file($tempFile,$targetFile); 
    echo "1"; 
} 
0

要使用imagefilter你必须首先加载图像。使用GD加载函数之一(如:imagecreatefrompng)。 然后您可以使用加载的图片上的过滤器。顺便检查参数imagefilter(这需要加载图像,而不是图像路径)。以下是一些示例代码(取代您的imagefilter()):

// Check extension of the file, here is example if the file is png, but you have to check for extension and use specified function 
$img = imagecreatefrompng($tempFile); 

if(imagefilter($img, IMG_FILTER_GRAYSCALE)) 
{ 
    // success 
} 
else 
{ 
    // failture 
} 

// Save file as png to $targetFile 
imagepng($img, $targetFile); 

// Destroy useless resource 
imagedestroy($img);