can you add automatically resize uploaded images to avoid oversized images, especially for mobile devices?
GUIDE
Open ./includes/functions_posting.php and locate the following line (~ line 434):
- Code: Select all
$file->move_file($config['upload_path'], false, $no_image);
Insert the following code before this line:
- Code: Select all
// Modify these params accordingly to suit your installation
$nMaxWidth = 690;
$nMaxHeight = 800;
$nJPEGCompression = 90;
if (strpos($file->get('mimetype'), 'image/') === 0) {
$sImgSourceFilename = $file->get('filename');
if ($arrImageData = @getimagesize($sImgSourceFilename)) {
$nImageType = $arrImageData[2];
switch ($nImageType) {
case IMG_GIF:
$imgSource = @imagecreatefromgif($sImgSourceFilename);
break;
case IMG_JPG:
$imgSource = @imagecreatefromjpeg($sImgSourceFilename);
break;
case IMG_PNG:
$imgSource = @imagecreatefrompng($sImgSourceFilename);
break;
case IMG_WBMP:
$imgSource = @imagecreatefromwbmp($sImgSourceFilename);
break;
default:
$imgSource = null;
break;
}
if ($imgSource) {
$nImgWidth = $arrImageData[0];
$nImgHeight = $arrImageData[1];
$doResize = false;
if ($nImgHeight > $nMaxHeight || $nImgWidth > $nMaxWidth) {
$doResize = true;
if ($nImgHeight > $nMaxHeight) {
$nRatio = ($nImgWidth / $nImgHeight);
$nImgHeight = $nMaxHeight;
$nImgWidth = round($nMaxHeight * $nRatio);
}
if ($nImgWidth > $nMaxWidth) {
$nRatio = ($nImgHeight / $nImgWidth);
$nImgWidth = $nMaxWidth;
$nImgHeight = round($nMaxWidth * $nRatio);
}
}
if ($doResize) {
$imgScaled = ImageCreateTrueColor($nImgWidth, $nImgHeight);
if (imagecopyresampled($imgScaled, $imgSource, 0, 0, 0, 0, $nImgWidth, $nImgHeight, $arrImageData[0], $arrImageData[1]))
imagejpeg($imgScaled, $sImgSourceFilename, $nJPEGCompression);
imageDestroy($imgScaled);
}
imageDestroy($imgSource);
}
}
Thanks in advance.
Kind regards,
Heinrich von Westphalen.