Skip to content

Commit

Permalink
Deprecated previewImages and video processing, build own image genera…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
bluecraank committed Mar 15, 2024
1 parent 1f7b53e commit 7642f7a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 111 deletions.
86 changes: 37 additions & 49 deletions app/Console/Commands/PresentationProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Imagick;

class PresentationProcess extends Command
{
Expand All @@ -31,55 +32,54 @@ class PresentationProcess extends Command
public function handle()
{
$id = $this->argument('id');

$presentation = Presentation::where('id', $id)->firstOrFail();

$type = $this->argument('type');

if ($type == 'pdf') {
$this->processPdf($presentation);
} else if ($type == 'video') {
$this->processVideo($presentation);
// Deprecated
}
}

public function processPdf($presentation)
{
$pdf = new \Spatie\PdfToImage\Pdf(storage_path('app/public/presentations/' . $presentation->id . '/' . $presentation->id) . '.pdf');

$pages = $pdf->getNumberOfPages();
$presentation->total_slides = $pages;
$presentation->timestamps = false;
$presentation->save();
$i = 0;
$images = [];

for ($i = 1; $i <= $pages; $i++) {
while(true) {
$random = Str::random(7);
$imagename = $random . '-' . $i . '.jpg';
$pdf->setResolution(300);

// $pdf->setPage($i)->saveImage(storage_path('app/public/presentations/' . $presentation->id . '/orig-' . $imagename));
$pdf->setPage($i)->saveImage(storage_path('app/public/presentations/' . $presentation->id . '/orig-' . $imagename));
$image = $this->processPdfPageToImage(storage_path('app/public/presentations/' . $presentation->id . '/' . $presentation->id) . '.pdf', $i, $presentation->id, $imagename);

$resizeImage = \Intervention\Image\Facades\Image::make(storage_path('app/public/presentations/' . $presentation->id . '/orig-' . $imagename));
$resizeImage->resize(1920, 1080)
->save(public_path('data/presentations/' . $presentation->id . '/' . $imagename));
if ($image) {
$images[] = $imagename;
} else {
break;
}

Slide::updateOrCreate([
'presentation_id' => $presentation->id,
'order' => $i,
'order' => $i + 1,
'type' => 'image',
], [
'name_on_disk' => $imagename,
'name' => $random,
]);

$i++;
}

$presentation->timestamps = true;
$presentation->processed = true;

File::delete(storage_path('app/public/presentations/' . $presentation->id . '/' . $presentation->id) . '.pdf');
File::deleteDirectory(storage_path('app/public/presentations/' . $presentation->id . '/'));

$presentation->touch();
$presentation->total_slides = $pages;
$presentation->processed = true;
$presentation->total_slides = count($images);
$presentation->save();

Log::create([
Expand All @@ -89,44 +89,32 @@ public function processPdf($presentation)
]);
}

public function processVideo($presentation)
{
$random = Str::random(7);
$videoname = $random . '-' . 'v' . '.mp4';
$previewname = 'preview-' . $random . '-v' . '.jpg';

// Save video to public
$ffmpeg = \FFMpeg\FFMpeg::create();

$video = $ffmpeg->open(storage_path('app/public/presentations/' . $presentation->id . '/' . $presentation->id) . '.mp4');
public function processPdfPageToImage($pdf, $page, $pres_id, $imagename) {
$image = new Imagick();
$image->setResolution(300, 300);
try {
$image->readImage($pdf . '[' . $page . ']');
} catch (\ImagickException $e) {
return false;
}

$timeCode = \FFMpeg\Coordinate\TimeCode::fromSeconds(0);
$frame = $video->frame($timeCode);
$image->setImageFormat('jpg');

$frame->save(public_path('data/presentations/' . $presentation->id . '/' . $previewname));

$video->save(new \FFMpeg\Format\Video\X264(), public_path('data/presentations/' . $presentation->id . '/' . $videoname) );
if ($image->getImageWidth() == 0) {
return false;
}

Slide::updateOrCreate([
'presentation_id' => $presentation->id,
'order' => 1,
'type' => 'video',
], [
'name_on_disk' => $videoname,
'name' => $random,
]);
// Compress
$image->setImageCompressionQuality(100);

$presentation->processed = true;
// Resize
$image->resizeImage(1920, 1080, Imagick::FILTER_LANCZOS, 1);

Log::create([
'ip_address' => request()->ip(),
'username' => "System",
'action' => __('log.presentation_file_success_updated', ['name' => $presentation->name, 'type' => 'video', 'pages' => 1]),
]);
$image->writeImage(public_path('data/presentations/' . $pres_id . '/' . $imagename));

File::delete(storage_path('app/public/presentations/' . $presentation->id . '/' . $presentation->id) . '.mp4');
File::deleteDirectory(storage_path('app/public/presentations/' . $presentation->id . '/'));
$image->clear();

$presentation->save();
return true;
}
}
8 changes: 3 additions & 5 deletions app/Models/Slide.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ public function getPublicpreviewpathAttribute() {
}

public function publicpreviewpath() {
// Replace file extension
$name = pathinfo($this->name_on_disk, PATHINFO_FILENAME);
$extension = pathinfo($this->name_on_disk, PATHINFO_EXTENSION);
$name = $name . '.jpg';
return asset('data/presentations/' . $this->presentation_id . '/' . $name);
// Deprecated

return asset('data/presentations/' . $this->presentation_id . '/' . $this->name_on_disk);
}

public function getPublicpathAttribute() {
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"laravel/ui": "^4.2",
"livewire/livewire": "^3.0",
"php-ffmpeg/php-ffmpeg": "^1.1",
"spatie/laravel-permission": "^5.11",
"spatie/pdf-to-image": "^1.2"
"spatie/laravel-permission": "^5.11"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",
Expand Down
56 changes: 1 addition & 55 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Http\Controllers\UserController;
use App\Http\Controllers\ScheduleController;
use App\Http\Controllers\SettingsController;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;

Expand Down Expand Up @@ -112,6 +113,14 @@
});
});

Route::get('/test', function() {
$id = 1;
Artisan::call('presentation:process', [
'id' => $id,
'type' => 'pdf'
]);
});

Auth::routes();


0 comments on commit 7642f7a

Please sign in to comment.