php Intervention \ Image \ Exception \ NotReadableException 使用 laravel 4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24195028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:12:35  来源:igfitidea点击:

Intervention \ Image \ Exception \ NotReadableException using laravel 4

phpimagefile-uploadlaravellaravel-4

提问by french_dev

I'm using laravel 4and I installed the Intervention Image package. When I'm using it in my code whith method->resize, ->moveetc etc etc... I have this error:

我正在使用laravel 4并安装了Intervention Image 包。当我在我的代码中使用它时,方法- >调整大小, - >移动等等等等......我有这个错误:

Intervention \ Image \ Exception \ NotReadableException

Image source not readable

图片来源不可读

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

I'm also using MAMP and Sublime Text 3 on MAC OS if it could help you.

如果可以帮助您,我也在MAC OS 上使用MAMP 和 Sublime Text 3。

This is my code in my controller:

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

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',
);

(I don't show you the redirect of my view, but it's worked fine for this section of my controller)

(我没有向您展示我的视图的重定向,但它适用于我的控制器的这一部分)

here is my form code (using blade laravel template):

这是我的表单代码(使用刀片 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

Of course I have installed Intervention Image packagefollowingthe official website image.intervention.io/getting_started/installation(url).

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

How can I make my file "readable"?or resolve this error?

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

回答by The Alpha

Change this:

改变这个:

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

To this:

对此:

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

Read more about file on Laravel documentation.

在 Laravel 文档中阅读更多关于文件的信息

回答by nikssa23

I have the same problem. When I change image driver everything works fine.

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

Try to change image driver from app/config/packages/intervention/image/config.php from GD to Imagick

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

If you cant find config file try to run commands below:

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

Publish configuration in Laravel 5

在 Laravel 5 中发布配置

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

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

Publish configuration in Laravel 4

在 Laravel 4 中发布配置

$ php artisan config:publish intervention/image

$ php artisan 配置:发布干预/图像

Example content from config file:

配置文件中的示例内容:

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'

);

回答by Jon Awoyele

if you use a sub-folder in your public path, use chmod to change the permission on that folder e.g cd public; chmod -Rv 755 public/{your_path_name};

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

Run

man chmod;

for more details

更多细节

回答by Mas Hary

this in solution

这在解决方案中

$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);