Trim an image using PHP and GD

I was looking for a way to trim images with the GD library. So I made a function to do just that. The function, which I only naturally called imagetrim(), trims a image much like Photoshop's trim feature. Additionally, you may add padding too if desired.

So, here's the function:

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
<?php
// Trims an image then optionally adds padding around it.
// $im  = Image link resource
// $bg  = The background color to trim from the image
// $pad = Amount of padding to add to the trimmed image
//        (acts simlar to the "padding" CSS property: "top [right [bottom [left]]]")
function imagetrim(&$im, $bg, $pad=null){

    // Calculate padding for each side.
    if (isset($pad)){
        $pp = explode(' ', $pad);
        if (isset($pp[3])){
            $p = array((int) $pp[0], (int) $pp[1], (int) $pp[2], (int) $pp[3]);
        }else if (isset($pp[2])){
            $p = array((int) $pp[0], (int) $pp[1], (int) $pp[2], (int) $pp[1]);
        }else if (isset($pp[1])){
            $p = array((int) $pp[0], (int) $pp[1], (int) $pp[0], (int) $pp[1]);
        }else{
            $p = array_fill(0, 4, (int) $pp[0]);
        }
    }else{
        $p = array_fill(0, 4, 0);
    }

    // Get the image width and height.
    $imw = imagesx($im);
    $imh = imagesy($im);

    // Set the X variables.
    $xmin = $imw;
    $xmax = 0;

    // Start scanning for the edges.
    for ($iy=0; $iy<$imh; $iy++){
        $first = true;
        for ($ix=0; $ix<$imw; $ix++){
            $ndx = imagecolorat($im, $ix, $iy);
            if ($ndx != $bg){
                if ($xmin > $ix){ $xmin = $ix; }
                if ($xmax < $ix){ $xmax = $ix; }
                if (!isset($ymin)){ $ymin = $iy; }
                $ymax = $iy;
                if ($first){ $ix = $xmax; $first = false; }
            }
        }
    }

    // The new width and height of the image. (not including padding)
    $imw = 1+$xmax-$xmin; // Image width in pixels
    $imh = 1+$ymax-$ymin; // Image height in pixels

    // Make another image to place the trimmed version in.
    $im2 = imagecreatetruecolor($imw+$p[1]+$p[3], $imh+$p[0]+$p[2]);

    // Make the background of the new image the same as the background of the old one.
    $bg2 = imagecolorallocate($im2, ($bg >> 16) & 0xFF, ($bg >> 8) & 0xFF, $bg & 0xFF);
    imagefill($im2, 0, 0, $bg2);

    // Copy it over to the new image.
    imagecopy($im2, $im, $p[0], $p[3], $xmin, $ymin, $imw, $imh);

    // To finish up, we replace the old image which is referenced.
    $im = $im2;
}
?>

Comments

cant get this tim function to work

can someone please give an example of using this function from a source file please?
cheers
Gavin

Here's an example!

Have the function somewhere in the script and this will be a good example:

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
<?php

$imw = 500; $imh = 500;
$im = imagecreatetruecolor($imw,$imh); // Create image

// Create some colors
$bg = imagecolorallocate     ($im,0xFF,0xFF,0x00);     // Background color (yellow)
$ln = imagecolorallocatealpha($im,0xFF,0x00,0x00,110); // Line color (transparent red)

imagefill($im,0,0,$bg);

// Generate something a little random.
$r = $imh/10; $t2 = array($r,0);
for ($a=0;$a<360;$a++){
  $noise = rand(-$r,$r);
  $t1 = $t2;
  $t2[0] = (($r+$noise) * cos(deg2rad($a)));
  $t2[1] = (($r+$noise) * sin(deg2rad($a)));
  imageline($im, ($imw/2)+$t1[0], ($imh/2)+$t1[1], ($imw/2)+$t2[0], ($imh/2)+$t2[1], $ln);
}

// Padding: 1px top, 2px right, 3px bottom, 4px left
//imagetrim($im,$bg,'1 2 3 4');

// Padding: 1px top, 2px right, 3px bottom, 2px left
//imagetrim($im,$bg,'1 2 3');

// Padding: 1px top, 2px right, 1px bottom, 2px left
//imagetrim($im,$bg,'1 2');

// Padding: 5px top, 5px right, 5px bottom, 5px left
//imagetrim($im,$bg,'5');

// Padding: 0px top, 0px right, 0px bottom, 0px left
imagetrim($im,$bg);

// Set the header and output image.
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

?>

Hope this helps! :)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You can use BBCode tags in the text, URLs will automatically be converted to links.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Copy the characters (respecting upper/lower case) from the image.