ApiController.php 3.35 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":"SISKINDS_test file","folder":"SISKINDS_test","pdfLocation":"https://www.polyu.edu.hk/iaee/files/pdf-sample.pdf"}],"changes":[{"label":"none","locationUp":"400","locationRight":90,"pages":"1","content":"LOGO_text_black.jpg","z-index":null,"idstylesPDF":1,"order":"2","style":null,"idchange_typePDF":2,"change_type":"2","fileLocation":"http://www.arielpremium.com/images/vsample_images/LOGO_text_black.jpg"},{"label":"text style","locationUp":"60","locationRight":290,"pages":"1","content":"519.660.2121    siskinds.com\/change","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:14:\"cmyk 0 0 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('local')->getDriver()->getAdapter()->getPathPrefix();
        $folder = str_replace(' ', '-', $json->pdf[0]->label); // Replaces all spaces with hyphens.
        $folder = preg_replace('/[^A-Za-z0-9\-]/', '', $folder);

        if(!is_dir($storagePath . $folder . '/')){
            mkdir($storagePath . $folder . '/');
       }

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

            $pdfLocation = $json->pdf[0]->pdfLocation;
            $file = explode('/', $pdfLocation);
            $curl = new Curl();
            $curl->download($pdfLocation, $storagePath . $folder . '/' . end($file));
            $curl->close();
            $im = new Imagick();
            $im->setResolution(300, 300);     //set the resolution of the resulting jpg
            $im->readImage($storagePath . $folder . '/' . end($file));    //[0] for the first page
            $im->setImageFormat('jpg');
            $fileName = end($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 getList($json)
    {

        $rows = new pdfModel();
        $rows = $rows->getList($json)->get();
        $rows = json_encode($rows);
        var_dump($rows);

    }
    public function getPdf($json)
    {

        $rows = new pdfModel();
        $rows = $rows->getpdf($json)->get();
        $rows = json_encode($rows);
        var_dump($rows);

    }
}