如何通过干预将图像 trim 变白 space?
How can I trim white space off image with Intervention?
我有一个 class 可以拍摄我下载的照片,然后再重新上传到 S3 以进行长期存储,我想 trim 白色 space。当我存储临时图像(工作正常)时,我尝试在我的 storeTempFile() 方法中调用干预 trim() 但这不会 trim 白色 space.我在想这可能是我代码中的位置?
class PhotoProcessor
{
public function __construct(Listing $listing, $photoData)
{
$this->bucket = 'real-estate-listings';
$this->s3 = App::make('aws')->get('s3');
$this->tempFileName = 'app/storage/processing/images/retsphotoupload';
$this->photoData = $photoData;
$this->listing = $listing;
$this->photo = new RetsPhoto;
}
public function process()
{
$this->storeTempFile();
$this->storeFileInfo();
$this->buildPhoto();
$success = $this->pushToS3();
// if Result has the full URL or you want to build it, add it to $this->photo
DB::connection()->disableQueryLog();
$this->listing->photos()->save($this->photo);
$this->removeTempFile();
unset ($this->photoData);
return $success;
}
private function storeTempFile()
{
// return File::put($this->tempFileName, $this->photoData['Data']) > 0;
File::put($this->tempFileName, $this->photoData['Data']);
Image::make($this->tempFileName)->trim('top-left', null, 60);
return($this->tempFileName) > 0;
}
private function storeFileInfo()
{
$fileInfo = getimagesize($this->tempFileName);
// Could even be its own object
$this->fileInfo = [
'width' => $fileInfo[0],
'height' => $fileInfo[1],
'mimetype' => $fileInfo['mime'],
'extension' => $this->getFileExtension($fileInfo['mime'])
];
}
private function buildPhoto()
{
$this->photo->number = $this->photoData['Object-ID']; // Storing this because it is relevant order wise
$this->photo->width = $this->fileInfo['width'];
$this->photo->height = $this->fileInfo['height'];
$this->photo->path = $this->getFilePath();
}
private function getFilePath()
{
$path = [];
if ($this->listing->City == NULL)
{
$path[] = Str::slug('No City');
}
else
{
$path[] = Str::slug($this->listing->City, $separator = '-');
}
if ($this->listing->Subdivision->subdivision_display_name == NULL)
{
$path[] = Str::slug('No Subdivision');
}
else
{
$path[] = Str::slug($this->listing->Subdivision->subdivision_display_name, $separator = '-');
}
if ($this->listing->MLSNumber == NULL)
{
$path[] = Str::slug('No MLSNumber');
}
else
{
$path[] = Str::slug($this->listing->MLSNumber, $separator = '-');
}
$path[] = $this->photoData['Object-ID'].'.'.$this->fileInfo['extension'];
return strtolower(join('/', $path));
}
private function pushToS3()
{
return $this->s3->putObject([
'Bucket' => $this->bucket,
'Key' => $this->photo->path,
'ContentType'=> $this->fileInfo['mimetype'],
'SourceFile' => $this->tempFileName
]);
}
private function getFileExtension($mime)
{
// Use better algorithm than this
$ext = str_replace('image/', '', $mime);
return $ext == 'jpeg' ? 'jpg' : $ext;
}
private function removeTempFile()
{
return File::delete($this->tempFileName);
}
}
trim()
return 是修剪后的图像,但您未对 return 值执行任何操作。尝试调用 save()
将图像保存到文件系统:
Image::make($this->tempFileName)->trim('top-left', null, 60)->save();
// ^^^^^^
我有一个 class 可以拍摄我下载的照片,然后再重新上传到 S3 以进行长期存储,我想 trim 白色 space。当我存储临时图像(工作正常)时,我尝试在我的 storeTempFile() 方法中调用干预 trim() 但这不会 trim 白色 space.我在想这可能是我代码中的位置?
class PhotoProcessor
{
public function __construct(Listing $listing, $photoData)
{
$this->bucket = 'real-estate-listings';
$this->s3 = App::make('aws')->get('s3');
$this->tempFileName = 'app/storage/processing/images/retsphotoupload';
$this->photoData = $photoData;
$this->listing = $listing;
$this->photo = new RetsPhoto;
}
public function process()
{
$this->storeTempFile();
$this->storeFileInfo();
$this->buildPhoto();
$success = $this->pushToS3();
// if Result has the full URL or you want to build it, add it to $this->photo
DB::connection()->disableQueryLog();
$this->listing->photos()->save($this->photo);
$this->removeTempFile();
unset ($this->photoData);
return $success;
}
private function storeTempFile()
{
// return File::put($this->tempFileName, $this->photoData['Data']) > 0;
File::put($this->tempFileName, $this->photoData['Data']);
Image::make($this->tempFileName)->trim('top-left', null, 60);
return($this->tempFileName) > 0;
}
private function storeFileInfo()
{
$fileInfo = getimagesize($this->tempFileName);
// Could even be its own object
$this->fileInfo = [
'width' => $fileInfo[0],
'height' => $fileInfo[1],
'mimetype' => $fileInfo['mime'],
'extension' => $this->getFileExtension($fileInfo['mime'])
];
}
private function buildPhoto()
{
$this->photo->number = $this->photoData['Object-ID']; // Storing this because it is relevant order wise
$this->photo->width = $this->fileInfo['width'];
$this->photo->height = $this->fileInfo['height'];
$this->photo->path = $this->getFilePath();
}
private function getFilePath()
{
$path = [];
if ($this->listing->City == NULL)
{
$path[] = Str::slug('No City');
}
else
{
$path[] = Str::slug($this->listing->City, $separator = '-');
}
if ($this->listing->Subdivision->subdivision_display_name == NULL)
{
$path[] = Str::slug('No Subdivision');
}
else
{
$path[] = Str::slug($this->listing->Subdivision->subdivision_display_name, $separator = '-');
}
if ($this->listing->MLSNumber == NULL)
{
$path[] = Str::slug('No MLSNumber');
}
else
{
$path[] = Str::slug($this->listing->MLSNumber, $separator = '-');
}
$path[] = $this->photoData['Object-ID'].'.'.$this->fileInfo['extension'];
return strtolower(join('/', $path));
}
private function pushToS3()
{
return $this->s3->putObject([
'Bucket' => $this->bucket,
'Key' => $this->photo->path,
'ContentType'=> $this->fileInfo['mimetype'],
'SourceFile' => $this->tempFileName
]);
}
private function getFileExtension($mime)
{
// Use better algorithm than this
$ext = str_replace('image/', '', $mime);
return $ext == 'jpeg' ? 'jpg' : $ext;
}
private function removeTempFile()
{
return File::delete($this->tempFileName);
}
}
trim()
return 是修剪后的图像,但您未对 return 值执行任何操作。尝试调用 save()
将图像保存到文件系统:
Image::make($this->tempFileName)->trim('top-left', null, 60)->save();
// ^^^^^^