automatically resize uploaded images

the place to talk about tennis in general, and any other topic you wish
Forum rules
Speak English and don't hijack topics, troll, use profanities nor insults

automatically resize uploaded images

Postby Heinrich von Westphalen » 15 Oct 2014, 12:33

Hello manutoo,
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.
Heinrich von Westphalen
crazy of the little yellow ball
crazy of the little yellow ball
 
Messages: 1479
Gaming Since: 12 Dec 2012, 05:48
Location: Germany

Re: automatically resize uploaded images

Postby manutoo » 16 Oct 2014, 05:21

Hello,

I turned on the thumbnail generation for large picture (couldn't set it for the dimensions, only the file size :sweat: ).
Attachments
2014-10 Abandoned Palace #1.jpg
ManuTOO
== Mana Games ==

>> I don't answer Private Message, except if it's really a _Private_ topic <<
User avatar
manutoo
Game Author
 
Messages: 18694
Gaming Since: 24 Jan 2004, 15:38
Location: France

Re: automatically resize uploaded images

Postby Nigogu » 03 Nov 2014, 05:42

It looks like you solved that problem manutoo, but can this be applied in old attachments :?:

Regards :) .
User avatar
Nigogu
crazy of the little yellow ball
crazy of the little yellow ball
 
Messages: 3562
Gaming Since: 26 Mar 2012, 16:48

Re: automatically resize uploaded images

Postby manutoo » 04 Nov 2014, 04:38

Nigogu,
unfortunately, no.
ManuTOO
== Mana Games ==

>> I don't answer Private Message, except if it's really a _Private_ topic <<
User avatar
manutoo
Game Author
 
Messages: 18694
Gaming Since: 24 Jan 2004, 15:38
Location: France


Return to Off Topic

Who is online

Users browsing this forum: No registered users and 7 guests