Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8558151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:44:39+00:00 2026-06-11T15:44:39+00:00

My end goal here is to resize the input image to 100px width, 125px

  • 0

My end goal here is to resize the input image to 100px width, 125px height. Some of the input images are a different Aspect Ratio, so I wish for them to be in a 100×125 container with the background sparse filled from their edge color.

Ok, so this works for the basic resize:

$image = new Imagick($imgFile);
$image->resizeImage(100,0, Imagick::FILTER_LANCZOS, 1, false);
$image->writeImage("$Dir/$game.png");
header("Content-type: ".$image->getImageFormat());
echo $image;
$image->clear();
$image->destroy();

However I’ve been searching for hours, and I cannot find a simple “This is how you center an image in a canvas” bit for PHP’s Imagick library. Everything is for the actual ImageMagick convert application, which is not really what I’m after. I’ve tried compositing the resized image into an empty newImage with the set width and height, but it just seems to overwrite the dimensions regardless of the composite type, setting the Gravity to center and then the extent to 100×125 has no effect ( It always sits at 0,0, and trying to set the y offset to ((125-imageheight)/2) resulted in an offset that was way more than it should have been )

Edit:

    $imageOutput = new Imagick();
    $image = new Imagick($imgFile);
    $image->resizeImage(100,0, Imagick::FILTER_LANCZOS, 1, false);
    $imageOutput->newImage(100, 125, new ImagickPixel('black'));
    $imageOutput->compositeImage($image, Imagick::COMPOSITE_ADD, 0, ((125 - $image->getImageHeight()))/2 );
    $imageOutput->setImageFormat('png');
    $imageOutput->writeImage("$Dir/$game.png");
    header("Content-type: ".$imageOutput->getImageFormat());
    echo $imageOutput;
    $image->clear();
    $image->destroy();

So I got my centering working, gravity apparently has no effect on actual images.

I have absolutely no idea where I would even begin to try and recreate a command line edge-in sparse fill in PHP with the library.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:44:40+00:00Added an answer on June 11, 2026 at 3:44 pm

    I ended up using a combination of Imagick and shell calls to convert itself, I’ll eventually rewrite it to use entirely shell calls. I also changed my dimensions, here’s the code:

        $imageOutput = new Imagick(); // This will hold the resized image
        $image = new Imagick($imgFile); // Open image file
        $image->resizeImage(120,0, Imagick::FILTER_LANCZOS, 1, false); // Resize it width-wise
        $imageOutput->newImage(120, 150, "none"); // Make the container with transparency
        $imageOutput->compositeImage($image, Imagick::COMPOSITE_ADD, 0, ((150 - $image->getImageHeight())/2) ); // Center the resized image inside of the container
        $imageOutput->setImageFormat('png'); // Set the format to maintain transparency
        $imageOutput->writeImage("$Dir/$game.temp.png"); // Write it to disk
        $image->clear(); //cleanup -v
        $image->destroy(); 
        $imageOutput->clear();
        $imageOutput->destroy();
        //Now the real fun
        $edge = shell_exec("convert $Dir/$game.temp.png -channel A -morphology EdgeIn Diamond $Dir/$game.temp.edge.png"); // Get the edges of the box, create an image from just that
        $shepards = shell_exec("convert $Dir/$game.temp.edge.png txt:- | sed '1d; / 0) /d; s/:.* /,/;'"); // get the pixel coordinates
        $final = shell_exec("convert $Dir/$game.temp.edge.png -alpha off -sparse-color shepards '$shepards' png:- | convert png:- $Dir/$game.temp.png -quality 90 -composite $Dir/$game.jpg"); // Sparse fill the entire container using the edge of the other image as shepards , then composite that on top of this new image
        unlink("$Dir/$game.temp.png"); // cleanup temp files
        unlink("$Dir/$game.temp.edge.png");
        set_header_and_serve("$Dir/$game.jpg"); // serve the newly created file
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The end goal is to have some form of a data structure that stores
having some issues with a networking assignment. End goal is to have a C
Note: I've only been using Objective-C for a week. Here's my end goal: I
Couple questions here: My end goal is to password protect the file logged_in.php. Note:
I actually have a few questions here: My end goal is to write a
here is the end goal: get a new connection from a factory by name
The end goal of this part of my project is to be able to
My end goal is local development of a Radiant CMS installation. So, need rails
This is for the Apple platform. My end goal is to do a find
First off, let me define the end goal: I'd like to Wordpress (version 2.8)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.