Embedding transparent png onto an image is too slow

169 views Asked by At

The function below embeds a watermark onto an image. In some servers it's super slow while I never encounter the same issue in my server. What could be wrong with it? I'm out of ideas.

function add_watermark( $source_file_path, $output_file_path, $watermark_file_path, $quality = 80 ) {
    list( $source_width, $source_height, $source_type ) = getimagesize( $source_file_path );

    if ( $source_type === NULL ) {
        return false;
    }

    switch ( $source_type ) {
        case IMAGETYPE_GIF:
            $source_gd_image = imagecreatefromgif( $source_file_path );
            break;
        case IMAGETYPE_JPEG:
            $source_gd_image = imagecreatefromjpeg( $source_file_path );
            break;
        case IMAGETYPE_PNG:
            $source_gd_image = imagecreatefrompng( $source_file_path );
            break;
        default:
            return false;
    }

    $overlay_gd_image = imagecreatefrompng( $watermark_file_path );
    $overlay_width = imagesx( $overlay_gd_image );
    $overlay_height = imagesy( $overlay_gd_image );

    imagecopy(
        $source_gd_image,
        $overlay_gd_image,
        $source_width - $overlay_width - 10,
        $source_height - $overlay_height - 10,
        0,
        0,
        $overlay_width,
        $overlay_height
    );

    imagejpeg( $source_gd_image, $output_file_path, $quality );
    imagedestroy( $source_gd_image );
    imagedestroy( $overlay_gd_image );
}

I'm not sure if it has something to do with php.ini configs but here's the configs that's used in all servers:

memory_limit = 100M
upload_max_filesize = 192M
post_max_size = 100M
file_uploads = On
allow_url_fopen = On
0

There are 0 answers