I'm using Imagick to create jpeg from 2 png images - one background and one over it. But when I composite it, $image is smaller than original. I tried this:
$image->scaleimage(
$image->getImageWidth() * 1.46,
$image->getImageHeight() * 1.47
);
and $image is almost exact size. If I don't multiply $image original width and height, they are smaller. What it is due to and how to resolve it?
I use the following code:
$image = new Imagick($_REQUEST['image']);
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
$image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
$image->setBackgroundColor(new ImagickPixel('transparent'));
$image->scaleimage(
$image->getImageWidth(),
$image->getImageHeight()
);
$background = new Imagick($_REQUEST['background']);
$background->compositeImage($image, Imagick::COMPOSITE_ADD,0, 0);
$date = date('YMd'); $name = time().$rand;
$background->writeImage(DOCROOT.'assets/uploads/'.$date.'/'.$name.'.jpg');
$image->destroy();
$background->destroy();
When I var_dump($image->getImageWidth())
and var_dump($image->getImageHeight())
, exact width and height are shown.
Edited: This is my original code. Problem is that when 2 images are merged into one image, $image has smaller width and height than its original sizes.
Width $image->scaleimage
that I used above, I tried to fix that. I multiplied its width and height width some procent, so that I could achieve original sizes of $image.
But my question is why without scaling $image, it's not preserved its original width and height, but they are smaller after sompositeImages?
$image = new Imagick($_REQUEST['image']);
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
$image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
$image->setBackgroundColor(new ImagickPixel('transparent'));
$background = new Imagick($_REQUEST['background']);
$background->compositeImage($image, imagick::COMPOSITE_ADD, 20, 10);
$date = date('YMd');$name = time().rand()&10000;
$background->writeImage(DOCROOT.'assets/uploads/'.$date.'/'.$name.'.jpg');
$image->destroy();
$background->destroy();
When I place image over background like that:
Imagick composites it like that - Dog is smaller.
Image is smaller that its own original sizes.
Your code is almost certainly doing exactly what you told it to do.
The output image is going to have the same size as the $background
image....because that's the image that you're compositing the other image into.
If you want the image that is being composited on top to be the same size as the background image, then scale it to be the size of the background image.
$image->scaleImage(
$background->getImageWidth(),
$background->getImageHeight()
);
Instead of scaling it to be the same size it already is....
Image is smaller that its own original sizes.
The composited image is not smaller. You are compositing a small image into a large image. In your web-browser the composited image is then being scaled down to be displayed because it is too large to display in place. This makes the image that has been composited in appear smaller, but it is not actually smaller.
If you don't want the composited image (including the image that you are compositing into the background) to be scaled down, you ought to make sure it fits into the element on the webpage where you are trying to display.
Alternatively you could try to scale up the image that you are compositing onto the background image .....by however much is appropriate.....however I can't tell you how much it should scale by, that is your own decision as to what the 'correct' size is.