Skip to main content
Version: Fegg 5.x

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.

Controller
<?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.

typeMethodResult
iTrim::resizeWithin the size, keeping the ratio
cTrim::resizeFillExactly the size, the empty area white or transparent
fTrim::resizeForceStretched to the size
lt t rt l m r lb b rbTrim::resizeTrimFills 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:

KeyDefaultRemarks
image_max2000Largest width or height that can be asked for. A larger one is a 404
image_cache_max50How many sizes of one image are saved. Past that, new sizes are made for each request (Trim::output) and not saved
htdocs/_fegg/site/config/upload_dir.php
$upload_dir['image_max'] = 2000;
$upload_dir['image_cache_max'] = 50;

Methods​