Skip to content

Instantly share code, notes, and snippets.

@ryandemmer
Created April 1, 2020 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryandemmer/5605a441b01190cddbc1f4ccd37c49e3 to your computer and use it in GitHub Desktop.
Save ryandemmer/5605a441b01190cddbc1f4ccd37c49e3 to your computer and use it in GitHub Desktop.
Sample plugin to resize any image uploaded in Joomla / JCE
<?php
defined('JPATH_BASE') or die;
class PlgSystemUploadResize extends JPlugin
{
public function onContentAfterSave($context, &$object_file)
{
if (!preg_match('#^com_(media|jce)\.file$#', $context)) {
return;
}
if (!isset($object_file->filepath)) {
return;
}
$file = $object_file->filepath;
if (!is_file($file)) {
return;
}
// must be an image
if (!preg_match('#\.(jpg|jpeg|png)$#', $file)) {
return;
}
jimport('joomla.image.image');
$image = new JImage($file);
// resize image to fit 800 x 600
$image->resize(800, 600, false)->toFile($file);
// destroy handle
$image->destroy();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment