2011-04-06 29 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);