ApiController.php 6.05 KB
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers;
use  App\pdfModel;
use URL;
use Storage;
use Response;
use File;
Use PDF;
use Illuminate\Support\Facades\Input;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Facade;
use League\Flysystem\Filesystem;
use Curl\Curl;
use Imagick;





class ApiController extends Controller
{


    //Takes json data and passes it to model and grabs related files.

    public function insert($json)
    {

	
	 $json = '{"pdf":[{"name":"Sample PDF Document","folder":"Sample","pdfLocation":"http://hotelpodlipou.sk/uploads/files/sample.pdf"}],"changes":[{"label":"none","locationUp":"400","locationRight":90,"pages":"1","content":"logo-sample-text_355-558.jpg","z-index":null,"idstylesPDF":1,"order":"2","style":null,"idchange_typePDF":2,"change_type":"2","fileLocation":"https://image.freepik.com/free-vector/logo-sample-text_355-558.jpg"},{"label":"text style","locationUp":"60","locationRight":290,"pages":"1","content":"519.642.4404 London","z-index":null,"idstylesPDF":2,"order":"1","style":"a:3:{s:11:\"font-family\";s:11:\"TradeGothic\";s:9:\"font-size\";s:2:\"14\";s:10:\"font-color\";s:16:\"cmyk 0 0.6 0.5 0\";}","idchange_typePDF":1,"change_type":"1"}]}';

        $apiCallId = new pdfModel();
        $apiCallId = $apiCallId->addApiCall('1',$json);
        //$apiCallId = "3";
        $pdfId = new pdfModel();
        $pdfId = $pdfId->addPdf($apiCallId, $json);
        $json = json_decode($json);




        $storagePath = Storage::disk('public')->getDriver()->getAdapter()->getPathPrefix();
        $folder = str_replace(' ', '-', $json->pdf[0]->name); // Replaces all spaces with hyphens.
        $folder = preg_replace('/[^A-Za-z0-9\-]/', '', $folder);

        if(!is_dir($storagePath . $folder . '/')){
            $old = umask(0);
            mkdir($storagePath . $folder . '/', 0777);
            umask($old);
       }

        if (isset($json->pdf[0]->pdfLocation)) {

            $pdfLocation = $json->pdf[0]->pdfLocation;
            $file = explode('/', $pdfLocation);
            $file = end($file);
            $curl = new Curl();
            $curl->download($pdfLocation, $storagePath . $folder . '/' . $file);
            $curl->close();
            $fileEntry = New FileEntryController();
           // echo $storagePath . $folder . '/' . $file;
            //$fileEntry->addfile( $folder . '/' . $file);


            $im = new Imagick();
            $im->setResolution(300, 300);     //set the resolution of the resulting jpg
            if (! is_readable($storagePath . $folder . '/' . $file)) {
                echo 'file not readable';
                exit();
            }
            $im->readImage($storagePath . $folder . '/' . $file);    //[0] for the first page
            $im->setImageFormat('jpg');
            $fileName = $file;
            $fileName = explode('.', $fileName);
            $im->writeImage($storagePath . $folder . '/' . $fileName[0].'.jpg');

        }
        foreach ($json->changes as $change) {
            if (isset($change->fileLocation)) {
                $fileLocation = $change->fileLocation;
                $file = explode('/', $fileLocation);
                $curl = new Curl();
                $curl->download($fileLocation, $storagePath . $folder . '/' . end($file));
                $curl->close();

            }


        }

    }

    public function update()
    {


        $json = Input::all();

        $json = json_encode($json);
        $json = json_decode($json);

        
        $storagePath = Storage::disk('public')->getDriver()->getAdapter()->getPathPrefix();


        foreach ($json->changes as $change) {
            if ($change->change_type == 'img') {

                $folder = str_replace(' ', '-', $json->pdf[0]->folder); // Replaces all spaces with hyphens.
                $folder = preg_replace('/[^A-Za-z0-9\-]/', '', $folder);
                $fileLocation = $change->fileLocation;
                $file = explode('\\', $change->content);

                $curl = new Curl();
                $curl->download($fileLocation.'/'.end($file), $storagePath . $folder . '/' . end($file));
                $curl->close();

            }
        }

        $json = json_encode($json);
       // $json = '{"pdf":[{"name":"Sample PDF Document","folder":"Sample","pdfLocation":"http://hotelpodlipou.sk/uploads/files/sample.pdf"}],"changes":[{"label":"none","locationUp":"400","locationRight":90,"pages":"1","content":"logo-sample-text_355-558.jpg","z-index":null,"idstylesPDF":1,"order":"2","style":null,"idchange_typePDF":2,"change_type":"2","fileLocation":"https://image.freepik.com/free-vector/logo-sample-text_355-558.jpg"},{"label":"text style","locationUp":"60","locationRight":290,"pages":"1","content":"519.642.4404 London","z-index":null,"idstylesPDF":2,"order":"1","style":"a:3:{s:11:\"font-family\";s:11:\"TradeGothic\";s:9:\"font-size\";s:2:\"14\";s:10:\"font-color\";s:16:\"cmyk 0 0.6 0.5 0\";}","idchange_typePDF":1,"change_type":"1"}]}';

        //$apiCallId = new pdfModel();
       // $apiCallId = $apiCallId->addApiCall('1',$json);
        $apiCallId = "3";
        $pdfId = new pdfModel();
        $pdfId = $pdfId->changePdf($apiCallId, $json);

        $pdf = new PdfController();
        $pdf =  $pdf->pdf($pdfId);
        //$response =  $pdf;
        $statusCode = 200;
		
		 return Response::json('good', $statusCode);
        
           


    }



    public function getList($json)
    {
        $rows = new pdfModel();
        $rows = $rows->getList($json)->get();
        $response = $rows;
        $statusCode = 200;
        return Response::json($response, $statusCode);

    }

    public function getApi($json)
    {
        $file = Storage::disk('public')->get('interface.js');
        return $file;

        return (new Response($file, 200))
            ->header('Content-Type', 'text/html');
    }



    public function getPdf($json)
    {


        $rows = new pdfModel();
        $rows = $rows->getpdf($json)->get();
        $response = $rows;
        $statusCode = 200;
        return Response::json($response, $statusCode);

    }



}