Tuesday, May 3, 2011

Looking for a PHP Image Library... rounded corners & resizing

Just looking for a good PHP Image Library, I want to display images with rounded corners, do some resizing, and blur some other pictures either on the fly, or on upload.

From stackoverflow
  • Have a go with http://wideimage.sourceforge.net/wiki/MainPage

    It doesn't do it out of the box but you could have a partially transparent PNG that you could put on top of your original image, making it blurry.

  • I'd suggest to have a look around ImageMagick.

    There are excellent wrappers for the library in PHP too: http://www.imagemagick.org/script/api.php#php

  • this is a dirty hack i did for a project a while ago. it applies an grayscale image as a transparency map to another image (black is transparent, white opaque. scaling the map to the images proportions is supported). you could create a fitting rounded-corners transparency map (including antialiasing, whoo!).

    it's slow because it's pure php, but i always cache the results anyway.

    $image and $transparencyMap are gd image ressources, so you have to imagecreatefromxyz them yourself.

    <?php
    function applyTransparencyMap($image, $transparencyMap) {
        if (!function_exists('extractrgb')) {
            function extractrgb($rgb) {
                $a = ($rgb >> 24) & 0xFF;
                $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >> 8) & 0xFF;
                $b = $rgb & 0xFF;
                return array($r, $g, $b, $a);
            }
        }
    
        $sx = imagesx($image);
        $sy = imagesy($image);
        $tx = imagesx($transparencyMap);
        $ty = imagesy($transparencyMap);
        $dx = $tx / $sx;
        $dy = $ty / $sy;
    
        $dimg = imagecreatetransparent(imagesx($image), imagesy($image));
    
        for ($y = 0; $y<imagesy($image); $y++) {
            for ($x = 0; $x<imagesx($image); $x++) {
                $intcolor                = imagecolorat($image, $x, $y);
                $intalpha                = imagecolorat($transparencyMap, floor($x*$dx), floor($y*$dy-1));
                list($tr, $tg, $tb, $ta) = extractrgb($intalpha);
                $alphaval                = 127-floor(($tr+$tg+$tb)/6);
                list($r, $g, $b, $a)     = extractrgb($intcolor);
                $targetAlpha             = max(0, min(127,$alphaval+$a));
                $sct                     = imagecolorallocatealpha($image, $r, $g, $b, $targetAlpha);
                imagesetpixel($dimg, $x, $y, $sct);
            }
        }
    
        return $dimg;
    }
    ?>
    

    on the other hand, better use wideimage, as apikot suggested. does the same and more.

  • You can try with this library http://freelogic.pl/thumbnailer/examples

0 comments:

Post a Comment