php everywhere plugin use more cpu

The PHP Everywhere plugin may increase CPU usage due to its nature of embedding PHP code on any part of a WordPress site. This can lead to increased processing load and potentially slow down the website.

Elaboration:

  • What it does:The PHP Everywhere plugin allows you to run PHP code on any page of your WordPress site without modification. This can be useful for various purposes, but it also comes with potential drawbacks. 
  • CPU impact:The plugin essentially adds overhead to every page load, as it needs to execute PHP code on each visit. This can strain the server’s CPU resources, especially if the plugin is used extensively on many pages or if the PHP code within the plugin is complex. 
  • When to be careful:Consider the performance impact of the plugin, especially if your site has high traffic. It’s best to use the plugin judiciously and only when necessary. 
  • Alternatives:Explore alternative solutions for running PHP code on your site, such as using WordPress’ built-in shortcodes or custom plugin functionalities, which may be more performant. 
  • Monitoring CPU usage:Use tools like Task Manager or server monitoring tools to track CPU usage and identify if the plugin is contributing significantly to high CPU loads. 
  • Resource-heavy tasks:Be mindful of resource-heavy PHP tasks performed within the plugin, as they can significantly impact CPU usage. Optimize these tasks or consider using alternative methods for more efficient execution. 

How to Address CPU Usage Concerns:

  1. 1. Monitor CPU usage:Use server monitoring tools or Task Manager to track CPU usage and identify if the plugin is the culprit. 
  2. 2. Optimize the plugin:If possible, optimize the PHP code within the plugin to reduce resource consumption. 
  3. 3. Consider alternatives:Explore other ways to achieve the desired functionality without relying on the PHP Everywhere plugin. 
  4. 4. Reduce plugin usage:If you’re not using the plugin extensively, consider deactivating or removing it. 
  5. 5. Use caching:Implement a caching plugin to reduce the load on the server by caching static content and reducing the need for PHP execution on every page load. 

How to prevent video from downloading on website

Preventing video downloads on a website can be challenging, as users can always find ways to capture or download content. However, you can implement several strategies to make it more difficult:

1. Use Streaming Protocols

  • HLS (HTTP Live Streaming) or DASH (Dynamic Adaptive Streaming over HTTP): These protocols break videos into small segments, making it harder to download the entire file easily.

2. Disable Right-Click

  • Use JavaScript to disable right-click options on the video element to prevent users from accessing context menus.
document.addEventListener('contextmenu', function(e) {
    e.preventDefault();
});

3. Use a Video Player with Protection Features

  • Implement a video player that offers built-in protections against downloading, such as encrypted streaming or watermarking.

4. Watermark Your Videos

  • Include a visible watermark in your videos to discourage unauthorized use. This won’t prevent downloads but can deter misuse.

5. Implement Token Authentication

  • Use a server-side token system that generates temporary URLs for video access. This limits the time a video can be accessed and prevents direct linking.

6. Set Proper HTTP Headers

  • Configure your server to send headers like Content-Disposition: attachment; filename="video.mp4" to discourage direct downloads.

7. Restrict Access via Referrer

  • Only allow video playback from your domain by checking the HTTP referrer. This prevents direct links from other sites.

8. Use DRM (Digital Rights Management)

  • Implement DRM solutions to protect your content, though this may be more complex and costly.

Conclusion

While no method is foolproof, combining several of these techniques can significantly reduce the chances of users downloading your videos.

What is better, placing images in MySQL BLOB fields or using text fields to reference image files in a given directory?

Storing Images in BLOB Fields

Pros:
1. Atomic Transactions: Storing images in the database allows for atomic transactions, meaning that the image and its associated metadata can be stored and retrieved together, ensuring data integrity.
2. Backup and Recovery: Having all data in one place simplifies backup and recovery processes, as you only need to back up the database.
3. Security: Database access can be more tightly controlled than file system access, potentially enhancing security.

Cons:
1. Performance: Retrieving large images from a database can be slower than serving them from a file system, especially if the images are frequently accessed.
2. Database Size: Storing large binary files can lead to significant database bloat, which can affect performance and management.
3. Complexity: BLOB handling can add complexity to your application code, as you need to manage binary data.

Using Text Fields to Reference Image Files

Pros:
1. Performance: Serving images from a file system is generally faster and more efficient than retrieving them from a database, especially for web applications.
2. Scalability: File systems can handle large amounts of data more easily than databases, making it easier to scale.
3. Simplicity: Managing images as files can be simpler, as you can use standard file handling techniques and tools.

Cons:
1. Data Integrity: There’s a risk of losing the reference to files if they are moved or deleted, leading to broken links or orphaned records in the database.
2. Backup Complexity: You’ll need to ensure that both the database and the file system are backed up together to maintain data integrity.
3. Security: File system access can be more challenging to secure compared to database access.

Recommendations

  • Use BLOBs if you need strong data integrity, atomic transactions, and if security is a primary concern.
  • Use text fields with file references if performance is critical, especially for web applications, and if you expect to handle a large number of images.

Conclusion

The choice largely depends on your specific use case, including factors like the size of the images, the frequency of access, and the overall architecture of your application. For many web applications, the common practice is to store images on a file system and keep references in the database, balancing performance and manageability.

print array in php recursively by all elements

function pretty_dump($arr, $d=1){
    if ($d==1) echo "<pre>";    // HTML Only
    if (is_array($arr)){
        foreach($arr as $k=>$v){
            for ($i=0;$i<$d;$i++){
                echo "\t";
            }
            if (is_array($v)){
                echo $k.PHP_EOL;
                Pretty_Dump($v, $d+1);
            } else {
                echo $k."\t".$v.PHP_EOL;
            }
        }
    }
    if ($d==1) echo "</pre>";   // HTML Only
}

htaccess file for PHP

DirectoryIndex index.php
RewriteEngine on
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]

imagick resize base64 image to base64 image in php

$PostImage = $_POST['PostImage'];
	
$imageBlob = base64_decode(str_ireplace('data:image/png;base64,','',$PostImage));
$im = new Imagick();
$im->readImageBlob($imageBlob);
//$im = new Imagick($_FILES['imageForPost']["tmp_name"]);
            $imageprops = $im->getImageGeometry();
    $finalWidth = 512;
    $finalHeight = 512;
    
            $ext = '.png';        
           
            $size = 40;            
         
                $width = $imageprops['width'];
                $height = $imageprops['height'];
                if($height > $finalHeight){
                $newHeight = $finalHeight;
                $newWidth = ($finalHeight / $height) * $width;
                }else{
                $newWidth = $imageprops['width'];
                $newHeight = $imageprops['height'];
                }

                $im->scaleImage($finalWidth,$finalHeight,true,true);  

                        $canvas = new Imagick();
                        $canvas->newImage($finalWidth, $finalHeight,  'white', 'png');                                              
                
                    $canvas->blurImage(25, 5);
                    $imageprops = $im->getImageGeometry();
                    $newWidth = $imageprops['width'];
                    $newHeight = $imageprops['height'];
                    if($finalWidth == $finalHeight){
                        $pos = 0;
                        $posh = ($finalHeight - $newHeight)/2;
                    }else{
                        $pos = ($finalWidth - $newWidth)/2;
                        $posh = ($finalHeight - $newHeight)/2;
                    }
                    
                    $canvas->compositeImage($im, Imagick::COMPOSITE_OVER,  $pos, $posh);
                  
                    $canvas->setOption('jpeg:extent', $size.'kb');
                    $canvas->setImageFormat( "jpeg" );
                   
				$thumbnail = $im->getImageBlob();	
				$PostImage = $thumbnail;
				
				echo "<img src='data:image/jpg;base64,".base64_encode($thumbnail)."' />";    

imagick example base64 in php

$PostImage = $_POST['PostImage'];
$imageBlob = base64_decode(str_ireplace('data:image/png;base64,','',$PostImage));

$im = new Imagick();
$im->readImageBlob($imageBlob);
	//$im = new Imagick($_FILES['imageForPost']["tmp_name"]);
            $imageprops = $im->getImageGeometry();

    $finalWidth = 512;
    $finalHeight = 512;
        
            $ext = '.png';                  
            $size = 100;            
            $name = time().$ext;//$_FILES["image"]["name"];
                          
                $width = $imageprops['width'];
                $height = $imageprops['height'];
                if($height > $finalHeight){
                $newHeight = $finalHeight;
                $newWidth = ($finalHeight / $height) * $width;
                }else{
                $newWidth = $imageprops['width'];
                $newHeight = $imageprops['height'];
                }
                
                $im->scaleImage($finalWidth,$finalHeight,true,true);
                $im->setOption('png:extent', $size.'kb');
				$im->setImageFormat( "png" ); 
				header("Content-Type: image/png");
				echo $im;                 
                          

text over image using imagick in php

<?php
/* Create some objects */
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( ‘gray’ );

/* New image */
$image->newImage(800, 75, $pixel);

/* Black text */
$draw->setFillColor(‘black’);

/* Font properties */
$draw->setFont(‘Bookman-DemiItalic’);
$draw->setFontSize( 30 );

/* Create text */
$image->annotateImage($draw, 10, 45, 0,
‘The quick brown fox jumps over the lazy dog’);

/* Give image a format */
$image->setImageFormat(‘png’);

/* Output the image with headers */
header(‘Content-type: image/png’);
echo $image;