PHP Resize based on overlaying image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
pbennett87
Posts: 5
Joined: 2019-09-02T06:58:09-07:00
Authentication code: 1152

PHP Resize based on overlaying image

Post by pbennett87 »

I am having to use system commands in PHP, how would I resize the top layer to fit both height and width (even if the top layer is cropped slightly) it also needs to keep aspect ratio.

system('convert background.png overlay.php \
\( -clone 0 -alpha extract \) \
\( -clone 0 -clone 1 -compose multiply -composite \) \
-delete 0,1 +swap -alpha off -compose copy_opacity -composite overlay_result.png');

I am really new to imagemagick so any help would be grateful, kindest regards.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PHP Resize based on overlaying image

Post by snibgo »

When resizing one image to the size of another, if they have different aspect ratios then you have a choice of either resizing exactly or keeping the original aspect ratio. You can't do both.

Please show sample input images. ".php" is not a format known to IM.
snibgo's IM pages: im.snibgo.com
pbennett87
Posts: 5
Joined: 2019-09-02T06:58:09-07:00
Authentication code: 1152

Re: PHP Resize based on overlaying image

Post by pbennett87 »

Hi snibgo, sorry the format is actually PNG that is a typo. Please can you show me an example for both (exact resize, and keeping aspect ratio)? Both would be useful, I thought we could possibly resize but keeping the aspect ratio > then crop ? then overlay?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PHP Resize based on overlaying image

Post by snibgo »

For exact resizing, and other options, see http://www.imagemagick.org/script/comma ... p#geometry

Yes, after resizing, one image may be cropped. Or the other could be extended. See options on http://www.imagemagick.org/script/comma ... ptions.php
snibgo's IM pages: im.snibgo.com
pbennett87
Posts: 5
Joined: 2019-09-02T06:58:09-07:00
Authentication code: 1152

Re: PHP Resize based on overlaying image

Post by pbennett87 »

So if my small image is 500x800px and my large image is 2000x2000 I should probably use
500x800^ ?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PHP Resize based on overlaying image

Post by snibgo »

That depends on what you want. I encourage you to try it out:

Code: Select all

magick -size 2000x2000 xc: -resize 500x800^ info:

xc: XC 800x800 800x800+0+0 16-bit sRGB 1.844u 0:00.250
The result is 800x800 pixels.
snibgo's IM pages: im.snibgo.com
Post Reply