Skip to main content

Image Orientation in PHP

                     Image Orientation in PHP


How to fixed Correct Orientation rotate image problem .



Solution:-

public function imageOrientaionUpload(Request $request) {
    $publicPath = public_path('uploads_directory/');
    if($request->file('imgFile') == null) {
        echo 'No File Exist';
    }
    $file = $request->file('imgFile');
    // generating unique name for uploaded file
    $uniq = uniqid().time();
    $uploadFile = $uniq.'.'.$file->getClientOriginalExtension();
    // getting file path
    $filePath = $file->getPathName();
    // image rotation code in php starts here
    $exif = @exif_read_data($filePath);
    if(isset($exif['Orientation'])) {
        $orientation = $exif['Orientation'];
        if (isset($orientation) && $orientation != 1){
            switch ($orientation) {
                case 3:
                   // 180 degree image rotation
                    $deg = 180;
                    break;
                case 6:
                   // 270 degree image rotation
                    $deg = 270;
                    break;
                case 8:
                   // 90 degree image rotation
                    $deg = 90;
                    break;
                }
            if ($deg) {
                // If uploaded image is php
                if ($file->getClientOriginalExtension() == "png") {
                    $img_new = imagecreatefrompng($filePath);
                    $img_new = imagerotate($img_new, $deg, 0);
                    // Save rotated image
                    imagepng($img_new,$filePath);
                    } else {
                        $img_new = imagecreatefromjpeg($filePath);
                        $img_new = imagerotate($img_new, $deg, 0);
                        // Save rotated image
                        imagejpeg($img_new,$filePath,80);
                    }
                }
            }
        }
    // image rotation code in php ends here
    // image compression start
    $destinationPath = $publicPath.$uploadFile;
    // checking file extenstion in php
    $getFileExtn = strtolower($file->getClientOriginalExtension());
    $imageExtnArr = ['jpeg','jpg','png','bmp'];
    if(!in_array($getFileExtn, $imageExtnArr)){
        echo 'Image File format not supported';
    }
    // save roatate image
    $img->save($destinationPath,70);
    echo 'failed to upload image';
}

How to do a rotation of an image with PHP (Laravel)

Note:- Any Question any Time on the comment section

Your Answer:-Please comment on this.

Thanks. 




Comments

Popular posts from this blog

RSS FEED Laravel

RSS FEED IN LARAVEL Installation The Laravel 5/6/7 Feeds Service Provider can be installed via Composer by requiring the willvincent/feeds package in your project's  composer.json. {     "require": {         "willvincent/feeds": "1.1.*"     } } Configuration If you're using Laravel 5.5 or newer you may skip the next step. To use the Feeds Service Provider, you must register the provider when bootstrapping your Laravel application. Find the providers key in your   config/app.php  and register the Service Provider.      'providers' => [         // ...         willvincent\Feeds\FeedsServiceProvider::class,     ], Find the aliases key in your config/app.php and register the Facade.      'aliases' => [         // ...         'Feeds'    => willvincent\Feeds\Facades\FeedsFacade::class,     ], ...

REMOVE ANY TIMEZONE FROM DATE AND TIME USING LARAVEL

                            REMOVE ANY TIMEZONE FROM DATE AND TIME STEP 1:- INSTALL CARBON DATE  Use the following command to install with composer. composer require nesbot/carbon Step 2:- Use this code $date = Carbon::parse('Thu, 28 May 2020 02:42:45 -0400'); dump($date->toDateTimeString()); dump($date->format('Y-m-d')); dump($date->format('Y-m-d H:i:s')); dump($date->timezone->getName()); dump($date->timestamp); output:-  Thu, 28 May 2020 02:42:45

Laravel 5.7 - Stripe Payment Gateway By Captain Laravel

Presented By Captain Laravel STEP 1:  we need to install stripe-php via the Composer package manager, so one your terminal and fire bellow command:   Command:-  composer require stripe/stripe-php STEP 2 :- Now, we need to set stripe key and secret. so first you can go on  Stripe  website and create development stripe account key and secret and add bellow: .env STRIPE_KEY=pk_test_reFxwbsm9cdCYYYBbynUfxAR STRIPE_SECRET=sk_test_iQMFWtUUTTBGFECAtgApY routes/api.php Route::get('stripe', 'StripePaymentController@stripe'); Route::post('stripe', 'StripePaymentController@stripePost')->name('stripe.post');