开发者

Image manipulation using PHP: force 1:1 aspect ratio (sqaure images)

开发者 https://www.devze.com 2023-04-12 11:22 出处:网络
I am looking to add a background to images that users upload that are not square. So if they upload a tall and skinny photo I want to add a white background to th开发者_JAVA百科e sides of the image to

I am looking to add a background to images that users upload that are not square. So if they upload a tall and skinny photo I want to add a white background to th开发者_JAVA百科e sides of the image to make the resulting image have an aspect ratio of 1:1. Is this possible using PHP or javascript?


You can use the GD library for what, with a library called Wideimage it's a breeze:

$image = WideImage::load('img_form_field_name');
$size = max($image->getHeight(), $image->getWidth());
$white = $image->allocateColor(255, 255, 255);
$image->resizeCanvas($size, $size, 'center', 'center', $white);

See the documentation and examples, many functions can even be tested interactively.


The GD library is the most commonly used image manipulation package. It's a set of functions often installed with PHP which handle image manipulation.

What you'll want to do is either scale and crop your image to a specific aspect ratio so that you place your image on a square canvas and cut off whatever does fit or

You'll want to simply resize your image to a fixed aspect ratio and place it on a square canvas with whitespace around it.

Either way, this tutorial should point you in the right direction

http://return-true.com/2009/02/making-cropping-thumbnails-square-using-php-gd/


Yep you'll want to look into either the GD library or ImageMagik. There are plenty of tutorials available for this task.

Functions like imagecreatetruecolor() etc will allow you to create a new image, and then stack the uploaded image on top of it and save it as a new file.


Yes.

http://www.php.net/manual/en/refs.utilspec.image.php

0

精彩评论

暂无评论...
验证码 换一张
取 消