PHP Imagick:将图像对象复制到另一个图像对象,但分辨率更高

时间:2012-10-11 02:17:20

标签: php resize imagick

为什么这不起作用?

var_dump($Obj_old_image->getImageResolution());
//array(2) { ["x"]=> float(72) ["y"]=> float(72) } 

$Obj_new_image->setResolution(200, 200);

var_dump($Obj_new_image->getImageResolution());
//array(2) { ["x"]=> float(200) ["y"]=> float(200) } 

$Obj_new_image->setImage($Obj_old_image);

$Obj_new_image->setImageFormat("png");
header("Content-Type: image/png");
echo $Obj_new_image;

它返回相同的分辨率。

这应该在不损失质量的情况下调整SVG图像的大小。

2 个答案:

答案 0 :(得分:0)

根据您发布的代码,我不知道$Obj_old_image是什么或者它在做什么。但是,以下代码将与PHP和安装的标准GD lib一起使用:

// image to be scaled
$rawImgPath = './test.jpg';

// new image size (guessing you know the new size)
$newImgSize['w'] = 200;
$newImgSize['h'] = 200;

// the steps to create the new scaled image
$rawImg = imagecreatefromjpeg($rawImgPath);
$newImg = imagecreatetruecolor($newImgSize['w'], $newImgSize['h']);

// need to know the current width and height of the source image
list($rawImgSize['w'], $rawImgSize['h']) = getimagesize($rawImgPath);

// resize the iamge
imagecopyresampled($newImg,$rawImg, 0,0,0,0,
    $newImgSize['w'],$newImgSize['h'],$rawImgSize['w'],$rawImgSize['h']);

// no longer need the original
imagedestroy($rawImg);

// display scaled image
header('Content-Type: image/png');
imagepng($newImg);

// no longer need the scaled image
imagedestroy($newImg);

此代码应该可以工作并提供更好的结果,但不适合我。基本上,imagescale()是新代码,在PHP站点上是not well document

// image to be scaled
$rawImgPath = './test.jpg';

// new image size (guessing you know the new size)
$newImgSize['w'] = 200;
$newImgSize['h'] = 200;

// the steps to create the new scaled image
$rawImg = imagecreatefromjpeg($rawImgPath);
$newImg = imagescale($rawImg, $newImgSize['w'], $newImgSize['h'],
    IMG_BICUBIC_FIXED);

// no longer need the original
imagedestroy($rawImg);

// display scaled image
header('Content-Type: image/png');
imagepng($newImg);

// no longer need the scaled image
imagedestroy($newImg);

答案 1 :(得分:-1)

只需使用PHP的原生clone keyword复制Imagick个实例。

$smallImg = new Imagick('small.png')
$newImg = clone $smallImg;

$newImg->resizeImage(200, 200, Imagick::FILTER_LANCZOS, 1, true);

$newImg->setImageFormat("png");
header("Content-Type: image/png");
echo $newImg;

扩展需要bestfit的{​​{1}}参数(最后一个布尔值)。没有它,Imagick将只缩小,留下较小的图像。

Imagick::resizeImage docs的评论会比较各种调整大小过滤器的速度。我和Lanczos一起取得了不错的成绩。