js

Thursday, February 29, 2024

PHP: write a php code to unzip `.zip` file

 Q. Write a php code to unzip `.zip` file

Solution:
<?php

// Specify the path to the zip file
$zipFilePath = 'path/to/sample.zip';

// Specify the directory where you want to extract the contents
$extractPath = 'path/to/output/folder/';

// Create a ZipArchive object
$zip = new ZipArchive;

// Open the zip file
if ($zip->open($zipFilePath) === TRUE) {
    
    // Extract the contents to the specified directory
    $zip->extractTo($extractPath);
    
    // Close the zip file
    $zip->close();
    
    echo 'Extraction successful!';
} else {
    echo 'Failed to open the zip file';
}
?>

Javascript: Image and text merge

Q. I possess both an image and a string, for example, 'Test' and image is 'sample.jpg'. Provide a JAVASCRIPT code that combines the string 'Test' with the image 'sample.jpg', merging them into a single image 'output.jpg'.


Solution:

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Merge Text and Image</title>
</head>
<body>
    <script>
        function mergeTextAndImage(text, imagePath) {
            // Create an HTML image element
            var img = new Image();

            // When the image is loaded, perform the merging
            img.onload = function() {
                // Create a canvas element
                var canvas = document.createElement('canvas');
                var context = canvas.getContext('2d');

                // Set canvas dimensions to match the image
                canvas.width = img.width;
                canvas.height = img.height;

                // Draw the image on the canvas
                context.drawImage(img, -10,-50);

                // Set the text properties
                context.font = '40px Arial'; // You can change the font and size here
                context.fillStyle = 'white'; // Text color

                // Set the position to place the text on the canvas
                var x = 10; // X-coordinate
                var y = 30; // Y-coordinate

                // Draw the text on the canvas
                context.fillText(text, x, y);

                // Create a new image with the merged content
                var mergedImage = new Image();
                mergedImage.src = canvas.toDataURL('image/png');

                // Append the merged image to the body or display it as needed
                document.body.appendChild(mergedImage);
            };

            // Set the source of the image
            img.src = imagePath;
        }

        // Example usage        
        var text = 'Test';
        var imagePath = 'path/to/sample.jpg'; // Change this to the path of your image

        mergeTextAndImage(text, imagePath);
    </script>
</body>
</html>
 

PHP Image and text merge

 Q. I possess both an image and a string, for example, 'Test' and image is 'sample.jpg'. Provide a PHP code that combines the string 'Test' with the image 'sample.jpg', merging them into a single image 'output.jpg'.


Solution:

<?php
// Function to merge text and image using default font
function mergeTextAndImage($text, $imagePath, $outputPath) {
    // Load the image
    $image = imagecreatefromjpeg($imagePath);
    // Set the text color Red
    $textColor = imagecolorallocate($image, 255, 0, 0);
    // Set the font size
    $fontSize = 5;

    // Set the position to place the text on the image
    $x = 10; // X-coordinate
    $y = 30; // Y-coordinate

    // Merge text onto the image using default font
    imagestring($image, $fontSize, $x, $y, $text, $textColor);

    // Save the merged image
    imagejpeg($image, $outputPath); // Change this based on your image type

    // Free up memory
    imagedestroy($image);
}

// Example usage
$text = 'Test';
$imagePath = 'sample.jpg'; // Change this to the path of your image
$outputPath = 'output.jpg'; // Change this to the desired output path

mergeTextAndImage($text, $imagePath, $outputPath);
?>
<img src="output.jpg">

Monday, February 19, 2024

Write a psql script to list the columns with `text` datatype

 Write a psql script to list the columns with `text` datatype
Answer:

SELECT column_name
FROM information_schema.columns
WHERE table_name = 'myTable'
  AND (data_type = 'text' OR data_type = 'character varying');

Problem

 Write psql script to find a phase `%abc%123%` in a given table `myTable`.

Answer:

DO $$
DECLARE
    v_column_name text;
    p_table_name text := 'myTable';
    search_pattern text := '%abc%123%';
    query_string text;
BEGIN
    -- Create a dynamic query to search for the pattern in all columns
    FOR v_column_name IN
        SELECT column_name
        FROM information_schema.columns
        WHERE table_name = p_table_name
    LOOP
        query_string := format(
            'SELECT * FROM %I WHERE %I::text LIKE $1',
            p_table_name, v_column_name
        );

        -- Execute the dynamic query
        EXECUTE query_string USING search_pattern;
    END LOOP;
END $$;

Saturday, February 17, 2024

MySQL: Explain RPAD() function in MySQL.

 

In MySQL, the RPAD() function is used to right-pad a string with a specified set of characters, up to a certain length. This is often used to format strings, ensuring that they have a specific length by adding characters to the right side of the original string.

The basic syntax of the RPAD() function is as follows:

sql
RPAD(str, length, padstr)
  • str: The original string that you want to right-pad.
  • length: The length of the resulting string after padding.
  • padstr: The string or characters to use for padding. This is optional, and if not provided, it defaults to a space.

Here's an example:

sql
SELECT RPAD('123', 5, '0') AS padded_string;

This query will return the string '12300' because the original string '123' is right-padded with zeros to make the total length 5.

If the padstr parameter is not provided, it defaults to a space character. For example:

sql
SELECT RPAD('abc', 6, ' ') AS padded_string;

This query will return the string 'abc ' because the original string 'abc' is right-padded with spaces to make the total length 6.

The RPAD() function is useful in scenarios where you need to format strings to a specific length, such as when dealing with fixed-width data in a table or when creating formatted outputs.

Friday, February 16, 2024

MySQL: Explain LPAD() function in MySQL.

 

In MySQL, the LPAD() function is used to left-pad a string with a specified set of characters, up to a certain length. This is often used to format strings, ensuring that they have a specific length by adding characters to the left side of the original string.

The basic syntax of the LPAD() function is as follows:

sql
LPAD(str, length, padstr)
  • str: The original string that you want to left-pad.
  • length: The length of the resulting string after padding.
  • padstr: The string or characters to use for padding. This is optional, and if not provided, it defaults to a space.

Here's an example:

sql
SELECT LPAD('123', 5, '0') AS padded_string;

This query will return the string '00123' because the original string '123' is left-padded with zeros to make the total length 5.

If the padstr parameter is not provided, it defaults to a space character. For example:

sql
SELECT LPAD('abc', 6,' ') AS padded_string;

This query will return the string ' abc' because the original string 'abc' is left-padded with spaces to make the total length 6.

The LPAD() function is useful in scenarios where you need to format strings to a specific length, such as when dealing with fixed-width data in a table or when creating formatted outputs.

MySQL: Explain GROUP_CONCAT() function in MySQL.

 

The GROUP_CONCAT() function in MySQL is used to concatenate values from multiple rows into a single string within each group defined by the GROUP BY clause. It is often used to create a comma-separated list or concatenate strings within each group.

The basic syntax of the GROUP_CONCAT() function is as follows:

sql
GROUP_CONCAT([DISTINCT] expression [ORDER BY {column_name | expr | position} [ASC | DESC] [, ...] [SEPARATOR str_val]])
  • DISTINCT: Optional keyword to remove duplicate values before concatenation.
  • expression: The column or expression whose values you want to concatenate.
  • ORDER BY: Optional clause to specify the order in which values are concatenated within each group.
  • ASC | DESC: Optional sorting order for the ORDER BY clause.
  • SEPARATOR: Optional parameter to specify the separator between concatenated values. The default separator is a comma (,).

Here's an example to illustrate the usage of GROUP_CONCAT():

sql
SELECT department, GROUP_CONCAT(employee_name ORDER BY hire_date DESC SEPARATOR ', ') AS employee_list FROM employees GROUP BY department;

In this example:

  • The employee_name values within each department group are concatenated into a single string.
  • The concatenation is ordered by hire_date in descending order (ORDER BY hire_date DESC).
  • The values are separated by a comma and space (SEPARATOR ', ').

It's important to note that the GROUP_CONCAT() function is commonly used with the GROUP BY clause. If you use it without grouping, it will aggregate all rows into a single concatenated string.

sql
SELECT GROUP_CONCAT(employee_name ORDER BY hire_date DESC SEPARATOR ', ') AS all_employees FROM employees;

In this case, it concatenates all employee_name values into a single string for all rows in the employees table.

Be cautious about the length limitations of the result string, as there is a system variable group_concat_max_len that determines the maximum length of the result string produced by GROUP_CONCAT(). If your concatenated string exceeds this limit, you may need to adjust the variable or consider other solutions.

Class 10 Assamese lesson 10 অৰুণিমা সিনহা Question answer

 (১) চমুকৈ উত্তৰঃ লিখা। (ক) অৰুণিমা সিনহাৰ কোনবিধ খেলত বিশেষ দক্ষতা আছিল ?  উত্তৰঃ অৰুণিমা সিনহাৰ ভলীবল খেলত বিশেষ দক্ষতা আছিল। (খ) অৰুনি...