PHP code example of phpixie / image

1. Go to this page and download the library: Download phpixie/image library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phpixie / image example snippets


$image = new \PHPixie\Image();

//You can also specify which driver to use ('gd', 'imagick' or 'gmagick')
//With 'gd' being the default one
$image = new \PHPixie\Image('gmagick');

$image = $builder->components()->image();

// /assets/config/image.php
return array(
    'defaultDriver' => 'imagick'
);

// Create a new 100x200 white image
$img = $image->create(100, 200);

// Create a new 100x200 image filled with red color at 0.5 opacity
$img = $image->create(100, 200, 0xff0000, 0.5);

// Read a file from disk
$img = $image->read('/some/file.png');

// Load a file from file contents
$data = file_get_contents('/some/file.png');
$img = $image->load($data);

$driver = $image->driver('imagick');
$img = $driver->create(100, 200, 0xff0000, 0.5);
$img = $driver->read('/some/file.png');
$img = $driver->load($data);

//Or using an optional parameter
$img = $image->create(100, 200, 0xff0000, 0.5, 'imagick');
$img = $image->read('/some/file.png', 'imagick');
$img = $image->load($data, 'imagick');

// Save to file
// By default Image will guess the image format
// from the file name
$img->save('pixie.png');

// You can always specify the format and quality manually though
$img->save('pixie.jpg', 'jpg', 90);

// This will render the image data into a variable,
// useful for sending directly to the browser
$data = $img->render('png');

// This method also supports quality specification
$data = $img->render('jpg', 90);

// Resize it to 400px width, aspect ratio is maintained
$img->resize(400);

// Resize to 200px in height
$img->resize(null, 200);

// Resize to fit in a 200x100 box
// A 300x300 image would become 100x100
// it's as if you specify the maximum size
$img->resize(200, 100);

// Resize to "fill" a 200x100 box
// A 300x300 image would become 200x200
// it's as if you specify the minumum size
$img->resize(200, 100, false);

//Scale image using a ratio
//This would make it twice as big
$img->scale(2);

//Crop image to 100x150 with 10 horizontal offset
//and 15 vertical
$img->crop(100, 100, 10, 15);

//Let's assume $img is 300x200
//and we want to make 100x100 avatars.

//Note how you can chain the methods together
$img->resize(100, 100, false) //becomes 150x100
	->crop(100, 100)
	->save('avatar.png');

//We even have a predefined fill() function for this =)
$img->fill(100, 100)->save('avatar.png'); //that's it

//Rotate the image 45 degrees counter clockwise
//filling the background with semitransparent white
$img->rotate(45, 0xffffff, 0.5);

$img->flip(true); //flip horizontally
$img->flip(false, true); //flip vertically
$img->flip(true, true); //flip bloth

$meadow = $image->read('meadow.png');
$fairy  = $image->read('fairy.png');
$flower = $image->read('flower.png');

//Put fairy at coordinates 40, 50
$meadow->overlay($fairy, 40, 50)
	->overlay($flower, 100, 200)
	->save('meadow2.png');

$large = $image->read('large.png');// 500x300
$small = $image->read('small.png');// 100x100

//Make transparent canvas the size of large image
$canvas = $image->create($large->width(), $large->height();
$canvas->overlay($small)
	->overlay($large)
	->save('merged.png');

//Make white background
$img = $this->pixie->create(500, 500, 0xffffff, 1);

//Write "tinkerbell" using font.ttf font and font size 30
//Put it in coordinates 50, 60 (baseline coordinates)
//And make it half transparent red color
$img->text("Tinkerbell", 30, '/path/font.ttf', 50, 60, 0xff0000, 0.5);

//Wrap text so that its 200 pixel wide
$text = "Trixie is a nice little fairy that like spicking flowers";
$img->text($text, 30, '/path/font.ttf', 50, 60, 0xff0000, 0.5, 200);

//Increase Line spacing by 50%
$img->text($text, 30, '/path/font.ttf', 50, 60, 0xff0000, 0.5, 200, 1.5);

//Write text under a 45 degree counter clockwise angle:
$img->text("Tinkerbell", 30, '/path/font.ttf', 50, 60, 0xff0000, 0.5, null, 1, 45);