Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay when generating video thumbnails #36

Open
septierg opened this issue Nov 9, 2022 · 2 comments
Open

Delay when generating video thumbnails #36

septierg opened this issue Nov 9, 2022 · 2 comments

Comments

@septierg
Copy link

septierg commented Nov 9, 2022

Hi every one, i noticed a mini delay when generating the thumbnail. Here is my situation: I 'm building a URL that accept media and from the media title I do a thumbnail from video or thumbnail from image depends of the mime_content_type. The plugin works fine but just asking is it normal to have let say 1 to 2 sec of delay. When i do the thumbnail from image it's super fast and when i do it with Thumbnails it take 1 to 2 sec to do it.
Here is a video to take a look.
Any help would help.

Thank you

IMG_2057.MOV
@lakshmaji
Copy link
Owner

Hi @septierg, Thanks for raising the issue related to performance.
I don't know the actual cause, could you be able to find the reason or when it is happening like when execution context is making the call to library, or when it is exiting ?

Could be able to provide me reproducible repo!

@septierg
Copy link
Author

HI @lakshmaji thank you for your response, here is my code:
Route:
Route::get('/media/thumbnail/{size}/{file}', 'MediaController@getThumbnail')->name('media.get_thumbnail');

Controller:

public function getThumbnail($size, $file) {
    $media = Media::where('title', $file)->first();

    if(!is_numeric($size) || !isset($media)) {
        return response("", 400);
    }


    $thumb_size = $size > config('app.limit_size_thumbnail') ? config('app.limit_size_thumbnail') : ($size <= 0 
            ? config('app.default_size_thumbnail') : $size);

    return $this->generateThumbnail($media, $thumb_size);
}

private function generateThumbnail(Media $media, $size) {
  
    if(!\File::exists(storage_path("app/public/uploads/thumbnail"))) {
        \File::makeDirectory(storage_path("app/public/uploads/thumbnail"));
    }

    $file_source = storage_path($media->directory);
    $file_target = storage_path("app/public/uploads/thumbnail")."/". pathinfo($media->title, PATHINFO_FILENAME)."-".$size."-thumbnail.jpg";
    
    // generate video thumbnail
    if(strstr(mime_content_type($file_source), "video/")) {
        $thumbnail_status = Thumbnail::getThumbnail($file_source,pathinfo($file_target, PATHINFO_DIRNAME),pathinfo($file_target, PATHINFO_FILENAME).'.jpg');
        if($thumbnail_status)
  {
    echo "Thumbnail generated";
  }
  else
  {
    echo "thumbnail generation has failed";
  }
           
        }
     
    else if(strstr(mime_content_type($file_source), "image/")) {
        if (\File::copy($file_source , $file_target)) {
              return $this->createThumbnail($file_target, $size, $size);
        }
        return null;
    }
}

private function createThumbnail($path, $width, $height) {
    $img = Image::make($path)->resize($width, $height, function ($constraint) {
        $constraint->aspectRatio();
    });
    $img->save($path);  
    return $img->response();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants