//jerrywalsh.org

coding, hacking, startups, computer security, technology and more

Automatically Resizing Image Attachment Uploads With PHPBB

I manage a number of PHPBB bulletin boards and one such board receives quite alot of image uploads from users who let's say aren't the most tech saavy. Some of the users know nothing about image file sizes and resolution and they end up uploading enormous images. Rather than instruct each user how they can resize images I figured it would be better if PHPBB would just resize the image on the fly so that the dimensions ensure better file sizes and don't break the layout of the site. I checked the PHPBB administration control panel and it seems that this simple requirement's not possible out of the box. Googling a little for it didn't show up much so I dived in to the source..

How to get PHPBB to automatically resize uploaded images?

I applied this mod to PHPBB3, it would not surprise me thou if this also applied cleanly to a PHPBB2 installation. Open ./includes/functions_posting.php and locate the following line (~ line 434):

1
$file->move_file($config['upload_path'], false, $no_image);

Insert the following code BEFORE this line:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  // 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) {
          $nRatio = (int)($nImgHeight / $nImgWidth);
        } else {
          $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 = (int)($nImgWidth / $nImgHeight);
            $nImgHeight = $nMaxHeight;
            $nImgWidth = round($nMaxHeight * $nRatio);
          }
          if ($nImgWidth > $nMaxWidth) {
            $nRatio = (int)($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);
      }
    }
  }

Success! This patch worked a treat; no more oversized images!