【问题标题】:PHP imagefilter and uploadifyPHP 图像过滤和上传
【发布时间】:2011-04-06 08:29:38
【问题描述】:

我有一个关于如何在移动文件时过滤图像的问题。我使用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,我只是插入了一个过滤器使其成为灰度。请帮我。提前致谢。

【问题讨论】:

  • 关于如何在将图像移动到目录之前将其设置为灰度
  • imagefilter() 适用于您需要首先使用适当的imagecreatefrom*() 函数初始化的图像资源。有关示例,请参见 manual on imagefilter

标签: php gd uploadify imagefilter


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    要使用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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      相关资源
      最近更新 更多