我尝试按照有关插入图像的教程并调整其大小,但我遇到一个问题,显示图像源不可读。
我正在使用PHP,Laravel 5框架和mysql。当我运行我的代码时,我会停在Image::make
这是我的控制器代码:
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\FoodRequest;
use App\Http\Controllers\Controller;
use App\Photo;
use Image;
use App\Restaurant;
use Symfony\Component\HttpFoundation\File\UploadedFile;
public function addPhoto($zip, $street, Request $request)
{
$this->validate($request, [
'photo' => 'required|mimes:jpg,jpeg,png,bmp'
]);
$photo = $this->makePhoto($request->file('photo'));
Restaurant::locatedAt($zip, $street)->addPhoto($photo);
}
protected function makePhoto(UploadedFile $file)
{
return Photo::named($file->getClientOriginalName())
->move($file);
}
这是照片代码:
public static function named($name)
{
return (new static)->saveAs($name);
}
protected function saveAs($name)
{
$this->name = sprintf("%s-%s", time(), $name);
$this->path = sprintf("%s-%s", $this->baseDir, $this->name);
$this->thumbnail_path = sprintf("%s/tn-%s", $this->baseDir, $this->name);
return $this;
}
public function move(UploadedFile $file)
{
$file->move($this->baseDir, $this->name);
$this->makeThumbnail();
return $this;
}
protected function makeThumbnail()
{
Image::make($this->path)
->fit(200)
->save($this->thumbnail_path);
}
答案 0 :(得分:1)
我做了同样的教程,你应该这样做:
Image::make($this->path.$this->name)->resize(128,
128)->save($this->thumbnail_path.$this->name);
而不是这样做:
Image::make($this->path)->fit(200)->save($this->thumbnail_path);
答案 1 :(得分:0)
这是我自己的代码中的一个例子,我写了我的照片的路径
$destinationpath = 'img/' . $propertyid;
$frontpage = 'img/' . $propertyid. '/frontpage/' ;
$gallery = 'img/' . $propertyid. '/gallery/' ;
$thumbpath = 'img/' .$propertyid .'/thumbnails/';
将图像文件移动到干预操作的位置 代码将处理图像,更改大小等。我们将保存结果 处理各自的文件夹,然后删除此图像。
$ image-> move($ destinationpath,$ filename);
$dbImg = new Picture;
$dbImg->property_id = $propertyid;
$dbImg->name = $filename;
$dbImg->save();