Introduction
Resizing and cropping images
Fegg\Tool\Trim resizes and crops JPEG, GIF and PNG images with the GD extension. JPEG images are rotated according to their Exif orientation.
How to use
Import Fegg\Tool\Trim and create it with new. Set the source image and the output path with Trim::setImage, resize it with one of the resize* methods, then save it with Trim::create or send it without saving with Trim::output.
<?php
namespace Site\Controller;
defined('FEGG_CORE_DIR') || exit;
use Fegg\Tool\Trim;
class Thumbnail extends Controller
{
public function index()
{
$trim = new Trim();
try {
$trim->setImage('path/to/image.jpg', 'path/to/thumbnail/300_300.jpg');
} catch (\RuntimeException $e) {
http_response_code(404);
$this->halt('Not Found');
}
$trim->resizeTrim(300, 300, 'm');
if ($trim->create()) {
// Output the saved image to the browser
$trim->view();
}
$this->halt();
}
}
Images of the CMS in other sizes
The images uploaded in the CMS are shown in other sizes through URLs such as /_fegg/site/up_dir/image/12/i-300_200.jpg, built by the getImageUrl modifier ({{ $page.data.image_id|getImageUrl:300:200 }}).
/_fegg/site/up_dir/image/{id}/{type}-{width}_{height}.{jpg|png|gif}
/_fegg/site/up_dir/image/{id}/{type}-{width}.{jpg|png|gif}
/_fegg/site/up_dir/image/{id}/{type}.{jpg|png|gif}
Fegg\Controller\Up_dir::image() makes a missing size with this class and saves it at that path; later requests get the saved file.
| type | Method | Result |
|---|---|---|
i | Trim::resize | Within the size, keeping the ratio |
c | Trim::resizeFill | Exactly the size, the empty area white or transparent |
f | Trim::resizeForce | Stretched to the size |
lt t rt l m r lb b rb | Trim::resizeTrim | Fills the size, keeping that part of the image |
A missing width or height is the original one. A name that is not one of these (x-10.jpg, I-10.JPG), an unknown image ID or a file that is not an image is a 404.
Two settings of htdocs/_fegg/site/config/upload_dir.php keep anyone from filling the disk or the memory with sizes:
| Key | Default | Remarks |
|---|---|---|
image_max | 2000 | Largest width or height that can be asked for. A larger one is a 404 |
image_cache_max | 50 | How many sizes of one image are saved. Past that, new sizes are made for each request (Trim::output) and not saved |
$upload_dir['image_max'] = 2000;
$upload_dir['image_cache_max'] = 50;
Methods
Trim::setImage
Set the source image and the output path
Trim::getSize
Get the width and height of the loaded image
Trim::resize
Resize the image to fit in the specified size, keeping its ratio
Trim::resizeFill
Resize the image keeping its ratio and fill the empty area (like object-fit: contain)
Trim::resizeForce
Resize the image to the specified size, ignoring its ratio
Trim::resizeTrim
Resize the image keeping its ratio and cut off the overflow (like object-fit: cover)
Trim::create
Save the image to the output path
Trim::output
Output the image to the browser without saving it
Trim::view
Output the saved image to the browser