Image

Image manipulation using GD library.

5 stars out of 5

20000
  • AuthorTaufik Nurrohman
  • Maintainer1
  • Member
  • Version2.4.0
Table of Contents
  1. Usage
    1. Create Image
    2. Read Image Dimension
    3. Read 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. Render Image
  2. Page Image Data

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

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('http://127.0.0.1/path/to/image.png');

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

Read Image Dimension

Read current image width and height:

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

Read Image Type

Read 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 new 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:

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

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

Render Image

Render image to the browser window as a file:

status(200, ['content-type' => $blob->type]);
echo $blob;
status(200, ['content-type' => $blob->type]);
echo $blob->blob(null, 50); // Set image quality to 50%

Load image from file cache if available:

status(200, ['content-type' => $blob->type]);
if (is_file($file = '.\path\to\image.png')) {
    echo file_get_contents($file);
} else {
    echo $blob->blob($file);
}

Page Image Data

This extension 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 have been set the image and images property values explicitly in the page header:

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

0 Comments

No comments yet.