//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!

Euro Symbols on Dynamically Generated PDFs With PHP

Recently it became necessary for me to display a euro symbol in a PDF which was being generated using PDFlib on PHP.  After some trials and tribulations between googling for solutions and experimenting with different techniques I finally figured out what the problem is.

Firstly, the euro symbol can be passed to PDFLib as chr(128) but this will only work if the font has been loaded using the correct encoding. There's two methods of using PDFLib, the non-OOP approach:

$font = pdf_findfont($pdf, "Helvetica-Bold", "host", 0);

and the OOP method:

$font = $objPDF->load_font("Helvetica", "host", "");

The problem with the above methods of loading the fonts is that the encoding is determined by the OS which in FreeBSD is something other than winansi. So the solution is simple, modify your font loading call accordingly, specifying winansi as the encoding.  For the non-OOP approach:

$font = pdf_findfont($pdf, "Helvetica-Bold", "winansi", 0);

or the OOP method:

$font = $objPDF->load_font("Helvetica", "winansi", "");

I hope somebody finds this useful - there's not much information on googles search index which explains how to do this!

TextBuddy Now Supports Meteor!

Yes indeed.. it's been awhile, but it's finally here...

TextBuddy v1.1.41 has been released and I'm delighted to announce that Meteor is now supported!

Scorpion and the Turtle

A turtle was happily swimming along a river when a scorpion hailed it from the shore.

"Dear friend turtle!" called the scorpion. "Please let me climb upon your back and swim me to the other side of the river!"

"No," replied the turtle, "for if I do, you shall sting me, and I shall die."

"Nonsense!" replied the scorpion. "If I kill you in the middle of the river, you shall sink, and I shall drown and die with you."

The turtle thought this over, and saw the truth of the scorpion's statement. He let it upon his back and began swimming towards the other side of the river. Halfway across, he felt a sharp pain in the back of his neck.

"Why have you stung me?!" cried the turtle as his body began to stiffen. "Now you shall die as well!"

"Because it is in my nature. I can not do different." replied the scorpion as the turtle sank beneath the waters.

Quote of the Day: Perfection

It seems that perfection is reached not when there is nothing left to add, but when there is nothing left to take away. -- Antoine de Saint-Exupéry