Why doesn't this work?
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;
It returns the same resolution.
This is supposed to resize an SVG image without losing quality.
I have no idea what $Obj_old_image
is or what it is doing based on your posted code. However the following code will work with PHP and the standard GD lib installed:
// 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);
And this code should work and give better results, but does not for me. Basically, imagescale()
is newish code and is not well document on the PHP site.
// 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);
Just use PHP's native clone
keyword to duplicate Imagick
instances.
$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;
The bestfit
argument to resizeImage
(last boolean) is required to scale up. Without that, Imagick will only scale down, leaving smaller images unchanged.
A comment on the Imagick::resizeImage
docs compares speeds for the various resize filters. I've had good results with Lanczos.