Page 1 of 1

How to do it in Imagemagick

Posted: 2009-12-21T05:43:58-07:00
by chan15
1. resize the image to new image by the short side to 120px at first, for example 300 x 200 -> 180 x 120, 400 x 700 -> 120 x 210, and not output yet.
2. make a thumbnail by above image, cut the middle square by request, for exmple 75 x 75
example http://www.drama.com.tw/question/cat.jpg

Code: Select all

<?php
/**
* $dir - source directory
* $file - file name
* $size - dimension
*/
function imgSquareThumbnail($dir, $file, $size) {
    $ratioSize = 120; // resize image to 120 regardless the size
    $realPosition = $dir . $file;
    $thumbDir = $dir . 'thumbnails/'; // directory for thumbnail

    if (file_exists($realPosition)) {
        $fileName = current(explode('.', $file)); // filanem
        $ext = strtolower(end(explode('.', $realPosition))); // extension name
        $newName = $fileName . '_' . $size . 'x' . $size . '.' . 'jpg'; // new file name
        
        // show image directly if file exist
        if (file_exists($thumbDir . $newName)) {
            echo '<img src="' . $thumbDir . $newName . '" />';
            return;
        }
        
        switch ($ext) {
            case 'jpg':
            case 'jpeg':
                $src = imagecreatefromjpeg($realPosition);
                break;
            case 'gif':
                $src = imagecreatefromgif($realPosition);
                break;
            case 'png':
                $src = imagecreatefrompng($realPosition);
                break;
        }
        
        $srcW = imagesx($src); // source width
        $srcH = imagesy($src); // source height
        
        if ($srcW >= $srcH) {
            // resize by height
            $newW = intval($srcW / $srcH * $ratioSize); // new width
            $newH = $ratioSize; // new hieght
        } else {
            // resize by width
            $newW = $ratioSize; // new width
            $newH = intval($srcH / $srcW * $ratioSize); // new height
        }
        
        // resize to 120
        $im = imagecreatetruecolor($newW, $newH);
        imagecopyresized($im, $src, 0, 0, 0, 0, $newW, $newH, $srcW, $srcH);
        
        // resize to request
        $im2 = imagecreatetruecolor($size, $size);
        $coordX = ($newW - $size) / 2;
        $coordY = ($newH - $size) / 2;
        imagecopyresized($im2, $im, 0, 0, $coordX, $coordY, $size + $coordX, $size + $coordY, $newW, $newH);
        
        imagejpeg($im2, $thumbDir . $newName, 100); // output
        imagedestroy($im);
        imagedestroy($im2);

        echo '<img src="' . $thumbDir . $newName . '" />';
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>image customize</title>
<style type="text/css">
body {
    font-size: 12px;
}
</style>
</head>

<body>

<?php
imgSquareThumbnail('img/', 'cat.gif', 75);
?><br />
</body>
</html>


Re: How to do it in Imagemagick

Posted: 2009-12-21T10:43:25-07:00
by fmw42
1. resize the image to new image by the short side to 120px at first, for example 300 x 200 -> 180 x 120, 400 x 700 -> 120 x 210, and not output yet.
2. make a thumbnail by above image, cut the middle square by request, for exmple 75 x 75
example http://www.drama.com.tw/question/cat.jpg
1. see the ^ character for resizing at
http://www.imagemagick.org/script/comma ... php#resize
http://www.imagemagick.org/script/comma ... p#geometry

2. see -thumbnail (but you can use ^ with thumbnail and do the resize and thumbnail at the same time with just thumbnail)
http://www.imagemagick.org/script/comma ... #thumbnail

also

see -gravity center -crop ... +repage at
http://www.imagemagick.org/script/comma ... s.php#crop
http://www.imagemagick.org/Usage/crop/#crop


try

convert image -thumbnail "75x75^" -gravity center -crop 75x75+0+0 +repage result

or

convert image -resize "120x120^" -strip -gravity center -crop 75x75+0+0 +repage result

Not sure which you need (probably the latter)?

Re: How to do it in Imagemagick

Posted: 2009-12-22T22:24:32-07:00
by chan15
Thansk for your answer, could you tell what's differents among resize, thumbnail and geometry?

Re: How to do it in Imagemagick

Posted: 2009-12-22T22:32:39-07:00
by chan15
120x120^ still make the image become another image that not large than 120.

Re: How to do it in Imagemagick

Posted: 2009-12-23T10:40:48-07:00
by fmw42
-thumbnail is essentially -resize ... -strip

that is it basically removes the excess header information and profiles, which is what -strip does.

image geometry defines how the arguments of -thumbnail and -resize are used to control how it is scaled up or down. (-geometry is another thing all together and is used when compositing images to identify where to do the composite)

"120x120^" is used to scale the smaller image dimension (whether width or height) to 120 pixels. the other dimension is scaled in proportion. If your image is square, then it is no different from "120x120". Note the quotes around it are important; otherwise you need to escape the ^ symbol. Read all the page at http://www.imagemagick.org/script/comma ... p#geometry

The ^ symbol is new as of 6.3.8-2. If you are on an older IM, it won't work.