Php 如何将我的上载文件重命名为数据库中的url,然后在另一个页面中调整上载的大小
我在一个与Laravel 4合作的项目中工作,我想将上传文件重命名为数据库中的url,我不知道怎么做。我所有的验证代码都运行良好,我只需要找到如何在url名称中重命名上载的文件,然后在另一个页面/布局/表单(blade.php)中调整上载文件的大小(200x200)Php 如何将我的上载文件重命名为数据库中的url,然后在另一个页面中调整上载的大小,php,file-upload,laravel-4,image-resizing,Php,File Upload,Laravel 4,Image Resizing,我在一个与Laravel 4合作的项目中工作,我想将上传文件重命名为数据库中的url,我不知道怎么做。我所有的验证代码都运行良好,我只需要找到如何在url名称中重命名上载的文件,然后在另一个页面/布局/表单(blade.php)中调整上载文件的大小(200x200) 'Image' => 'Intervention\Image\Facades\Image' 以下是我的表单代码(createBand.blade.php): 我的代码运行良好,创建的乐队已在我的数据库中注册,但我想在我的数据
'Image' => 'Intervention\Image\Facades\Image'
以下是我的表单代码(createBand.blade.php):
我的代码运行良好,创建的乐队已在我的数据库中注册,但我想在我的数据库中以url格式注册乐队头像。我的数据库中为此创建了一个字段
提前谢谢。我发现了它的工作原理:
事实上,我需要上传软件包干预图像。你可以在这里找到他
之后,在此处执行重定向:。
只需按照laravel的说明进行操作:
安装:
安装干预映像的最佳方法是使用Composer快速方便地安装。在windows命令提示符下运行Composer
'Image' => 'Intervention\Image\Facades\Image'
-通过Composer.json中的Composer需要软件包:
"intervention/image": "2.*"
然后,运行Composer来安装或更新新需求
$ php composer.phar install
或
现在我可以要求vendor/autoload.php文件自动加载库PSR-0
示例
// include composer autoload
require 'vendor/autoload.php';
// import the Intervention Image Manager Class
use Intervention\Image\ImageManagerStatic as Image;
// and you are ready to go ...
$image = Image::make('public/foo.jpg')->resize(300, 200);
Image类还具有可选的Laravel 4支持
对于Laravel集成:
安装干预映像后,打开Laravel配置文件config/app.php
,并添加以下行。
在$providers
数组中,为该软件包添加服务提供商
'Intervention\Image\ImageServiceProvider'
将此包的外观添加到$alias数组中
'Image' => 'Intervention\Image\Facades\Image'
现在,图像类将由Laravel自动加载
配置:
默认情况下,干预图像使用PHP的GD库扩展来处理所有图像。如果我想切换到Imagick,我可以通过运行以下artisan命令将配置文件拉入我的应用程序
$ php artisan config:publish intervention/image
此命令将配置文件复制到app/config/packages/intervention/image/config.php
,在这里我可以在本地更改应用程序的驱动程序设置
如果您需要有关PHP的GD库或Imagick的更多信息,请点击laravel入门链接
现在,这里有我的控制器代码将我的上传文件保存到数据库的URL中,并将图像裁剪成200x200格式:
//*****UPLOAD FILE (on server it's an image, on the DB it's an url*****
$file = Input::file('url_Avatar');
$destinationPath = 'upload/';
$filename = str_random(32) . '.' . $file->getClientOriginalExtension();
//This produces a random string of length 24 made up of alphanumeric characters [a-zA-z0-9]
//$extension =$file->getClientOriginalExtension();
$upload_success = Input::file('url_Avatar')->move($destinationPath, $filename);
var_dump(Image::make($image->getRealPath())->resize('200', '200')->save('upload/' . $filename));
// we can do a redirect with some messages that file was uploaded,
// but we need to pass all validators before reaching to the layout default
if ($upload_success) {
//save in the Band table database
Band::create($data);
return Redirect::to('/')
->with('alert_success', 'your band have been created');
}
else {
return Redirect::to('createBand')
->withInput()
->withErrors($validation)
->with('alert_error', 'respect format image or correct errors');
}
}
我对你的问题进行了编辑,以改进拼写和语法,如果你不喜欢,请随时回滚编辑。但我还是不明白你想做什么,你说的“url格式”是什么?您是否只需要为您的图像设置一个URL?在这种情况下,只需将图像上载到公用文件夹,URL将为http://server/your_public_folder/image_filename
@André没问题,我知道我的英语可以做得更好。就像你说的,我想像你的例子一样重命名文件http://server/your_public_folder/image_filename
。在我的项目中,该文件位于公用文件夹中,但为了在我的数据库中注册该文件,我需要找到在url中重命名该文件的方法。它还没有完成,当我创建带文件的band时,它没有出现在我的数据库中,因为它不是url。我想生成一个指向给定路径的完全限定url,以便在我的数据库中注册他。这是一个很好的工作,看看你如何参与自己的帖子。谢谢你的解释。谢谢你的回答你所说的$image是什么意思??
//*****UPLOAD FILE (on server it's an image, on the DB it's an url*****
$file = Input::file('url_Avatar');
$destinationPath = 'upload/';
$filename = str_random(32) . '.' . $file->getClientOriginalExtension();
//This produces a random string of length 24 made up of alphanumeric characters [a-zA-z0-9]
//$extension =$file->getClientOriginalExtension();
$upload_success = Input::file('url_Avatar')->move($destinationPath, $filename);
var_dump(Image::make($image->getRealPath())->resize('200', '200')->save('upload/' . $filename));
// we can do a redirect with some messages that file was uploaded,
// but we need to pass all validators before reaching to the layout default
if ($upload_success) {
//save in the Band table database
Band::create($data);
return Redirect::to('/')
->with('alert_success', 'your band have been created');
}
else {
return Redirect::to('createBand')
->withInput()
->withErrors($validation)
->with('alert_error', 'respect format image or correct errors');
}
}