file_put_contents 给我一个特定的错误

Posted

技术标签:

【中文标题】file_put_contents 给我一个特定的错误【英文标题】:file_put_contents give me a specific error 【发布时间】:2012-05-15 14:18:55 【问题描述】:

我正在使用 CodeIgniter 开发一个项目。当我尝试从远程服务器下载图像时,出现以下错误:

错误:

Message: file_put_contents(): supplied resource is not a valid stream resource

导致错误的代码:

            $newImg = resizeImageFrontDimensions($uploadData, UPLOAD_DEFAULT_IMGTHUMB50, UPLOAD_DEFAULT_IMGTHUMB50);

            $imageWhiteFilled = addImageWhiteSpaces(UPLOAD_DEFAULT_IMGTHUMB50, $fileType, DIR_SCREENS . $newImg);
            file_put_contents(DIR_SCREENS, $imageWhiteFilled);

在addImageWhiteSpaces中:

function addImageWhiteSpaces($imageSize, $fileType, $fileName)
   
    switch($fileType)
    
        case DEFAULT_IMAGE_FILETYPE_PNG:
            $image1 = imagecreatefrompng($fileName);
            break;
        case DEFAULT_IMAGE_FILETYPE_JPG:
            $image1 = imagecreatefromjpeg($fileName);
            break;
        case DEFAULT_IMAGE_FILETYPE_JPEG:
            $image1 = imagecreatefromjpeg($fileName);
            break;
        case DEFAULT_IMAGE_FILETYPE_GIF:
            $image1 = imagecreatefromgif($fileName);
            break;      
    

    $image1_x       = imagesx($image1);
    $image1_y       = imagesy($image1);

    if($image1_x > 500 && $image1_y > 500)
        return $fileName;
    else 
    
        switch ($imageSize) 
        
            case  UPLOAD_DEFAULT_IMGTHUMB50: 
                $image_bg = imagecreatefrompng(PATH_MEDIA_IMG . 'white_bg_50.png');
                break;
            case  UPLOAD_DEFAULT_IMGTHUMB74:
                $image_bg = imagecreatefrompng(PATH_MEDIA_IMG . 'white_bg_74.png');
                break;
            case UPLOAD_DEFAULT_IMGTHUMB114:
                $image_bg = imagecreatefrompng(PATH_MEDIA_IMG . 'white_bg_114.png');
                break;
            case UPLOAD_DEFAULT_IMGTHUMB300:
                $image_bg = imagecreatefrompng(PATH_MEDIA_IMG . 'white_bg_300.png');
                break;
            case UPLOAD_DEFAULT_IMGTHUMB500:
                $image_bg = imagecreatefrompng(PATH_MEDIA_IMG . 'white_bg_500.png');
                break;
        

        $image_bg_x     = imagesx($image_bg);
        $image_bg_y     = imagesy($image_bg);

        $imageStart_x   = 0;
        $imageStart_y   = 0;

        if($image_bg_y > $image1_y)
            $imageStart_y   = ($image_bg_y - $image1_y) / 2;

        if($image_bg_x > $image1_x)
            $imageStart_x   = ($image_bg_x - $image1_x) / 2;


        imagecopy($image_bg, $image1, $imageStart_x, $imageStart_y, 0, 0, $image1_x, $image1_y);

        // Output the image.
//      header("Content-type: image/png");
//      imagejpeg($image_bg);
        return $image_bg;
    

虽然我收到此错误,但正在下载图像。但是因为它给出了上面的错误,我下载了图像并且不能继续剩下的代码。任何可能的解决方案或理由对我来说都是一个保险库。非常感谢。

【问题讨论】:

您使用的是 GD 而不是 imagemagick,我会将 Imagemagick 标签更改为 GD。 【参考方案1】:

既然您使用的是codeigniter,那么这个怎么样?

http://codeigniter.com/user_guide/libraries/file_uploading.html

http://codeigniter.com/user_guide/libraries/image_lib.html

你为什么要硬编码你正在使用的框架已经提供的东西?

【讨论】:

好吧,让我解释一下,实际上我正在使用框架的替代方案,但这不是表单上传。我想要做的是从服务器下载图像并更改其尺寸和内容,然后将新更改的文件上传回服务器。因此,我不能使用文件上传,但我在 resizeImageFrontDimensions() 函数中使用了 image_lib。

以上是关于file_put_contents 给我一个特定的错误的主要内容,如果未能解决你的问题,请参考以下文章

PHP file_put_contents() 函数

详解PHP file_put_contents() 函数用法示例

file_put_contents 将文件保存到哪里?

Fopen 或 File_Put_Contents 超时选项?

Laravel:file_put_contents() 权限被拒绝——正确的存储/框架/缓存权限?

file_put_content() 文件权限错误