How to desaturate an image with Imagick (PHP)
If you want to desaturate an image using the Imagick library, you could use this simple code:
$image = new Imagick();
$image->readImage("test.jpg");
$image->setImageType(Imagick::IMGTYPE_GRAYSCALE);
If you want to desaturate an image using the Imagick library, you could use this simple code:
$image = new Imagick();
$image->readImage("test.jpg");
$image->setImageType(Imagick::IMGTYPE_GRAYSCALE);
If you need to display an image created / opened using the Imagick library in the img tag, then you can use this code:
$image = new Imagick();
$image->readImage("test.jpg");
//output to the img tag:
echo '<img src="data:image/jpg;base64,'.base64_encode($image->getImageBlob()).'" alt="test" />';
Converting an image to black and white in Python is easy with Pillow library
An example:
from PIL import Image
img=Image.open('test.jpg')
greyscaleImg = img.convert('L')
greyscaleImg.show()
where 'test.jpg' - is a path to your image
If you want to show hidden files and folders in Windows 11, then:
select “View” → “Show” → “Hidden items” from the toolbar in windows explorer.
If you're experiencing a Git "pre-receive hook declined" error, there are a few options how to fix it.
The most common reason is that in your repository (for example GitLab) the master branch is now “protected” and the main branch have a name “main”.
Possible solutions:
To query data from a table for the current day, you can use the following query code:
SELECT *
FROM table_name
WHERE DATE(date_field_name) = CURDATE()
where table_name = the name of the table where you want to get data from.
date_field_name - name of the field where dates are stored (in date or datetime format)
An alternative is to write today's date into the query using PHP (sometimes it makes sense if you use indexing on a date column):
$mysqlQuery = "SELECT *
FROM table_name
WHERE date_field_name > '" . date('Y-m-d') . " 00:00:00' ";
To translate Daterangepicker into your language you need to add translations during script initialization.
Initial values:
$("#your_input_id").daterangepicker(
{
locale: {
"daysOfWeek": [
"Su",
"Mo",
"Tu",
"We",
"Th",
"Fr",
"Sa"
],
"monthNames": [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
],
"applyLabel": "Apply",
"cancelLabel": "Cancel",
"fromLabel": "From",
"toLabel": "To",
"customRangeLabel": "Custom",
}
});
Replace those values with your language.
If you want to set up week start on Monday, you need to specify the firstDay parameter during script initialization:
$("#your_input_id").daterangepicker(
{
locale: {
firstDay: 1,
}
});
Let's consider the case that you have a date “08/25/2022” and you want to use in a new Date() object. The problem is that this date format cannot be passed to new Date(), because there will either be an error or the final date value won't be correct.
First you need to convert the date to the other format.
An example:
var splittedDate = "25.08.2022".split(".");
var formattedDate = splittedDate[1] + "/" + splittedDate[0] + "/" + splittedDate[2];
var finDate = new Date(formattedDate);
console.log(finDate);
You can set the required format during script initialization:
$(function() {
$("#your_input_id").daterangepicker(
{
opens: 'left',
locale: {
format: 'DD.MM.Y' //set up date format here
}
},
function(start, end, label) {
});
});
html:
<input name="your_input_name" id="your_input_id" value="" />
You can use this code to clear ZWNBP (zero width no-break space) in a string:
$cleanResult = trim(utf8_decode($sourceString), '?');
where $sourceString = string with ZWNBP
You can use the usual copy function for fast file extraction from a zip archive:
copy("zip://" . $zipFilePath. "#" . $zippedFileName, $unzipFilePath);
where
Example:
copy("zip://myfile.zip#textfile.doc", "myDocument.doc");
This is an example of a function that extracts files from an archive, renaming them if there are files with the same name in the destination folder:
function extractZip($zipFilePath, $folderToExtract)
{
$zipArchive = new ZipArchive();
$result = $zipArchive->open($zipFilePath);
if ($result === TRUE) {
for ($i = 0; $i < $zipArchive->numFiles; $i++) {
$filename = $zipArchive->getNameIndex($i);
$filePath = $folderToExtract. '/' . $filename;
if (file_exists($filePath)) {
$fileInfo = pathinfo($filename);
$newFileName = $fileInfo['filename'] . '_(copy).' . $fileInfo['extension'];
copy("zip://" . $zipFilePath . "#" . $filename, $folderToExtract . '/' . $newFileName);
} else {
copy("zip://" . $zipFilePath. "#" . $filename, $filePath);
}
}
$zipArchive->close();
}
}
Function parameters:
$zipFilePath - path to zip file
$folderToExtract - path to the folder where the archive should be unpacked.
It should be noted that the function has not been tested on nested archives and nested folders.
A function that allows you to extract files from a zip archive to a specified folder:
function extractZip($filePath, $extractTo){
$zip = new ZipArchive();
$result = $zip->open($filePath);
if ($result === TRUE) {
$zip->extractTo($extractTo);
$zip->close();
}
return $result;
}
$file = 'myfile.zip'; // change to your file path
$folder = 'myfolder'; // change to your folder path
extractZip($file, $folder);
Note: you need a zlib library for this code to work
If you want to delete a directory using PHP, first you need to delete all existing files inside it.
For these purposes, you can use this function:
public function removeDirectory($dir): bool
{
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? $this->removeDirectory("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
Let's consider an example of how you can get an image dimensions, knowing its URL.
We create an Image object, then assign a link to it and then after image load we can find out the width and height of the object.
An example:
const img = new Image();
img.src = 'http://mysite.com/myimage.gif'; // change to your URL
img.onload = function() {
console.log(this.width);
console.log(this.height);
}
This example should print to the console the width and height of the image you have specified in the URL.
If you want to get image link using jQuery, use the following code:
$('#my_image').attr('src');
For example, if you have this HTML code:
<img id="my_image" src="image.jpg" alt="my image" />
As a result you will get “image.jpg" path.
Let's look at an example. This code will click on the text containing the word "test". The element must be clickable.
$client = Client::createChromeClient();
$client->request('GET', 'https://testsite.com/'); //opens website
$client->findElement(WebDriverBy::xpath("//*[contains(text(),'test')]"))->click();
$client->close();
If you need the restriction that user cannot select a date in the Datepicker from the future, then there are 2 easy ways to do this.
You must add the maxDate parameter with a value of 0 or the current date “new Date()” at Datepicker initialization.
An example:
$(function() {
$( "#mydate" ).datepicker({ maxDate: new Date() });
});
Alternative:
$(function() {
$( "#mydate" ).datepicker({ maxDate: 0 });
});
If you have encountered an error "A problem occurred evaluating project ':app'. > Could not find method kapt() for arguments" when you have tried to include room in Android Studio, then this quick solution should help you to fix it up.
In the build.gradle file find plugins section and add “id 'kotlin-kapt'” to it:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
}