0

I have a pdf that is created using an image (so it has image's original dimensions)

Now, I am trying to scale it in its ratio to fit A4 pdf (width touching to sides and centering vertically).

This is the code I tried:

    $imagick = new \Imagick();
    $imagick->setResolution(576,576);
    $imagick->readImage($path);
    $imagick->resizeImage(2480,3508,\Imagick::FILTER_CUBIC,1);
    $imagick->setCompressionQuality(80);
    $imagick->setImageFormat('pdf');
    $imagick->writeImage($path);

But it's disturbing the image ratio to fit full page (100% width & 100% height).

For example, original image 717x558: enter image description here

Exported image,

enter image description here

However, what I am trying to achieve is this:

enter image description here

enter image description here


** Update **

$imagick = new \Imagick();
$imagick->readImage($path);
$imagick->resizeImage(595,842,\Imagick::FILTER_CUBIC,1);
$imagick->setBackgroundColor(new ImagickPixel('white')); 
$imagick->setGravity ( Imagick::GRAVITY_CENTER );
$imagick->extentImage ( 595 , 843 , 0 , 0 );
$imagick->setImageFormat('pdf');
$imagick->writeImage($path);

Using this, this is the output (it's full page but lost the ratio):

enter image description here

senty
  • 12,385
  • 28
  • 130
  • 260

2 Answers2

0

You only provided a PNG image, not a PDF. But this should help using Imagemagick command line. Sorry, I do not know Imagick, but you should be able figure out the equivalent commands. Note that A4 size is 595x842 pixels.

enter image description here

convert source.png -background white -resize 595x842 -gravity center -extent 595x842 -bordercolor red -border 1 result.png

Note you can remove the -bordercolor red -border 1, since I added a red border to show the size of the output against a white background.

enter image description here

If using a PDF input, you can try the same or if that does not give good quality, you can try increasing the density, such as

convert -density 300 source.pdf -background white -resize 595x842 -gravity center -extent 595x842 result.png
fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Yes, sorry that I couldn't upload a pdf file on here, so I uploaded a png. But you got the idea perfectly. I'd appreciate anyone that can convert this to Imagick/php. Thanks for your input though, it absolutely looks like what I wanted – senty Jan 23 '18 at 22:39
  • Note that if you are outputting to PDF, then Imagemagick will rasterize the PDF. But you say your PDF is really a raster image in a vector PDF shell. So that should be OK. You may want to use pdfimages to extract the raster image from the PDF before resizing it. – fmw42 Jan 23 '18 at 22:41
  • https://drive.google.com/open?id=1F5h-etf4E4l47CoyrDUvOAecLyfeqBWA This is the pdf url on my Drive if you want to give it a try and include in the answer. I'd highly appreciate it! By raster, do you mean like flatten? If you can give it a try and inform me, maybe I can achieve it with Imagick much easily (thanks again). – senty Jan 23 '18 at 22:43
0

This seems to work fine for me in Imagemagick.

convert birds.pdf -background white -resize 595x842 -gravity center -extent 595x842 result.pdf


You can try the following in Imagick, though I am not an expert on it.

$imagick = new \Imagick();
$imagick->readImage($path);
$imagick->resizeImage(595,842,\Imagick::FILTER_CUBIC,1);
$imagick->setBackgroundColor(new ImagickPixel('white')); 
$imagick->setGravity ( Imagick::GRAVITY_CENTER );
$imagick->extentImage ( 595 , 843 , 0 , 0 );
$imagick->setImageFormat('pdf');
$imagick->writeImage($path);
fmw42
  • 46,825
  • 10
  • 62
  • 80
  • I did, it's full-scale A4, but lost the ratio. :/ You can see my OP (updated part at the bottom) – senty Jan 23 '18 at 23:12
  • I think this part is `$imagick->resizeImage(595,842,\Imagick::FILTER_CUBIC,1);` making the image 595x842 without ratio – senty Jan 23 '18 at 23:22
  • `@sentry`. Sorry, I do not understand your last comment. – fmw42 Jan 24 '18 at 00:03
  • let me put it this way: I tried your code, and it's keeping the ratio, however this doesn't let it fit in the page with ratio. Instead, it's making the smaller side fit, and extends the other side even more (so outside of the pdf). Did the php code work for you? – senty Jan 26 '18 at 23:49
  • `@sentry` Please post an example of your result. Is it from my command line or from the Imagick code. If the latter, as I mentioned, I do not know Imagick well. So there could be a mistake. If from Imagemagick command line, then if what I gave you earlier does not work, then please show how it is wrong and what you want. – fmw42 Jan 27 '18 at 00:38
  • it's the php code which I have problem. My php output is [like this one](https://i.stack.imgur.com/cb3gl.png) but I wanted to achieve like the one you've showed in your [other post](https://i.stack.imgur.com/NpV9U.png) – senty Jan 27 '18 at 03:09
  • In my comment post, I believe it's because of this line `$imagick->resizeImage(595,842,\Imagick::FILTER_CUBIC,1);` – senty Jan 27 '18 at 03:24
  • Read the docs at http://us3.php.net/manual/en/imagick.resizeimage.php. Perhaps you need to use Bestfit=True, i.,e `>resizeImage(595,842,\Imagick::FILTER_CUBIC,1, true);` – fmw42 Jan 27 '18 at 03:31
  • Ah yeah. That worked for making the ratio! Now, I have the problem with center-ing. The image is attached to the top rather than centering. I used `$imagick->setGravity(\Imagick::GRAVITY_CENTER ); $imagick->extentImage ( 595 , 843 , 0 , 0 );`. Do you have any suggestions for that too? – senty Jan 27 '18 at 03:48
  • Not really. That should have worked. But I do not know Imagick very well. Try your same code but output to PNG. Does that work? – fmw42 Jan 27 '18 at 06:21
  • What is your version of Imagick, PHP and ImageMagick. Perhaps you have an older version that might be buggy and need to upgrade? – fmw42 Jan 27 '18 at 06:28
  • I have found [this answer](https://stackoverflow.com/questions/11921433/php-imagick-setgravity-function-doesnt-work-with-compositeimage-function) - I figured out the answer: `$w = $imagick->getImageWidth(); $h = $imagick->getImageHeight(); $imagick->extentImage ( 595 , 843 , ($w-595)/2, ($h-843)/2 );` and remove `setGravity` line. If you can update your answer, I can accept it :) Thanks a lot again – senty Jan 27 '18 at 16:58
  • *Edit:* With above code, the image is getting blurry. I believe it's the `resizeImage()`'s blur parameter. I tried setting it below 1 but it's not affecting much. Do you have any suggestions for that? – senty Jan 27 '18 at 17:09
  • Try other filters such as Lanczos or Catrom. Catrom produces sharper results at the expense of other artifacts. I do not know much about the blur factor in resizeImage. So try 0, .2, .5, .7, .9 to see what affect they have. If your input image is small and your are expanding it a lot, then you will get blurring. If none of those work, then try adding unsharp masking. See http://us3.php.net/manual/en/imagick.unsharpmaskimage.php – fmw42 Jan 27 '18 at 19:37