Skip to content

Long JPEG image output #243

Closed
Closed
@Dumra

Description

@Dumra

Hey. I'm trying to change my watermarking image service from GD to libvips for increasing performance/decrease OOM errors.
The watermarking process really shows faster on a JPEG file with a huge resolution but I have a weird behavior with a long output process for the FullHD or bigger JPEG file resolution after watermarking. I have no clue how to fix it.

Here's my code.

        error_log("Start process " . LibVipsConversion::class);
        $startTime = microtime(true);
        $image = Vips\Image::newFromFile($filePath, [
            'access' => 'sequential',

        ]);

        $page_height = $image->height;
        $page_width = $image->width;

        $text = $this->text[0];
        //$text = str_pad($text, \strlen($text) + 20);

        $text_mask = Vips\Image::text($text, [
            'width' => $image->width,
            'dpi' => 102,
            'font' => 'DejaVuSans 14',
        ]);

        $foreground = [0, 0, 0, (int)(255 * ($this->opacity / 100))]; // 38 is ~15% of 255

        $overlay = $text_mask->ifthenelse($foreground, null, [
            'blend' => true
        ]);

        $margin = 20;
        $overlay = $overlay->embed(
            $margin,
            $margin,
            $overlay->width + 2 * $margin,
            $overlay->height + $margin,

        );

        $overlay = $overlay->copy(['interpretation' => 'srgb']);
        $overlay = $overlay->rotate(360 - 45);

        $num_repeats_x = (int)ceil($page_width / $overlay->width);
        $num_repeats_y = (int)ceil($page_height / $overlay->height);
        $overlay = $overlay->replicate($num_repeats_x, $num_repeats_y);

        $image = $image->composite2($overlay, 'over');
        error_log("Before output: " . (microtime(true) - $startTime) * 1000);
        $data = $image->jpegsave_buffer([
            'Q' => 80,           
        ]);

        error_log("Output ready: " . (microtime(true) - $startTime) * 1000);
        echo $data;
        error_log("Close: " . (microtime(true) - $startTime) * 1000);

Output FullHD file:

│ NOTICE: PHP message: Before output: 46.720027923584 
│ NOTICE: PHP message: Output ready: 248.31509590149
│ NOTICE: PHP message: Close: 249.15599822998

Output 4k file:

│ NOTICE: PHP message: Before output: 51.825046539307 
│ NOTICE: PHP message: Output ready: 514.9199962616
│ NOTICE: PHP message: Close: 517.19999313354

So it's 200ms for FullHD, and 500ms for 4k images.

Am I doing something wrong? Should I add some options into jpegsave_buffer method call?

My env:
Debian Bookworm
PHP 8.3
libvips42t64/testing,now 8.15.2-1+b1 amd64 [installed,automatic]
"jcupitt/vips": "^2.4",

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions