Kais Kiste | Sie sind hier: Kiste > IT & Web > Joomla Notes

Joomla Notes

Auto resizing pictures on upload(com_media, using ImageMagick)

First create a little function to have a bit more control, and to keep things clean. This will handle the resizing, with a few parameters. Put it before the delete() funtion in administrator/com_media/controllers/file.php, should be just after Line 128 (Advice: backup file.php beforehand)

...
function resize($file,$maxWidth,$maxHeight){
  global $mainframe;
  
  $extension= mb_substr($file,-4,4);
  exec('/usr/bin/convert "'.$file.'" -resize '.$maxWidth.'x'.$maxHeight.
       ' "'.$file.'.temp'.$extension.'"');
  $ret = JFile::delete($file);
  rename($file.'.temp'.$extension, $file);
  if($ret != 1){
    return $kaisMegaHolder;
  }
}
...

The call the function after a successful upload, place the code in the block around Line 117, In this example I use a maximum width of 712px and a max height of 1000px, but one can use any value.

...
$mainframe->enqueueMessage($this->resize($filepath,712,1000));
...