phMagick :: Resizing Images
By default phMagick will resize images if they are bigger than the specified measures because enlarging images always produces bad results (heavily pixelized images).
Proportional width & height resize
If you just specify one measure the image will be resized proportionally to the desired size
To 200px width :
//resing to fit a particuar width $phMagick = new phMagick('lipe.jpg', 'resize_porp_w.png'); $phMagick->resize(200); // the same as resize(200,0)
| Original Image | Transformed Image |
![]() |
![]() |
To 100px height :
// and to fit a particular height $phMagick = new phMagick('lipe.jpg'); $phMagick->setDestination('resize_porp_h.png') ->resize(0,100); // set width to 0 so that phMagick knows that the resize should // be done proportional to the height of the image // We can set the destination file name in the class instantiation or by // using the setDestination() function
| Original Image | Transformed Image |
![]() |
![]() |
Resizing to fit in a box
If you specify width and height the image will be resized to a size that bests fits the measures, it may not be the exact measures since aspect ratio will be maintained. If you set ExactSize = true then no attempt to maintain aspect ratio will be done and the image will have exactly the measures specified
Mantaining aspect ration
//fitting a 150x 150px box $phMagick = new phMagick('lipe.jpg'); $phMagick->setDestination('resize_porp_wh.png') ->resize(150,100);
| Original Image | Transformed Image |
![]() |
![]() |
Exact dimensions
// making it exactly 80 x 150px $phMagick = new phMagick('lipe.jpg'); $phMagick->setDestination('resize_dumb.png') ->resize(80,150,true);
| Original Image | Transformed Image |
![]() |
|
As you can see resizing to an exact size may produce streched images
Resize an image to an exact size mantaining aspec ratio
It's possible to resize an image to an exact size while mantaining the aspect ratio, internaly this is done by resising and croping the new image
Take note that this aproach is not 100% accurate, there are some extreme situations where the resulting image will not have the desired size
$p = new phmagick('source.png', 'destination.png'); $p->resizeExactly(133,100);
| Original Image | Transformed Image |
![]() |
![]() |





Olá Franco,
Parabéns pela classe phMagick, ela é realmente fantástica.
Eu estava procurando alguma coisa na internet pra servir de base pra eu montar uma classe própria, mas achei a sua tão completa que acho que vou usar ela própria.
Eu só estou com um problema pra redimensionar imagens GIF com animação, elas ficam zoadas.
Você sabe como resolver isso ou a sua classe ainda não dá suporte para animações?
Pois preciso exatamente disso, inclusive é o motivo de eu não utilizar a classe Imagick que é nativa do PHP.
Eu tentei da seguinte forma:
require(dirname(__FILE__) . '/phmagick.php');
$img = new phMagick('animacao.gif');
$img->setDestination('saida_animacao.gif')->resize(250, 250);
Abraços!
Olá Leonardo,
Pois o ImageMagick não é grande coisa a redimensionar gifs animados, pelo que li é preciso fazer primeiro um coalesce à imagem
e depois então fazer o resize
um abraço
Hello Nuno. Firstly the phmagick package is excellent and very useful (and given me some good ideas on using plugin classes). Thankyou!
I had a problem with the polaroid and reflection methods when altering an image already altered using the crop class. The polaroid image shifted partly off the canvas so it was only partly visible, the reflected image was only a portion of the width of the original.
Long story short, after some tinkering I replaced the following
$cmd .= ‘ ‘ . $p->getDestination() ;
with
$cmd .= ‘ +repage ‘ . $p->getDestination() ;
in phMagick_crop::crop wich seemd to work like a charm.
I thought you may find it useful (although I haven’t fully tested that it doesn’t break anything else, my fixes usually do!)
Ciao. Se per te non è un problema crea una reindirizzo poichè in Internet tutti i link fanno riferimento a:
http://www.francodacosta.com/phmagick
Ho penato non poco… anche alcuni link di questo blog sn errati!!
PS… Grande Lavoro!!!
hi,
Is it possible to use cache directory.Instead of resizing everytime.converting takes some cpu resources..
@swiss
the onTheFly() function does that, only resizes an image if it was not resized before
Is it possible to use onTheFly with resizeExactly??
I am having a few trouble and exactDimentions stretches the image when set to True, I have tried using the, using the resizeExactly function instead of the resize but this does not seem to create the desired image?
Has anyone else been able to achieve this?
Thanks
I don’t think it’s possible
onthefly works with urls and resizeexacly with file paths
although it is really easy to implement the onthefly functionality with resize exacly as it checks if the file destination exists, if exists then use that file and do not create a new one
the exactDimentions is something different, it will resize the image to that exact dimensions ignoring aspect ratio
hope this helps you, if nt just mail me and I will try to help you
Thank you Nuno,
That clears things up
I decided to check the destination first and then use the resizeExactly function if the image did not exist. This solves my problem, as I did not want the image to be created every time the page was loaded.
Dear Nuno,
thank you very much for this superb PHP class! It works like a charm! But I do have a question: In all your examples there is always a source and a destination-file.
Is it also possible for phmagick to to resize an image live, so that the picture is just simply stored in the memory while the user is watching the file?
The thing is i’d like to resize my original images “live” while the user watches them. But the original file should be left untouched and i also don’t need any thumbs. I remember that there was a function for that in GD Lib of PHP. Afterwards this this picture was deleted with a function called “imagedestroy();”.
Is that also possible with phmagick?
Thanks in advance!
Regards Christian
Unfortunately no
phMagick runs ImageMagick from the command line, so it needs a source and destination files (they can be the same)
you can implement something similar using a temporary file name and overwriting it on each change
I will see if I can implement a preview mode in phMagick, but I’m very busy now