首先安装imagemagick软件。
windows下,到官网
http://www.imagemagick.org/script/download.php
下载,安装。
然后,确保php插件安装成功
[img]http://dl2.iteye.com/upload/attachment/0125/4492/c0ecc02e-5cee-3327-b9a7-1a6a8e40f033.png[/img]
(说明实在不想装imagick也行,用GD也是可以的。)
然后,composer安装imagine
composer.json
{
"require": {
"imagine/imagine":"0.7.1"
}
}
composer install
现在可以使用了
代码,目录请读者自己建立。
<?php
require ("../vendor/autoload.php");
$imagine = new Imagine\Imagick\Imagine();
$size = new Imagine\Image\Box(40, 40);
$mode = Imagine\Image\ImageInterface::THUMBNAIL_INSET;
//取完整的图,不保证最终尺寸。不变形
$imagine->open('D:/workspace/play/imgsrc/original.jpg')
->thumbnail($size, $mode)
->save('D:/workspace/play/imgdist/dist.jpg');
//保证最终尺寸,把多余的图像切除,不变形
$mode = Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND;
$imagine->open('D:/workspace/play/imgsrc/original.jpg')
->thumbnail($size, $mode)
->save('D:/workspace/play/imgdist/dist_outbound.jpg');
echo "ok";
下图:original.jpg
[img]http://dl2.iteye.com/upload/attachment/0125/4496/2e03e995-5d8a-3034-a178-da676e402ba6.jpg[/img]
下图:dist.jpg
[img]http://dl2.iteye.com/upload/attachment/0125/4498/35026003-d5f6-3f43-9088-1659524c611f.jpg[/img]
下图:dist_outbound.jpg
[img]http://dl2.iteye.com/upload/attachment/0125/4500/e56684d6-37f9-3460-873e-cd06a11b38fe.jpg[/img]