1

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.

Expedito
  • 7,771
  • 5
  • 30
  • 43
John Smith
  • 681
  • 5
  • 13
  • 30
  • Possible duplicate? http://stackoverflow.com/questions/10383305/how-to-resize-an-svg-with-imagick-imagemagick – Mike Oct 11 '12 at 02:30
  • No its not duplicate, forget SVG i need to load image from one one imagick object to another larger size imagick object. pretty much like you load image from JPEG file into imagick object and it resizes it. – John Smith Oct 11 '12 at 18:31

2 Answers2

0

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);
Tigger
  • 8,980
  • 5
  • 36
  • 40
0

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.

joemaller
  • 19,579
  • 7
  • 67
  • 84