Saturday, February 25, 2017

A simple guide to using Traits in Laravel 5

Its great if we can use trait in our application. Laravel has the ability to give us that freedom to use trait. Laravel is a strong php based framework today. At first we have to create a trait, but its nothing but a php class except we use here a keyword named 'trait' & surely this class has no constructor as it cannot be instantiated. So lets start with use of trait.

First we have to create a php file named "TestTrait.php" as follows:

namespace App\Trait;

trait TestTrait {
    public function someFunction() {
        
    }
}

I think you have already have good knowledge about namespace described in Laravel.

Now we have to use this trait in some Controller or Service etc...

Lets see an example of use of trait in Controller.

First we have to import "TestTrait" in our Controller as follows:

//First step is import
use App\Trait\TestTrait;
class TalentMapperController extends Controller
{
    //Second step is tell our Controller to use trait
    use TestTrait;
    
    public function __construct() {
        //Third & final step is apply functions from here
        super::someFunction();
    }
}

Friday, February 24, 2017

Http Redirect Using Laravel

Laravel is a strong php based framework today. It is very important forward to another url or redirect as http function. There are several ways describes in Laravel. The best way is redirect using Controller & Action. Using Controller Action is easy to redirect to another url:

return redirect()->action(
    'SomeController@action', ['id' => 1, 'status' => 'active']
);


Or you also can do the following:

return redirect("/afterRedirect/param1?x=1&y=2");

Get Current URL Using Laravel

Laravel is a strong php based framework today. We frequently coding on this platform. And who coded its important for him to know / get current working url sometimes. Its easy to get current url inside an Controller/Service class. Just need to use Request attribute to get desired output at Laravel. 

First need to import Request class to Controller/Service class:
use Illuminate\Http\Request;

And then need to pass Request as parameter as follows:
public function someAction(Request $request)

And finally use following method to get current full url:
$request->fullUrl();

You have different options when you use Laravel Request class as follows:

Get the request method
$request->method();

Get the root URL for the application
$request->root();

Get the URL (no query string) for the request
$request->url();

Get the full URL for the request
$request->fullUrl();

Get the current path info for the request
$request->path();

Determine if the request is the result of an AJAX call
$request->ajax(); 


There is another way around to get URL from Laravel Controller
At first you have to import following component:
use Illuminate\Routing\UrlGenerator;

Then need to initialize it on construct on controller as follows:
private $UrlGenerator;
public function __construct(UrlGenerator $UrlGenerator)
{
    $this->UrlGenerator = $UrlGenerator;
}

And then use below line of code get Laravel base URL:
$urlGenerator->to("/");

You can get all available method details of UrlGenerator from below link:
https://laravel.com/api/5.0/Illuminate/Routing/UrlGenerator.html
  

Tuesday, February 21, 2017

Create Thubmnail / Image From Each Page of a PDF Using Java

It is very important to make some thumbnails from pdf in our application. Using java it is now very easy to do this task. You have to open your pdf using java program then you can set which page (0...n) you want to make to thumbnail. Then you can save that as image to your server or desktop machine.

To complete this task you need to download PDFRenderer-0.9.1.jar.

You can download whole package from here.

Sample java code:

package com.pkm;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by pritom on 26/12/2016.
 */
public class ThumbnailFromPdf {
    public static void main(String[] args) throws Exception {
        File file = new File("src/com/pkm/PDF.pdf");
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        FileChannel fileChannel = randomAccessFile.getChannel();
        ByteBuffer byteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
        PDFFile pdfFile = new PDFFile(byteBuffer);
        PDFPage pdfPage = pdfFile.getPage(0);
        Rectangle rectangle = new Rectangle(0, 0, (int) pdfPage.getBBox().getWidth(), (int) pdfPage.getBBox().getHeight());
        Image image = pdfPage.getImage(rectangle.width, rectangle.height, rectangle, null, true, true);
        ImageIO.write((RenderedImage) image, "png", new File("src/com/pkm/PDF.png"));
    }
}

Sample output would be like something depends on your pdf file:


 

Saturday, February 18, 2017

VBScript Take Input From User

VBScript is a scripting language to manage computer developed by Microsoft. Below is a code snippet to take user input:

Wscript.StdOut.Write "Enter Some Input: "
Source = Wscript.StdIn.ReadLine

VBScript Create Rar File With Password Protection Using WinRar.exe

VBScript is a scripting language to manage computer developed by Microsoft. Sometimes we need to create .rar file using WirRar.exe and use VBScript. Below is a code snippet to create rar file using both VBScript & WinRar.exe. There is a extension you can use password to protect your rar file. -p"PASSWORD" will create a rar file with password or if you use only -p as arguments then a prompt will appear to take password from user as input. -r use to create recursive all folders and files withing the root folder. -hs argument for include all hidden files.

Dim ObjShell, FSO
Set ObjShell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")

Wscript.StdOut.Write "Enter Source: "
Source = Wscript.StdIn.ReadLine

Wscript.StdOut.Write "Enter Destination: "
Destination = Wscript.StdIn.ReadLine

Wscript.StdOut.Write "Delete Source: "
DeleteSource = Wscript.StdIn.ReadLine

ObjShell.Run """C:\Program Files\WinRAR\WinRar.exe"" a -x -IBCK -ep1 -r -hs -p """ & Destination & """ """ & Source & """ ", 0, True
Set ObjShell = Nothing

Sub CheckRarCreated
    If (FSO.FileExists(Destination)) Then
       WScript.Echo Destination & " Created"
       If StrComp(DeleteSource, "True", 1) = 0 And FSO.FolderExists(Source) Then
          FSO.DeleteFolder Source
       End If
    Else
       WScript.Echo Destination & " Not Created"
    End If
End Sub
CheckRarCreated

Saturday, February 11, 2017

VBScript Open Another Program And Wait Until Closed

VBScript is a scripting language to manage computer developed by Microsoft. Below is a code snippet to open an external program suppose named "firefox.exe" and wait until firefox closed. Script will not be execute further statements until firefox closed. 

Dim objShell

ShowOption = 1
WaitUntilClose = True

Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run """C:\Program Files\Mozilla Firefox\firefox.exe""", ShowOption, WaitUntilClose
Set objShell = Nothing

'ShowOption
'0 Hide the window (and activate another window.)
'1 Activate and display the window. (restore size and position) Specify this flag when displaying a window for the first time. 
'2 Activate & minimize. 
'3 Activate & maximize. 
'4 Restore. The active window remains active. 
'5 Activate & Restore. 
'6 Minimize & activate the next top-level window in the Z order. 
'7 Minimize. The active window remains active. 
'8 Display the window in its current state. The active window remains active. 
'9 Restore & Activate. Specify this flag when restoring a minimized window. 
'10 Sets the show-state based on the state of the program that started the application.