【问题标题】:Intervention \ Image \ Exception \ NotReadableException using laravel 4干预\图像\异常\ NotReadableException使用laravel 4
【发布时间】:2014-08-03 09:44:11
【问题描述】:

我正在使用 laravel 4 并安装了 Intervention Image 包。当我在我的代码中使用它时 method ->resize, ->move etc etc... 我有这个 error:

Intervention \ Image \ Exception \ NotReadableException

图片来源不可读

open: /Applications/MAMP/htdocs/myNameProject/vendor/intervention/image/src/Intervention/Image/AbstractSource.php

        break;

        case $this->isFilePath():
            return $this->initFromPath($this->data);
            break;

        default:
            throw new Exception\NotReadableException("Image source not readable");
            break;
    }

如果对您有帮助的话,我还在 MAC OS 上使用 MAMP 和 Sublime Text 3。

这是我在控制器中的代码:

public function upload() {

//***** UPLOAD FILE (on server it's an image but an Url in Database *****//

// get the input file
$file = Image::make('url_Avatar');

//set a register path to the uploaded file
$destinationPath = public_path().'upload/';

//have client extension loaded file and set a random name to the uploaded file, produce a random string of length 32 made up of alphanumeric characters [a-zA-z0-9]
$filename = $destinationPath . '' . str_random(32) . '.' . $file->getClientOriginalExtension();
//$file->move($destinationPath,$filename);

//set $file in order to resize the format and save as an url in database
$file= Image::make($image->getRealPath())->resize('200','200')->save('upload/'.$filename);

//*****VALIDATORS INPUTS and RULES*****
$inputs = Input::all();
$rules = array(
'pseudo' => 'required|between:1,64|unique:profile,pseudo',
//urlAvatar is an url in database but we register as an image on the server
'url_Avatar' => 'required|image|min:1',
);

(我没有向你展示我的视图的重定向,但它在我的控制器的这一部分工作得很好)

这是我的表单代码(使用刀片 laravel 模板):

@extends('layout.default')
@section('title')
Name Of My Project - EditProfile
@stop

@section('content')
{{Form::open(array('url'=>'uploadAvatar','files' => true))}}

<p>
{{Form::label('pseudo','pseudo (*): ')}}
{{Form::text('pseudo',Input::old('nom'))}}
</p>
@if ($errors->has('pseudo'))
<p class='error'> {{ $errors->first('pseudo')}}</p>
@endif
<br>
<br>

<p>
{{Form::label('url_Avatar','Avatar: ')}}
{{Form::file('url_Avatar',Input::old('Url_Avatar'))}}
</p>
@if ($errors->has('url_Avatar'))
<p class='error'> {{ $errors->first('url_Avatar')}}</p>
@endif
<br>
<br>

<p>
{{Form::submit('Validate your avatar')}}
</p>

{{Form::close()}}
@stop

当然我已经安装了Intervention Image包 按照官网image.intervention.io/getting_started/installation强>(网址)。

如何使我的文件“可读”?或解决此错误?

【问题讨论】:

  • 显示您的代码并确保您的图像在服务器上的位置是readable/writable
  • @WereWolf-TheAlpha 我已经编辑了我的代码。如何使我的图像位置readable/writable
  • 你检查答案了吗?
  • @WereWolf-TheAlpha 我检查了你的答案。这部分解决了我的问题。我已经更新了作曲家,并完成了我的代码,并注意提出您的建议。检查我对您的回答的评论。

标签: php image file-upload laravel laravel-4


【解决方案1】:

改变这个:

$file = Image::make('url_Avatar');

到这里:

$file = Input::file('url_Avatar');
// ...
$filename = '...';
Image::make($file->getRealPath())->resize('200','200')->save($filename);

阅读更多关于file on Laravel documentation的信息。

【讨论】:

  • 确实,我换行你建议我改成$file = Input::file('url_Avatar');。我也改变了$file= Image::make($image-&gt;getRealPath())-&gt;resize('200','200')-&gt;save('upload/'.$filename); 变成 Image::make($file-&gt;getRealPath())-&gt;resize('200','200')-&gt;save($filename);。它运作良好。 感谢您的帮助,您的回答指导我制作了此功能。
  • 我也有类似的问题。我还按照您的指出编辑了我的代码,但它仍然给了我相同的错误消息。你能看看the link并帮帮我吗?
  • 我也遇到了同样的问题。我需要从网址制作图像。使用 Input::file() 我们只能从输入类型文件制作图像。但在我的情况下,输入是 url。那我该怎么办?
  • 你试过$image = Image::make('http://someurl.com/image.png')-&gt;save('/path/ImageName.png'); 吗?
【解决方案2】:

如果您在公共路径中使用子文件夹,请使用 chmod 更改对该文件夹的权限 例如 cd public; chmod -Rv 755 public/{your_path_name};

运行

man chmod;

更多详情

【讨论】:

    【解决方案3】:

    解决方案中的这个

    $filename = str_slug($products->name)."-0.jpg";
    $filename_fb = 'fb-'.$filename;
    $filename_tw = 'tw-'.$filename;
    
    $img = Image::make($_FILES['photo']['tmp_name']);
    // resize image
    $img->resize(800, 400);
    // save image
    $img->save($path.'/'.$filename);
    

    【讨论】:

      【解决方案4】:

      我也有同样的问题。当我更改图像驱动程序时,一切正常。

      尝试将图像驱动程序从 app/config/packages/intervention/image/config.php 从 GD 更改为 Imagick

      如果找不到配置文件,请尝试运行以下命令:

      在 Laravel 5 中发布配置

      $ php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"

      在 Laravel 4 中发布配置

      $ php artisan config:发布干预/图像

      配置文件中的示例内容:

      return array(
      
          /*
          |--------------------------------------------------------------------------
          | Image Driver
          |--------------------------------------------------------------------------
          |
          | Intervention Image supports "GD Library" and "Imagick" to process images
          | internally. You may choose one of them according to your PHP
          | configuration. By default PHP's "GD Library" implementation is used.
          |
          | Supported: "gd", "imagick"
          |
          */
      
          'driver' => 'imagick'
      
      );
      

      【讨论】:

        猜你喜欢
        • 2021-04-18
        • 1970-01-01
        • 1970-01-01
        • 2016-11-10
        • 1970-01-01
        • 2015-02-09
        • 1970-01-01
        • 1970-01-01
        • 2019-04-25
        相关资源
        最近更新 更多