PHP Classes
elePHPant
Icontem

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

Recommend this page to a friend!
  Info   View files Example   View files View files (25)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-10-10 (7 days ago) RSS 2.0 feedStarStarStarStar 64%Total: 503 This week: 9All time: 5,606 This week: 91Up
Version License PHP version Categories
canvas-image 2.0.8MIT/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.

  Performance   Level  
Name: Jose Luis Quintana <contact>
Classes: 2 packages by
Country: Peru Peru
Age: ???
All time rank: 29207 in Peru Peru
Week rank: 55 Up1 in Peru Peru Up

Details

GImage

Build Status Latest Stable Version Latest Unstable Version Total Downloads License

> A PHP library for easy image handling.

A simple presentation card with GImage

_Presentation card built with GImage - view sample code_

Features

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

  • Load an image from local path or URL.
  • Create shapes such as rectangles or ellipses with opacity.
  • Resize or scale images proportionally.
  • 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 and GIF.
  • Save images in local or output on the browser.
  • Save several copies of the same image.

Requirements

GImage requires PHP 7.x and latest GD Extension.

Install

composer require joseluisq/gimage

Usage

Image

Working with an external JPG image and output it on browser as PNG format:

<?php

use GImage\Image;

$avatar = new Image();
$avatar
	// Loading a JPEG image (200x200) from an URL (or local path too)
	->load('http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=200.jpg');
	// Scaling to 50% (100x100)
	->scale(50)
	// Set 50% of opacity
	->setOpacity(0.5)
	// Changing the format to PNG
	->toPNG()
	// Preserving the image before saving or outputting
	->preserve()
	// Saving to local path
	->save('octocat.png')
	// Preparing for destroy the resource after outputting
	->preserve(false)
	// Outputting on the browser and destroy the resource.
	->output();

Figure

Creating a green ellipse:

<?php

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();

Text

Creating a rectangle with Text embedded:

<?php

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

$figure = new Figure(400, 250);
$figure
    ->isRectangle()
    ->setBackgroundColor(47, 42, 39)
    ->create();

$text = new Text('My awesome text!');
$text
    ->setWidth(400)
    ->setHeight(250)
    ->setLineHeight(1.2)
    ->setAlign('center')
    ->setValign('center')
    ->setSize(22)
    ->setColor(255, 255, 255)
    ->setFontface('./fonts/Lato-Bol.ttf');

$canvas = new Canvas($figure);
$canvas
    ->append($text)
    ->toPNG()
    ->draw()
    ->save('./text.png');

Creating a simple presentation card

Presentation card example using Image, Canvas, Figure and Text.

<?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');

For more examples check out the ./examples dir.

Changelog

Check out the CHANGELOG.md file.

Documentation

View the documentation API

Contribution

If you would like to contribute pull requests and issues will be welcome! Feature requests are welcome. Please before sending some feature requests make sure provide as much detail and context as possible.

License

MIT license

© 2017 Josť Luis Quintana

  Files folder image Files  
File Role Description
Files folder imageexamples (9 files)
Files folder imagesrc (5 files)
Files folder imagetests (2 files)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
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 composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE.md 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

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file card.php Example Example script
  Accessible without login Plain text file crop.php Example Example script
  Accessible without login Plain text file ellipse.php Example Example script
  Accessible without login Plain text file jpg2png.php Example Example script
  Accessible without login Plain text file rectangle.php Example Example script
  Accessible without login Plain text file render.php Example Example script
  Accessible without login Plain text file rotate.php Example Example script
  Accessible without login Plain text file text.php Example Example script
  Accessible without login Plain text file _config.php Aux. Auxiliary script

  Files folder image Files  /  src  
File Role Description
  Plain text file Canvas.php Class Class source
  Plain text file Figure.php Class Class source
  Plain text file Image.php Class Class source
  Plain text file Text.php Class Class source
  Plain text file Utils.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file GImageTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:503
This week:9
All time:5,606
This week:91Up
User Ratings User Comments (1)
 All time
Utility:91%StarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:-
Examples:100%StarStarStarStarStarStar
Tests:-
Videos:-
Overall:64%StarStarStarStar
Rank:804
 
It is a nice Widget
3 years ago (Thomas Stoffregen)
80%StarStarStarStarStar