This is a script that uses gd2 and imagemagick to remap a rectangular image to a trapezoidal one, a la cover-flow. I originally made it to go with a modified version of imageflow. The function is intended to post-process a thumbnail .png image saved to the server by some other scripts, which already have given it a border and a reflection.

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
function imageDistortPerspective($src,$dst,$direction,$box,$params) {  
    // takes a box of dimensions $box ($box[0] = width ; $box[1] = height)
    // and transforms it into a trapezoid
    //   $direction = ['left'|'right'] sets which side is shrunk
    //   amount of vertical shrink is set by $params[0]
    //   the box is also shrunk horizontally to maintain ``aspect'' by $params[1]
    // the image is centered onto the original canvas by shifting it by shrink*w/2
    //
    //  xL,0
    //  --------
    //  | NW    ---------
    //  |                -------  < xR,y1
    //  |                   NE |
    //  |                      |
    //  |                   SE |
    //  |                -------  < xR,y2
    //  |SW     ---------
    //  --------
    //  xL,h
    //
   
    $pinch = $params[0]; $shrink = $params[1];
    $w = $box[0]; $h = $box[1];
    $y1 = round($h * $pinch);           // these are caretesian coordinates
    $y2 = round($h * (1-$pinch));       // see comment picture above for
    $xL = floor(($w * (1-$shrink))/2);  // which corners on the trapezoid
    $xR = $xL + round($w*$shrink);      // they correspond to
   
    // the corner coordinates of the ORIGINAL box (rectangle)
    $NWo = "0,0"; $NEo = "$w,0"; $SWo = "0,$h"; $SEo = "$w,$h";
   
    // the corner coordinates of the NEW box (trapezoid)
    switch($direction) {
        case 'left':
            $NW = "$xL,0"; $NE = "$xR,$y1"; $SW = "$xL,$h"; $SE = "$xR,$y2";
            break;
        case 'right':
            $NW = "$xL,$y1"; $NE = "$xR,0"; $SW = "$xL,$y2"; $SE = "$xR,$h";
            break;
    }
   
    // set imagemagick operation tags
    $op = "-matte    -virtual-pixel transparent   -distort Perspective";
    // shell-execute ``convert'' (imagemagick)
    exec("convert $src $op '$NWo,$NW  $NEo,$NE  $SWo,$SW  $SEo,$SE' $dst");
    // chmod($dst,0775);    // chmod to g+w so i can have write access
   
    // since imagemagick seems to create a larger png files than GD does,
    // we re-import and re-save with GD (overwrite)
    // $tmpimg = imagecreatefrompng($dst);
    // imagesavealpha($tmpimg,true);
    //      imagealphablending($tmpimg, false);
    // imagepng($tmpimg,$dst);
    // imagedestroy($tmpimg);
}