Mecha CMS

Add extra functionality to the core engine.


Table of Contents
  1. API Usage 
    1. Create Image 
    2. Get Image Dimension 
    3. Get Image Type 
    4. Resize Image 
    5. Scale Image 
    6. Fit Image 
    7. Crop and Resize Image 
    8. Crop Image without Resize 
    9. Save Image 
    10. Draw Image 
  2. Image API with Page Extension 

Image resizer and cropper.

Download Stable Version Download Development Version

Something looks broken? We accept bounties to fix them faster.

v2.3.0222 Downloads
v2.2.4143 Downloads
v2.2.3152 Downloads

This extension provides various API to work with BMP, GIF, JPEG, PNG and WEBP images.

API Usage 

Create Image 

Create a new image from a file path, an external URL or a Base64 URL:

// Create image from file
$blob = new Image('.\path\to\image.png');

// Create image from remote URL
$blob = new Image('');

// Create image from Base64 URL
$blob = new Image('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAy…');

Get Image Dimension 

Get current image width and height:

$width = $blob->width;
$height = $blob->height;

Get Image Type 

Get current image MIME type:

$type = $blob->type;

Resize Image 

Resize image to an absolute width and height:

$width = 100;
$height = 50;
$blob->resize($width); // Is the same as `->resize($width, $width)`
$blob->resize($width, $height);

Scale Image 

Resize image width and height relative to the current image width and height:

$blob->scale(50); // 50% of current image dimension
$blob->scale(200); // 200% of current image dimension

Fit Image 

Resize image and make sure that the width and height will not overflow the maximum width and height:

$max_width = 100;
$max_height = 100;

$blob->fit($max_width, $max_height);

Crop and Resize Image 

Resize and crop image to the center:

$width = 100;
$height = 100;

$blob->crop($width); // Is the same as `->crop($width, $width)`
$blob->crop($width, $height);

Crop Image without Resize 

Crop image with custom X and Y coordinate:

$left = 20;
$top = 20;
$width = 100;
$height = 100;

$blob->crop($left, $top, $width, $height);

Save Image 

Store the modified image blob to a file:

$blob->store('.\path\to\image-1.png', 50); // Set image quality to 50%

Draw Image 

Show image to the browser window as a file:

$blob->draw(null, 50); // Set image quality to 50%

Draw and store to a file:

$blob->draw('.\path\to\image-1.png', 50);

Draw as JPEG image:

$blob->draw(null, 50, 'image/jpeg');

Image API with Page Extension 

This extension will also add image and images property to the current $page object. The image property will contain the first image URL found in the current page content, and images property will contain list of images URL found in the current page content. This extension will skip the parsing process if you set the image and images property values explicitly in the page file header:

<?php foreach ($pages as $page): ?>
    <?php if ($image = $page->image(72, 72)): ?>
      <img alt="" src="<?= $image; ?>">
    <?php endif; ?>
<?php endforeach; ?>