Can You Build a CLI Image Drawing Laravel App with These Packages?

Share this article

Can You Build a CLI Image Drawing Laravel App with These Packages?

It’s time for our monthly hunt for new open source libraries to use and contribute to!

If you’re new to Sourcehunt, it’s our monthly post for promoting open source projects that seem interesting or promising and could use help in terms of Github stars or pull requests.

It’s our way of giving back – promoting projects that we use (or could use) so that they gain enough exposure to attract a wider audience, a powerful community and, possibly, new contributors or sponsors.

Sourcehunt logo


laracademy/interactive-make [216 ★]

Laravel Interactive Make is a plugin that lets you use Laravel’s make command interactively, without being verbose about exactly what it is you want to generate, as the following gif demonstrates:

Gif of Laravel Interactive Make in action

The tool will ask you about commands and sub-commands until you reach a point at which you’re happy with what got generated. No more looking up make commands!

There’s some issues to take care of, so get to helping!


reibengu/laravel-auto-validation [45 ★]

This package will let you remove all manual validation from Laravel Controllers, and instead rely on automatic validation that kicks instantly into action as a given controller and method are called – all you need to do is use the package’s trait in a controller, and set the service provider. Then, you define rules like so:

$rules = [
    'UserController' => [
        'register' => [
            'name'     => 'required|max:255',
            'email'    => ['required', 'email', 'max:255', Rule::unique('users')->where('status', 1)],
            'password' => 'required|min:6|confirmed',
            'gender'   => 'required|in:male,female',
            'birthday' => 'required|date_format:Y-n-j',
        ],
        'update' => function ($request) {
            return [
                'name'     => 'required|max:255',
                'email'    => 'required|email|max:255|unique:users,email,'.$request->user()->id,
                'gender'   => 'required|in:male,female',
                'birthday' => 'required|date_format:Y-n-j',
            ];
        },
    ],
];

return ['rules' => $rules];

The remainder of the process is automatic, and if validation fails, the request is automatically redirected back to the page from where it came, with error messages about validation flashed into session.

The project has no outstanding issues or pull requests, but why not add some? Here’s an idea: make it dead easy to customize these error messages, and integrate them with Laravel’s translator.


delight-im/PHP-PrivacyPolicy [26 ★]

In what’s a somewhat oddball case, PHP-PrivacyPolicy is a tool which programmatically generates a privacy policy and related documents for apps and digital tools and resources, in both human readable and machine oriented form.

Go ahead and add another paragraph or two you think might be needed one day, or even turn it into a SaaS and let people generate the policies online.


ozh/git-score [7 ★]

Inspired by the Python-based git-score, this command-line installable tool can be executed inside cloned repos on your machine in order to calculate information about contributors, and assign scores to all the people involved.

Here’s an example output, as taken from their README:

$ git score
name              commits  delta    (+)    (-)  files
Ozh                  2230  47906  66188  18282    500
Léo Colombaro         145   1038  15438  14400     84
lesterchan             43    553   1366    813     24
Nic Waller             13    322    434    112      5
BestNa.me Labs         12     10     21     11      4
Preovaleo              11     -5     28     33      7
Clayton Daley           9     13     29     16      2
Diftraku                8      0     16     16      8
Audrey                  4     10     21     11      4

No outstanding issues or PRs, but it would probably be fun to upgrade it with some extra stats to track – for example, a console based render of the activity for each user, similar to the activity graph on Github.com would be quite cool.


banago/PHPloy [1,119 ★]

PHPloy, deserves a mention here despite being quite a popular package already. PHPloy solves a very familiar pain point in deploying via (S)FTP, where users seldom know exactly which files they had changed, so they often opt to just re-upload the whole project. At the beginning, this doesn’t really matter, but as the project grows, the complexity of incremental updates grows exponentially with it, and why would one waste time on this when it’s so easy to use Git’s existing functionality to do it?

PHPloy detects changes between commits and only pushes the changed files online. It supports multiple servers, submodules, and rollbacks.


joseluisq/gimage [112 ★]

Gimage, another issue and PR-free package, is a tool for fluent building of images with PHP and the native GD library (extension).

Gimage provides a fluent interface for editing and drawing images and is little more than a very natural-sounding human-readable abstraction around GD’s native methods.

For example, here’s how to draw a green ellipse:

use GImage\Figure;

// Setting ellipse sizes
$ellipse = new Figure(500, 300);
$ellipse
    // Set ellipse type
    ->isEllipse()
    // Setting a green RGB color
    ->setBackgroundColor(170, 188, 147)
    // Creating the figure
    ->create()
    // Outputting image (PNG Figure by default) on the browser.
    ->output();

And here’s how to draw this image:

Example image made by Gimage

<?php

use GImage\Image;
use GImage\Text;
use GImage\Figure;
use GImage\Canvas;

$avatar_image = new Image();
$avatar_image
    ->load('http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=100.jpg')
    ->setTop(60)
    ->setLeft(70);

$about_text = new Text("MY AWESOME PRESENTATION CARD GENERATED WITH GIMAGE");
$about_text
    ->setSize(16)
    ->setWidth(300)
    ->setLeft(210)
    ->setTop(75)
    ->setColor(204, 164, 116)
    ->setFontface('fonts/Lato-Light.ttf');

$twitter_text = new Text('@username');
$twitter_text
    ->setSize(11)
    ->setWidth(70)
    ->setLeft(450)
    ->setTop(210)
    ->setColor(130, 127, 125)
    ->setFontface('fonts/Lato-Regular.ttf');

$canvas_figure = new Figure(550, 250);
$canvas_figure
    ->setBackgroundColor(47, 42, 39)
    ->create();

$avatar_box = new Figure($avatar_image->getWidth() + 16, $avatar_image
    ->getHeight() + 17);
$avatar_box
    ->setBackgroundColor(63, 56, 52)
    ->setLeft($avatar_image->getLeft() - 7)
    ->setTop($avatar_image->getTop() - 8)
    ->create();

$avatar_box2 = new Figure($avatar_image->getWidth() + 3, $avatar_image
    ->getHeight() + 19);
$avatar_box2
    ->setBackgroundColor(79, 72, 67)
    ->setLeft($avatar_image->getLeft() + 7)
    ->setTop($avatar_image->getTop() - 9)
    ->create();

$avatar_box3 = new Figure(120, 240);
$avatar_box3
    ->setBackgroundColor(63, 56, 52)
    ->create();

$line_vertical = new Figure(600, 10);
$line_vertical
    ->setBackgroundColor(119, 99, 77)
    ->setTop(240)
    ->create();

$line_horizontal = new Figure(1, 240);
$line_horizontal
    ->setBackgroundColor(79, 72, 67)
    ->setLeft(120)
    ->create();

$canvas = new Canvas($canvas_figure);
$canvas
    ->append([
      $line_horizontal,
      $avatar_box2,
      $avatar_box3,
      $avatar_box,
      $avatar_image,
      $about_text,
      $twitter_text,
      $line_vertical
    ])
    ->toPNG()
    ->draw()
    ->save('./card.png');

Further examples can be seen here.

The library needs more usage examples, so get to it!


That’s it for May. Found anything you could sink your teeth into?

As always, please throw your links at us with the #sourcehunt hashtag! If you build something with the projects we’ve mentioned, or if you submit an elaborate pull request you’d like to talk about, give us a shout and we’ll make sure the world knows about it!

Like last time, (that challenge remains unclaimed, by the way – there’s $500 in it for you if you do it!), we’re using the above packages for inspiration on creating a potentially useful app or two:

App+Tutorial idea(s) of the month:

  • an app which uses interactive make to add a new “draw image” command, and interactively asks the user about the kind of image to draw, using Gimage. The user’s input should be validated with auto-validation.
  • or, an app which lets people generate privacy policies interactively with PHP-PrivacyPolicy, and is deployed with PHPloy – a tutorial around all that.

Get in touch to find out how much these are worth to us!

Happy coding!

Frequently Asked Questions (FAQs) about Building a CLI Image Drawing Laravel App

What are the prerequisites for building a CLI Image Drawing Laravel App?

To build a CLI Image Drawing Laravel App, you need to have a basic understanding of PHP and Laravel. You should also be familiar with the command line interface (CLI) as you will be using it to interact with your Laravel application. Additionally, you need to have Laravel installed on your system. If you don’t have it installed, you can download it from the official Laravel website. You also need to install the GD library and Imagick extension for PHP, which are used for image manipulation.

How can I install the necessary packages for building a CLI Image Drawing Laravel App?

You can install the necessary packages using Composer, a dependency management tool for PHP. You need to run the following command in your terminal: composer require intervention/image. This command will install the Intervention Image package, which provides methods for image manipulation. You can also install the Laravel UI package by running composer require laravel/ui.

How can I create a new Laravel project?

You can create a new Laravel project by running the following command in your terminal: laravel new project-name. Replace “project-name” with the name of your project. This command will create a new directory with the specified name and install a fresh Laravel installation in that directory.

How can I use the Intervention Image package in my Laravel application?

After installing the Intervention Image package, you can use it in your Laravel application by adding the following line at the top of your PHP file: use Intervention\Image\ImageManagerStatic as Image. You can then use the Image facade to manipulate images.

How can I convert HTML to an image in Laravel?

You can convert HTML to an image in Laravel using the Browsershot package. First, you need to install the package by running composer require spatie/browsershot. Then, you can use the Browsershot::url('http://example.com') method to take a screenshot of a webpage and save it as an image.

How can I draw on an image in Laravel?

You can draw on an image in Laravel using the Intervention Image package. You can use the draw method to draw shapes on an image. For example, you can draw a rectangle on an image by using the following code: Image::make('public/foo.jpg')->rectangle(10, 10, 100, 100, function ($draw) { $draw->background('#0000ff'); })->save('public/bar.jpg');.

How can I display an image in a Laravel view?

You can display an image in a Laravel view by using the asset helper function. For example, if you have an image named “foo.jpg” in your public directory, you can display it in a view by using the following code: <img src="{{ asset('foo.jpg') }}" alt="Image">.

How can I resize an image in Laravel?

You can resize an image in Laravel using the Intervention Image package. You can use the resize method to resize an image. For example, you can resize an image to a width of 300 pixels and a height of 200 pixels by using the following code: Image::make('public/foo.jpg')->resize(300, 200)->save('public/bar.jpg');.

How can I handle errors in my Laravel application?

Laravel provides several ways to handle errors. You can use the abort function to throw an HTTP exception, which will be rendered by the exception handler. You can also use the report method to report an exception. Additionally, you can use the try and catch statements to handle exceptions.

How can I deploy my Laravel application?

You can deploy your Laravel application by uploading your project files to a web server. You can use FTP or SSH to upload your files. After uploading your files, you need to configure your web server to point to the “public” directory of your Laravel application. You also need to run composer install on your server to install the necessary dependencies.

Bruno SkvorcBruno Skvorc
View Author

Bruno is a blockchain developer and technical educator at the Web3 Foundation, the foundation that's building the next generation of the free people's internet. He runs two newsletters you should subscribe to if you're interested in Web3.0: Dot Leap covers ecosystem and tech development of Web3, and NFT Review covers the evolution of the non-fungible token (digital collectibles) ecosystem inside this emerging new web. His current passion project is RMRK.app, the most advanced NFT system in the world, which allows NFTs to own other NFTs, NFTs to react to emotion, NFTs to be governed democratically, and NFTs to be multiple things at once.

BrunoScliCommand linedeploydeploymentftplaravelOOPHPPHPphp frameworkphp frameworkssftpsourcehunt
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week