如何使用 CodeIgniter 填充图像?

发布于 2024-08-24 16:25:23 字数 109 浏览 2 评论 0原文

我的用户正在上传任意尺寸 XX x YY 的图像。我想找到更大的尺寸,并将图像缩小到 250x250 的正方形,并添加透明填充来弥补差异。有什么办法可以用 CI 的 Image Lib 来完成这个任务吗?

My user is uploading an image of any size XX x YY. I want to find the larger dimension and shrink the image to a square of 250x250 with transparent padding being added to make up the difference. Is there any way to accomplish this with CI's Image Lib?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平定天下 2024-08-31 16:25:23

试试这个:

$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['maintain_ratio'] = TRUE;
$config['width'] = 250;
$config['height'] = 250;

$this->load->library('image_lib', $config);
$this->image_lib->resize();

$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['maintain_ratio'] = FALSE;
$config['width'] = 250;
$config['height'] = 250;

$this->image_lib->initialize($config);
$this->image_lib->crop();

详细信息,请参阅 CodeIgniter API 中的 图像操作类

有关 老实说,我觉得 CI 的图像处理库不够用。它不是很灵活,对于重复或组合操作(如上面的操作)来说不是很快,并且它使用了一种糟糕的语法(这对于某些类型的应用程序可能会派上用场,但我个人更喜欢像 < a href="http://phpthumb.gxdlabs.com/" rel="noreferrer">PHP Thumb):


事实上,我最近为 PHP Thumb 编写了一个简单的包装库(下载)与 CodeIgniter 一起使用,效果非常好。使用 PHP Thumb 库而不是 CodeIgniter 自己的库,您可以像这样完成工作:

$this->load->library('phpthumb_lib');
$thumb = phpthumb_lib::create('/path/to/image/mypic.jpg');
$thumb->adaptiveResize(250, 250);
$thumb->save();

有关 PHP Thumb 的 GitHub 页面

只是这不会用透明像素填充图像(我认为这是一种不好的做法),它只是调整图像大小并裁剪图像以适合整个 250x250 像素区域,而不拉伸图像。

或者,您可以使其调整图像大小以适合 250x250 像素框(通过使用 resize 而不是 adaptiveResize),然后将250x250px DIV 容器内的图像。我猜这可能是您想要的解决方案。

Try this:

$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['maintain_ratio'] = TRUE;
$config['width'] = 250;
$config['height'] = 250;

$this->load->library('image_lib', $config);
$this->image_lib->resize();

$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['maintain_ratio'] = FALSE;
$config['width'] = 250;
$config['height'] = 250;

$this->image_lib->initialize($config);
$this->image_lib->crop();

For details, see the Image Manipulation Class from the CodeIgniter API

Although to be quite honest, I don't find CI's image manipulation library to be adequate. It isn't very flexible, it isn't very fast for repeated or combined actions (like the one above), and it uses an atrocious syntax (that may come in handy for certain types of applications, but I personally prefer something like PHP Thumb):


In fact, I recently put together a simple wrapper library for PHP Thumb (download) to use with CodeIgniter, and it works fabulously. Using the PHP Thumb library instead of CodeIgniter's own, you could get the job done like so:

$this->load->library('phpthumb_lib');
$thumb = phpthumb_lib::create('/path/to/image/mypic.jpg');
$thumb->adaptiveResize(250, 250);
$thumb->save();

More details on PHP Thumb's GitHub page.

Only this doesn't pad the image with transparent pixels (which I would consider a bad practice), it merely resizes and crops the image to fit the whole 250x250 pixel area without stretching the image.

Alternatively, you could make it resize the image to fit inside a 250x250 pixel box (by using resize rather than adaptiveResize), and then place the images inside 250x250px DIV containers. I'm guessing this is probably the solution you'll want.

猫卆 2024-08-31 16:25:23

我不裁剪图像,而是将其放置在 div 的背景中。这是我的代码。

1)将其放置在助手内部。

function getSmallerDimension($img_path, $width, $height) {
    list($wid, $hei, $type, $attr)= getimagesize($img_path);
    $ratio = $wid / $hei;

    if( $height < $width/$ratio ) {
        return 'width';
    }
    else if( $width < $height * $ratio ) {
        return 'height';
    }
    else {
        return 'auto';
    }
}

2)使用它来调整大小。

$config['image_library'] = 'gd2';
$config['source_image'] = 'source-path/source.jpg';
$config['create_thumb'] = true;
$config['thumb_marker'] = '-thumb';
$config['maintain_ratio'] = true;
$config['width'] = 250;
$config['height'] = 250;
$config['master_dim'] = getSmallerDimension('source-path/source.jpg', 250, 250);
$this->load->library('image_lib', $config);
$this->image_lib->resize();

I do not crop the image, rather place it in the background of a div. Here is my code.

1) Inside the helper place this.

function getSmallerDimension($img_path, $width, $height) {
    list($wid, $hei, $type, $attr)= getimagesize($img_path);
    $ratio = $wid / $hei;

    if( $height < $width/$ratio ) {
        return 'width';
    }
    else if( $width < $height * $ratio ) {
        return 'height';
    }
    else {
        return 'auto';
    }
}

2) Use this to resize.

$config['image_library'] = 'gd2';
$config['source_image'] = 'source-path/source.jpg';
$config['create_thumb'] = true;
$config['thumb_marker'] = '-thumb';
$config['maintain_ratio'] = true;
$config['width'] = 250;
$config['height'] = 250;
$config['master_dim'] = getSmallerDimension('source-path/source.jpg', 250, 250);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文