Php 我想用干涉/图像调整照片的大小
我想通过干预/图像调整上传的图像大小,但我不知道如何调整 我尝试了几种上传的方法,但我不知道如何让它工作。我想在storeImage函数中调整其大小 HomeController.phpPhp 我想用干涉/图像调整照片的大小,php,laravel,Php,Laravel,我想通过干预/图像调整上传的图像大小,但我不知道如何调整 我尝试了几种上传的方法,但我不知道如何让它工作。我想在storeImage函数中调整其大小 HomeController.php 根据for interference图像,您需要调用save(),而不是move(): 这是我们使用interference\image按比例调整图像大小的函数 public static function imageResizeWithFallback($img, $width, $height, $new_
根据for interference图像,您需要调用save()
,而不是move()
:
这是我们使用interference\image
按比例调整图像大小的函数
public static function imageResizeWithFallback($img, $width, $height, $new_name = false, $quality = 90){
$new_name = (!empty($new_name)) ? $new_name : $img;
$imgHandler = Intervention\Image\ImageManagerStatic::make($img);
$imgHandler->orientate();
$old = array(
'filesize' => $imgHandler->filesize(),
'width' => $imgHandler->width(),
'height' => $imgHandler->height()
);
$imgHandler->widen($width, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})->heighten($height, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$imgHandler->save($new_name, $quality);
$status = array(
'status' => true,
'filename' => $new_name
);
//fallback if the new image filesize > old image filesize
if(file_exists($new_name) && $imgHandler->filesize() >= $old['filesize'] && ($old['width'] == $imgHandler->width() && $old['height'] == $imgHandler->height())){
if($new_name != $img) {
unlink($new_name);
}
$status = array(
'status' => false,
'filename' => $img,
'debug' => array(
'old' => $old,
'new' => array(
'filesize' => $imgHandler->filesize(),
'width' => $imgHandler->width(),
'height' => $imgHandler->height()
)
)
);
}
return $status;
}
不支持move()。使用save()而不是move()
您可以看到干预如果您需要移动文件,最好使用处理文件的类,而不是图像。我尝试了此操作,但它没有将图像移动到文件夹…:(知道此错误吗?“无法将图像数据写入路径”我用“保存”更改了它,但我不知道如何将它保存到特定的位置folder@TomaRareş我已经更新了我的代码。顺便问一句,你的图像目录存在吗?是的,它存在于公用文件夹(public/images)中。我尝试了代码,但它仍然给出“无法将图像数据写入路径(images)”。现在它可以工作了。我必须更改为“保存('images/”.$user->image)非常感谢。
private function storeImage($user)
{
if (request()->has('image')) {
$user->image = $user->email . '.' . request()->image->getClientOriginalExtension();
$imageExtensions = ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'svg', 'svgz', 'cgm', 'djv', 'djvu', 'ico', 'ief', 'jpe', 'pbm', 'pgm', 'pnm', 'ppm', 'ras', 'rgb', 'tif', 'tiff', 'wbmp', 'xbm', 'xpm', 'xwd'];
$foo = \File::extension($user->image);
if (in_array($foo, $imageExtensions)) {
$img = Image::make(request()->image)
->resize(320, 240)
->save('images/' . $user->image);
}
}
}
public static function imageResizeWithFallback($img, $width, $height, $new_name = false, $quality = 90){
$new_name = (!empty($new_name)) ? $new_name : $img;
$imgHandler = Intervention\Image\ImageManagerStatic::make($img);
$imgHandler->orientate();
$old = array(
'filesize' => $imgHandler->filesize(),
'width' => $imgHandler->width(),
'height' => $imgHandler->height()
);
$imgHandler->widen($width, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})->heighten($height, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$imgHandler->save($new_name, $quality);
$status = array(
'status' => true,
'filename' => $new_name
);
//fallback if the new image filesize > old image filesize
if(file_exists($new_name) && $imgHandler->filesize() >= $old['filesize'] && ($old['width'] == $imgHandler->width() && $old['height'] == $imgHandler->height())){
if($new_name != $img) {
unlink($new_name);
}
$status = array(
'status' => false,
'filename' => $img,
'debug' => array(
'old' => $old,
'new' => array(
'filesize' => $imgHandler->filesize(),
'width' => $imgHandler->width(),
'height' => $imgHandler->height()
)
)
);
}
return $status;
}
private function storeImage($user){
if(request()->has('image')){
$user->image = $user->email.'.'.request()->image->getClientOriginalExtension();
$imageExtensions = ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'svg', 'svgz', 'cgm', 'djv', 'djvu', 'ico', 'ief','jpe', 'pbm', 'pgm', 'pnm', 'ppm', 'ras', 'rgb', 'tif', 'tiff', 'wbmp', 'xbm', 'xpm', 'xwd'];
$foo = \File::extension($user->image);
if(in_array($foo, $imageExtensions))
{
$img = Image::make(request()->image)
->resize(320, 240)
->save('images/'.$user->image);
}
}
}