PHP Classes

GImage PHP Canvas to Image: Create and compose canvas images from other images

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 59%Total: 571 All time: 5,377 This week: 54Up
Version License PHP version Categories
canvas-image 2.0.16MIT/X Consortium ...5.5PHP 5, Graphics
Description 

Author

This package can create and compose canvas images from other images.

It provides classes for creating image objects defined by colored rectangles, images from files and text.

An additional canvas class can compose complex images using images defined by the other classes easier.

The composed images may be saved to files in the PNG and JPEG formats or displayed as the current script output.

Picture of Jose Luis Quintana
  Performance   Level  

 

Example

<?php
/*
 * This file is part of GImage.
 *
 * (c) Jose Quintana <https://joseluisq.net>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

/**
 * Creating a Presentation Card.
 *
 * @author Jose Quintana <https://joseluisq.net>
 */

namespace GImage\Examples;

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

require
__DIR__ . '/_config.php';
require
__DIR__ . '/../tests/bootstrap.php';

// Creating an avatar image
$avatar_image = new Image();
$avatar_image
   
// Image size (500x500)
   
->load('https://i.imgur.com/vLXIIoY.jpg')
    ->
centerCrop(100, 100)
    ->
setTop(60)
    ->
setLeft(70);

$about_text = new Text("MY AWESOME PRESENTATION CARD\n~ BY GIMAGE ~");
$about_text->setSize(16)
    ->
setWidth(300)
    ->
setLeft(210)
    ->
setLineHeight(1.5)
    ->
setTop(75)
    ->
setColor(204, 164, 116)
    ->
setFontface(BASE_PATH . '/fonts/Lato-Lig.ttf');

$twitter_text = new Text('@joseluisq/gimage');
$twitter_text
   
->setSize(11)
    ->
setWidth(70)
    ->
setLeft(410)
    ->
setTop(210)
    ->
setColor(130, 127, 125)
    ->
setFontface(BASE_PATH . '/fonts/Lato-Reg.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
   
])
    ->
setQuality(100)
    ->
toPNG()
    ->
draw()
    ->
save(__DIR__ . '/card.png');


Details

GImage

Build Status ![](https://packagist.org/packages/joseluisq/gimage) Latest Unstable Version Total Downloads License

> A PHP library for easy image handling. ?

__GImage__ is a simple and small library based on PHP Image Processing and GD for processing images easily.

A simple presentation card with GImage

_Presentation card built with GImage - View code example_

Features

  • Load an image from a local path, URL or image resource.
  • Create shapes such as rectangles or ellipses with opacity.
  • Resize, scale or crop images proportionally.
  • Rotate images, shapes or texts.
  • Embed text with custom TTF fonts.
  • Compose a pool of images with `Canvas`.
  • Swap image formats such as JPEG, PNG or GIF.
  • Save images locally or output them on the browser.
  • Save several copies of the same image.
  • Render an image in memory and return its resource.

Requirements

GImage requires PHP 7.4 or a recent PHP version with the latest GD Extension.

Install

composer require joseluisq/gimage

Usage

Load an external PNG image and save it as JPG:

<?php

use GImage\Image;

// PNG image (600x199)
$url = 'https://i.imgur.com/G5MR088.png';

$arch = new Image();
$arch
    // Load from URL
    ->load($url)
    // Scale to 50% (300x99)
    ->scale(0.5)
    // Change the format to JPG
    ->toJPG()
    // Saving in local path
    ->save('arch.jpg');

See GImage Website for detailed usage instructions and more advanced examples.

Changelog

Check out the CHANGELOG.md file.

Examples

See code examples directory.

Development

Run tests

Via Composer

composer install
composer run-script test

Via Docker (multi PHP versions test)

make docker-tests

Contribution

Feel free to send some Pull request or file an issue.

License

This work is primarily distributed under the terms of the MIT license.

©2015-present Jose Quintana


  Files folder image Files (48)  
File Role Description
Files folder image.github (1 directory)
Files folder imagedocs (2 files, 1 directory)
Files folder imageexamples (11 files)
Files folder imagesrc (5 files)
Files folder imagetests (2 files)
Accessible without login Plain text file .php-cs-fixer.dist.php Example Example script
Accessible without login Plain text file CHANGELOG.md Example Example script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE-MIT Lic. License text
Accessible without login Plain text file Makefile Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Data Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:571
This week:0
All time:5,377
This week:54Up
User Ratings User Comments (1)
 All time
Utility:85%StarStarStarStarStar
Consistency:85%StarStarStarStarStar
Documentation:-
Examples:80%StarStarStarStarStar
Tests:-
Videos:-
Overall:59%StarStarStar
Rank:1379
 
It is a nice Widget
10 years ago (Thomas Stoffregen)
80%StarStarStarStarStar