1

为什么这不起作用?

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 图像的大小。

4

2 回答 2

0

$Obj_old_image根据您发布的代码,我不知道它在做什么或在做什么。然而,以下代码将适用于 PHP 和安装的标准 GD 库:

// 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 站点上不是很好的文档。

// 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);
于 2013-06-04T06:45:59.340 回答
0

只需使用 PHP 的 nativeclone关键字来复制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;

(last boolean)的bestfit参数需要按比例放大。resizeImage没有它,Imagick 只会缩小,保持较小的图像不变。

Imagick::resizeImage文档的评论比较了各种调整大小过滤器的速度。我在 Lanczos 上取得了不错的成绩。

于 2016-06-08T15:05:21.633 回答