Tuesday, April 15, 2014

[phpbb][Solved] Auto resize uploaded image with ReIMG Image Resizer mod (with images)

I have tried install mod REImg , and not work for me (maybe I missed some steps.)
And also add the BB code, the found BB code is for control the image display width and height only, not really for reducing image size. After searching the related topic on internet I found the correct way to make that:

This is the official site of REImg:
Download link of the mode:


The code I missed, copied from this link

Look for this line in "your_php_path/includes/functions_posting.php"

$file->move_file($config['upload_path'], false, $no_image);
And then replace this code:
  // Modify these params accordingly to suit your installation
  $nMaxWidth = 690;
  $nMaxHeight = 800;
  $nJPEGCompression = 90;
  // You may want to modify this "if this attachment an image" check.. at the moment I'm using this:
  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;
      }
      // Assuming we managed to read in the image OK..
      if ($imgSource) {
        $nImgWidth = $arrImageData[0];
        $nImgHeight = $arrImageData[1];
        $doResize = false;
        if ($nImgHeight > $nMaxHeight || $nImgWidth > $nMaxWidth) {
          $doResize = true;
          // otherwise image width and/or height exceed our max dimensions
          // work out the new widht/height for downward proportional resampling:
          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);
      }
    }
  }
$file->move_file($config['upload_path'], false, $no_image);

* * *
Testing and result:
 1) Choose a big image to upload:

 2) upload to the forum


3) At the preview page, the image was resized to 690 x 413px

4) the img link



Reference:

No comments :

Post a Comment