Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement on Laravel #95

Open
skittery opened this issue Oct 31, 2017 · 10 comments
Open

Implement on Laravel #95

skittery opened this issue Oct 31, 2017 · 10 comments

Comments

@skittery
Copy link

I know this might sound funny but I'm trying to call the library on Laravel from within a controller and can't seem to get it right.
This is my setup:

app/Http/Controllers/indexController.php is where I plan on using the screen capturing.
app/Http/Lib/Screen/ is where I have the library.

I tried a bunch of use options and nothing seemed to work.

How should I call the library?

Thanks in advance!

@peter-mw
Copy link
Contributor

install the library via Composer and call it as any other

@skittery
Copy link
Author

You're right.
Bad habit I do sometimes.

Though I have another weird issue.

This is my code:

		$url = 'https://github.com';

		$screenCapture = new Capture($url);
		$screenCapture->setWidth(1200);
		$screenCapture->setHeight(800);
		$screenCapture->setImageType('png');

$fileLocation = '/screen/';
$screenCapture->save($fileLocation); // Will automatically determine the extension type

echo $screenCapture->getImageLocation(); 

The save function is throwing this error:

PhantomJsException in Capture.php line 259:
'C:\xampp\htdocs\projectfolder' is not recognized as an internal or external command,
operable program or batch file.

What's the problem? The fileLocation path?
I tried a few different options and they all threw the same error.

Any help would be really appreciated.

Thanks

@peter-mw
Copy link
Contributor

strange.

try setting the phantomjs dir with

$screenCapture->binPath = '/path/to/bin/dir/';

no idea why this error appears, maybe @MASNathan knows

@skittery
Copy link
Author

We might be getting somewhere.
I added this:
$screenCapture->binPath = '/vendor/microweber/screen/bin/';

And now the error changed to PhantomJsException in Capture.php line 259: The system cannot find the path specified.

@MASNathan
Copy link
Contributor

Try this:

Add the following to your composer.json file. The binaries will be downloaded in your vendor/bin directory. For more info regarding this process, view the PhantonJS installer documentation.

 {
    //---
    "scripts": {
        "post-install-cmd": [
            "PhantomInstaller\\Installer::installPhantomJS"
        ],
        "post-update-cmd": [
            "PhantomInstaller\\Installer::installPhantomJS"
        ]
    }
    //---
}

And then run

$ composer require jakoch/phantomjs-installer microweber/screen

@skittery
Copy link
Author

Didn't work. It's something to do with the way it reads the path.
When adding: $screenCapture->binPath = '/vendor/microweber/screen/bin/';
The error is: The system cannot find the path specified.
Just for the heck of it, I added this: $screenCapture->binPath = 'vendor/microweber/screen/bin/'; (left out the first slash) and it threw: 'vendor' is not recognized as an internal or external command, operable program or batch file.
When I add the actual full path on my computer it also throws an error.

Any idea why this is happening?
Such a shame, I really want to use this on my project...

@AndrewFeeney
Copy link

@skittery I realise some years have passed since you posted this comment so you've probably moved on, but for the sake of posterity.

It looks like you're specifying a relative path from your project root to the bin directory whereas I believe you need to specify an absolute path. In a Laravel context you can do this with the base_path() helper. Just pass the relative path string as the argument into that function and it will automatically prepend the absolute path to the project directory.

$screenCapture->binPath = base_path('vendor/microweber/screen/bin/');

@skittery
Copy link
Author

Hi @AndrewFeeney
Thanks a lot for replying to this.
I actually still do need this functionality so I wanted to give it another shot.
I installed a brand new Laravel instalation (6.0.1) and it worked perfectly.

I built the functionality in a controller using use Screen\Capture; and it actually worked.
I don't exactly know what the problem was last time, but I didn't have to use your method.
Thanks for the good work!

BTW - small question - Is there anyway I can take a screenshot, say of 1920 width and 1080 height, but when I clip it to be say 500 it will shrink it without cropping?

Thanks!

@AndrewFeeney
Copy link

@skittery Yes, I noticed shortly after I posted my comment that the default path should work. That said, I've started using a different package by Spatie: https://github.com/spatie/browsershot as it relies on Puppeteer and headless Chrome instead of the now deprecated phantomjs.

Regarding your question, it sounds like you are trying to resize the image after capture, preserving the aspect ratio. I can't see any methods on this package for that. You could use another library to resize the image after the fact. Spatie's package does offer this functionality by exposing their image library: https://github.com/spatie/browsershot#manipulating-the-image

@skittery
Copy link
Author

skittery commented Sep 11, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants