Postingan

How to download an image from a URL using file_get_contents in PHP, resize the image using the GD library, and then encode the resized image as BASE64.

Here's an example of how to download an image from a URL using file_get_contents in PHP, resize the image using the GD library, and then encode the resized image as BASE64.php // URL of the image you want to download and resize $imageUrl = 'https://example.com/path/to/your/image.jpg'; // Download the image using file_get_contents $imageData = file_get_contents($imageUrl); if ($imageData === false) { echo 'Failed to download the image.'; exit; } // Create an image resource from the downloaded image data $originalImage = imagecreatefromstring($imageData); if ($originalImage === false) { echo 'Failed to create image resource.'; exit; } // Get the original image dimensions $originalWidth = imagesx($originalImage); $originalHeight = imagesy($originalImage); // Calculate the new dimensions for the resized image $newWidth = 200; // Replace with the desired width for the resized image $newHeight = ($originalHeight / $originalWidth) * $newWi

Create token using JSON Web Tokens (JWT)

To create a web token using JSON Web Tokens (JWT) in a web application, you can use libraries available in your programming language. Here, I'll demonstrate how to create a JWT in PHP using the "firebase/php-jwt" library. 1. Install the "firebase/php-jwt" library: Use Composer to install the PHP-JWT library. If you don't have Composer installed, you can download it from https://getcomposer.org/. Run the following command in your project's root directory to install the library: composer require firebase/php-jwt 2. Create a PHP script to generate a JWT: Create a new PHP file (e.g., generate_jwt.php) and add the following code: require __DIR__ . '/vendor/autoload.php'; // Include the composer autoloader use \Firebase\JWT\JWT; use \Firebase\JWT\Key; // Replace these values with your own secret key and any other claims you want to include in the token $secretKey = 'your_secret_key'; $issuedAt = time(); $expirationTime = $issued

Add these security headers to your website

The X-Frame-Options, Strict-Transport-Security, X-Content-Type-Options, Referrer-Policy, Permissions-Policy, and Content-Security-Policy headers are security-related HTTP headers that help enhance the security of web applications. To add these security headers to your website, you can use the .htaccess file for Apache web servers or the server configuration for other web servers. Here's how to set each header: 1. X-Frame-Options: The X-Frame-Options header helps prevent clickjacking attacks by controlling whether your website can be embedded in an iframe on another domain. For Apache (using .htaccess): Header always set X-Frame-Options "SAMEORIGIN" 1. Strict-Transport-Security (HSTS): The Strict-Transport-Security header enforces the use of HTTPS by instructing the browser to only access the website over a secure (HTTPS) connection. For Apache (using .htaccess): Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"

WhatsApp Web login QR code in an HTML page using whatsapp-web.js

Gambar
To display the WhatsApp Web login QR code in an HTML page using whatsapp-web.js in Windows, you can follow these steps: 1. Install the required dependencies: npm install express qrcode whatsapp-web.js 2. Create a new file named api-browser.js and add the following code: const express = require('express'); const qrcode = require('qrcode'); const { Client } = require('whatsapp-web.js'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.sendFile(__dirname + '/index.html'); }); app.get('/qrcode', async (req, res) => { const client = new Client(); client.on('qr', (qr) => { qrcode.toDataURL(qr, (err, url) => { if (err) { console.error('Error generating QR code:', err); res.sendStatus(500); } else { res.send(` `); } }); }); await client.initialize(); }); app.listen(port, () => { console.log(`Server is running on ht

Run a Node.js script as a background process

To run a Node.js script as a background process, you can use various methods depending on your specific requirements and the operating system you are using. Here are a few options: 1. Using a process manager (e.g., PM2): - Install PM2 globally by running npm install -g pm2 . - Start your Node.js script as a background process using PM2: pm2 start your_script.js - PM2 provides additional features like process monitoring, automatic restarts, and log management. 2. Using the nohup command: - Open a terminal and navigate to the directory where your Node.js script is located. - Run the following command: nohup node your_script.js & The nohup command ensures that the script continues running even if the terminal session is closed. 3. Using the screen command: - Install screen if it is not already installed by running sudo apt install screen . - Open a terminal and run the following command to start a new screen session: screen -S session_name - With

Whatsapp-web.js On Ubuntu Server

If you are encountering issues with launching the browser while trying to load the QR code using whatsapp-web.js on an Ubuntu server, you can try the following approach: First, make sure you have installed the necessary dependencies, including whatsapp-web.js, qrcode-terminal, and a headless browser like puppeteer: npm install whatsapp-web.js qrcode-terminal puppeteer Next, create a file called index.js and paste the following code: const qrcode = require('qrcode-terminal'); const { Client } = require('whatsapp-web.js'); const puppeteer = require('puppeteer'); // Path to store session data const SESSION_FILE_PATH = './session.json'; // Initialize the WhatsApp client const client = new Client({ session: require(SESSION_FILE_PATH), puppeteer: { executablePath: '/usr/bin/chromium-browser', // Change this path to your Chromium executable headless: true, args: ['--no-sandbox', '--disable-setuid

Convert a PDF file to a Word document on Node.js

To convert a PDF file to a Word document on Node.js, you can use a package like pdf-to-word. Here's an example code snippet that shows how to use pdf-to-word to convert a PDF file to a Word document: const fs = require('fs'); const pdf2docx = require('pdf-to-word'); const pdfFilePath = 'path/to/pdf/file.pdf'; const outputFilePath = 'path/to/word/file.docx'; pdf2docx.convert(fs.readFileSync(pdfFilePath)).then(function (result) { fs.writeFileSync(outputFilePath, result); console.log('PDF converted to Word document successfully.'); }).catch(function (error) { console.error('Error converting PDF to Word document:', error); }); In this example, we first require the fs module to read and write files, and the pdf-to-word package to convert the PDF to Word. Then, we specify the paths to the input PDF file and the output Word file. Next, we call pdf2docx.convert() and pass in the contents of the PDF file using fs.readFile

Read text from an image in Node.js

To use Tesseract.js on Node.js, you first need to install it as a dependency in your Node.js project using npm or yarn. Here are the steps to install and use Tesseract.js on Node.js: 1. Install Tesseract.js and its dependencies using npm or yarn: npm install tesseract.js or yarn add tesseract.js 2. Once installed, you can import and use Tesseract.js in your Node.js application. Here is an example that uses Tesseract.js to recognize text from an image file: const Tesseract = require('tesseract.js'); // Provide the path to the image file const imagePath = '/path/to/image.jpg'; // Use Tesseract.js to recognize text from the image Tesseract.recognize(imagePath, 'eng', { logger: m => console.log(m) }) .then(({ data: { text } }) => { console.log(`Recognized text: ${text}`); }) .catch(error => { console.log(`Error recognizing text: ${error}`); }); In this example, we first import Tesseract.js and then provide the path to

Reset the values of a form using jQuery

To reset the values of a form using jQuery, you can use the reset method. This method resets the form fields to their default values. Here is an example: HTML: <form id="myForm"> <input type="text" name="name" value="John"> <input type="text" name="email" value="john@example.com"> <button type="button" id="resetBtn">Reset</button> </form> JavaScript: $(document).ready(function() { $('#resetBtn').click(function() { $('#myForm')[0].reset(); }); }); In the above example, when the user clicks the "Reset" button, the values of the input fields with name="name" and name="email" will be reset to their default values. The reset method is called on the form element with id myForm. Note that the reset method only works on form elements, not on individual input fields.

Save data input from a datatable as a array

To save input form data from a DataTable, you can use the DataTable rows().data() method to get the data from the table, and then send it to the server via an AJAX request. Here is an example code snippet that demonstrates how to save input form data from a DataTable using jQuery and AJAX: // Get the DataTable instance var table = $('#example').DataTable(); // Handle form submission $('#form').submit(function(event) { event.preventDefault(); // Get the data from the DataTable var data = table.rows().data().toArray(); // Send the data to the server $.ajax({ url: 'save.php', method: 'POST', data: { data: data }, success: function(response) { alert('Data saved successfully!'); }, error: function(xhr, status, error) { console.error(error); alert('Error saving data!'); } }); }); In this example, the #example element represents the DataTable, and the #form element represents t