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;                 
                          

lavarel 10 being download on cpanel server

To install Laravel 10 on shared hosting and handle the issue with the public directory, you can follow these steps:

  1. Upload Your Laravel Application:
    • Compress your Laravel application into a zip file.
    • Upload the zip file to your shared hosting account, usually through a cPanel file manager or via FTP.
    • Extract the zip file to the desired directory on your shared hosting.
  2. Modify the public Directory:
    • Since you can’t change the document root, you’ll need to move the contents of the public directory to the root directory of your domain (often public_html or htdocs).
    • Move the .htaccess and index.php files from the public directory to the root directory.
  3. Update index.php:
    • Edit the index.php file in the root directory to update the paths to autoload.php and app.php:
    Copyrequire __DIR__.'/../vendor/autoload.php'; $app = require_once __DIR__.'/../bootstrap/app.php'; Change these lines to (assuming that the rest of your Laravel app is one level above the public_html):Copyrequire __DIR__.'/../your-laravel-directory/vendor/autoload.php'; $app = require_once __DIR__.'/../your-laravel-directory/bootstrap/app.php';
  4. Create or Modify the Root .htaccess File:
    • If you don’t already have a .htaccess file in your root directory, create one.
    • Add the following rules to handle the redirection and removal of the /public from the URL:
    Copy<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/ [L] </IfModule>
    • If you’re facing issues with trailing slashes, you can add the following rules to handle that:
    Copy<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^ %1 [L,R=301] RewriteRule ^(.*)$ public/ [L] </IfModule> This will remove the trailing slash for non-directory requests and then rewrite the request to the public directory.
  5. Secure Sensitive Directories:
    • To prevent public access to sensitive directories like /vendor or your .env file, you can add additional rules to your .htaccess file:
    Copy<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_URI} ^/(vendor|storage|.env)(/|$) RewriteRule ^ - [F,L,NC] </IfModule>
  6. Test Your Application:
    • After making these changes, visit your website and test to ensure that everything is working correctly.
    • Check different routes and add trailing slashes to see if the redirection works as expected.

Remember to replace your-laravel-directory with the actual directory name where your Laravel application resides on the server. Also, ensure that your server meets all the requirements for running Laravel 10, such as PHP version and necessary PHP extensions.

database username and password is correct but laravel website not working after server change or change to new password

To come over this issue, you have to execute all the below commands:

php artisan route:cache
php artisan route:clear
php artisan config:cache
php artisan config:clear
php artisan optimize

The problem is that the project made a cache file. ‘config.php’ must be deleted in order to allow the system to restart and you can do so by locate this file and deleting it:

bootstrap/cache/config.php

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;

if isset file php and laravel

Imagick Example

  if(isset( $_FILES["image"] ) && !empty( $_FILES["image"]["name"] ))
            {
            $drive = "drive/temp/";
            $im = new Imagick( $_FILES["image"]["tmp_name"]);
            
            $finalWidth = $_POST['width'];
            $finalHeight = $_POST['height'];
            $size = $_POST['size'];
            
            $name = time().".jpg";//$_FILES["image"]["name"];
            
            $imageprops = $im->getImageGeometry();
                $width = $imageprops['width'];
                $height = $imageprops['height'];
                if($height > $finalHeight){
                $newHeight = $finalHeight;
                $newWidth = ($finalHeight / $height) * $width;
                }else{
                $newWidth = $imageprops['width'];
                $newHeight = $imageprops['height'];
                }
               //$im->resizeImage($newWidth,$newHeight, Imagick::FILTER_MITCHELL, 0.9, true); 
                $im->scaleImage(0, $finalHeight);
                
                if(isset( $_FILES["imagebg"] ) && !empty( $_FILES["imagebg"]["name"] ) )
                {
                    $canvas = new Imagick($_FILES["imagebg"]["tmp_name"]);
                    $canvas->resizeImage($finalWidth, $finalHeight,  Imagick::FILTER_LANCZOS,1);
                }else
                {
                    $canvas = new Imagick();
                    $canvas->newImage($finalWidth, $finalHeight,  $_POST['bgcolor'], 'jpg');
                }     
                $canvas->blurImage(25, 5);
                $pos = ($finalWidth - $newWidth)/2;
                $canvas->compositeImage($im, Imagick::COMPOSITE_OVER,  $pos, 0);
                $canvas->setOption('jpeg:extent', $size.'kb');
                $canvas->setImageFormat( "jpeg" );
            
            //$im->blurImage(25, 5);
            //$im->adaptiveSharpenImage(10,5);
            //$im->unsharpMaskImage(0, 0.5, 1, 0);
            //$im->setImageResolution( 800, 300 );
            //$im->setResolution(800,300);
            //$im->resampleImage  (800,300,imagick::RESOLUTION_PIXELSPERINCH,1);
            //$im->cropThumbnailImage( 800, 300 );
            //$im->setImageCompressionQuality(70);
            //$im->setImageCompression(true);
            //$im->scaleImage(0, 300);
            //$im->adaptiveResizeImage(800,300);
            //$im->liquidRescaleImage(800, 300, 3, 18);
            //$im->resizeImage(800, 300,  Imagick::FILTER_LANCZOS,1);
            //$im->sharpenImage(0, 3);
            //$im->adaptiveBlurImage(5, 1, 0);
            
            if (file_exists($drive.$name)) {
                try{
                    unlink($drive.$name);
                }
                catch(Exception $ex)
                {
                echo $ex->getMessage()."Adi";
                }  
            }
            $canvas->writeImage($drive.$name);
            //print_r($canvas->identifyImage());
            return redirect("image-resizer")->withSuccess('drive/temp/'.$name);         
            }
            
        return view('imageresizer',['name'=>'drive/temp/'.$name,'title'=>'Online Image Resizer','isUpload'=>1]);

get file temp path laravel from request file for imagick

get file temp path laravel from request file for imagick

  $file = $request->file('image');
  $im = new Imagick( $file->getPathName());

In Core PHP

 $im = new Imagick( $_FILES["image"]["tmp_name"] );