缩略图生成/图像调整大小

发布于 2024-11-10 02:58:58 字数 151 浏览 0 评论 0 原文

您推荐使用什么 PHP 库来生成缩略图? 我需要它与 GD(不仅是 Imagick)一起使用,能够在仅给定一维时拉伸图像或保持纵横比,并且 - 最重要的是 - 能够裁剪和调整图像大小(生成缩略图时,我需要所有这些)比方说 128x128,所以我希望库在这种情况下裁剪图像) 您有什么建议?

What PHP library would you recommend for thumbnails generation?
I need it to work with GD (not only Imagick), be able to stretch images or keep aspect ratio when only one dimension given, and - most important - be able to crop&resize image (when generating thumbnails, I need all of them to be, let's say 128x128, so I want the library to crop images in that case)
What are yours recommendations?

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

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

发布评论

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

评论(3

趁年轻赶紧闹 2024-11-17 02:58:58

你可以试试这个: https://gist.github.com/342704/

示例用法:

include("classes/Resize.php");
$resizer = new Resize('images/cars/large/input.jpg');
$resizer->resizeImage(150, 100, 0);
$resizer->saveImage('images/cars/large/output.jpg', 100);

you can try this: https://gist.github.com/342704/

Example usage:

include("classes/Resize.php");
$resizer = new Resize('images/cars/large/input.jpg');
$resizer->resizeImage(150, 100, 0);
$resizer->saveImage('images/cars/large/output.jpg', 100);
孤凫 2024-11-17 02:58:58

我为我的框架编写了自己的 GD 包装器phunction。一些例子:

$input = '/path/to/source/image.jpg';
$output = '/path/to/destination/image.jpg';

/*
this will crop the biggest possible square (since 128/128 = 1)
from the center of the image and resize the width to 500px
while keeping the height porportional (to maintian aspect ratio)
*/
ph()->Disk->Image($input, '128/128', '500*0', null, $output);

/*
same as the above, but aspect ratio is not respected
since both width and height are specified
*/
ph()->Disk->Image($input, '128/128', '500*1000', null, $output);

/*
no cropping, just porpotional resize to the width
*/
ph()->Disk->Image($input, null, '500*0', null, $output);

还有其他不错的选择,例如 AsidoWideImage 但这对我有用,因为它是一种简单的方法,只有一个依赖项,您可以轻松地独立使用它:

function Image($input, $crop = null, $scale = null, $merge = null, $output = null, $sharp = true)
{
    if (isset($input, $output) === true)
    {
        if (is_string($input) === true)
        {
            $input = @ImageCreateFromString(@file_get_contents($input));
        }

        if (is_resource($input) === true)
        {
            $size = array(ImageSX($input), ImageSY($input));
            $crop = array_values(array_filter(explode('/', $crop), 'is_numeric'));
            $scale = array_values(array_filter(explode('*', $scale), 'is_numeric'));

            if (count($crop) == 2)
            {
                $crop = array($size[0] / $size[1], $crop[0] / $crop[1]);

                if ($crop[0] > $crop[1])
                {
                    $size[0] = round($size[1] * $crop[1]);
                }

                else if ($crop[0] < $crop[1])
                {
                    $size[1] = round($size[0] / $crop[1]);
                }

                $crop = array(ImageSX($input) - $size[0], ImageSY($input) - $size[1]);
            }

            else
            {
                $crop = array(0, 0);
            }

            if (count($scale) >= 1)
            {
                if (empty($scale[0]) === true)
                {
                    $scale[0] = round($scale[1] * $size[0] / $size[1]);
                }

                else if (empty($scale[1]) === true)
                {
                    $scale[1] = round($scale[0] * $size[1] / $size[0]);
                }
            }

            else
            {
                $scale = array($size[0], $size[1]);
            }

            $image = ImageCreateTrueColor($scale[0], $scale[1]);

            if (is_resource($image) === true)
            {
                ImageFill($image, 0, 0, IMG_COLOR_TRANSPARENT);
                ImageSaveAlpha($image, true);
                ImageAlphaBlending($image, true);

                if (ImageCopyResampled($image, $input, 0, 0, round($crop[0] / 2), round($crop[1] / 2), $scale[0], $scale[1], $size[0], $size[1]) === true)
                {
                    $result = false;

                    if ((empty($sharp) !== true) && (is_array($matrix = array_fill(0, 9, -1)) === true))
                    {
                        array_splice($matrix, 4, 1, (is_int($sharp) === true) ? $sharp : 16);

                        if (function_exists('ImageConvolution') === true)
                        {
                            ImageConvolution($image, array_chunk($matrix, 3), array_sum($matrix), 0);
                        }
                    }

                    if ((isset($merge) === true) && (is_resource($merge = @ImageCreateFromString(@file_get_contents($merge))) === true))
                    {
                        ImageCopy($image, $merge, round(0.95 * $scale[0] - ImageSX($merge)), round(0.95 * $scale[1] - ImageSY($merge)), 0, 0, ImageSX($merge), ImageSY($merge));
                    }

                    foreach (array('gif' => 0, 'png' => 9, 'jpe?g' => 90) as $key => $value)
                    {
                        if (preg_match('~' . $key . '$~i', $output) > 0)
                        {
                            $type = str_replace('?', '', $key);
                            $output = preg_replace('~^[.]?' . $key . '$~i', '', $output);

                            if (empty($output) === true)
                            {
                                header('Content-Type: image/' . $type);
                            }

                            $result = call_user_func_array('Image' . $type, array($image, $output, $value));
                        }
                    }

                    return $result;
                }
            }
        }
    }

    else if (count($result = @GetImageSize($input)) >= 2)
    {
        return array_map('intval', array_slice($result, 0, 2));
    }

    return false;
}

水印和转换/显示(而不是保存)图像也是支持。

I coded my own GD wrapper for my framework, phunction. Some examples:

$input = '/path/to/source/image.jpg';
$output = '/path/to/destination/image.jpg';

/*
this will crop the biggest possible square (since 128/128 = 1)
from the center of the image and resize the width to 500px
while keeping the height porportional (to maintian aspect ratio)
*/
ph()->Disk->Image($input, '128/128', '500*0', null, $output);

/*
same as the above, but aspect ratio is not respected
since both width and height are specified
*/
ph()->Disk->Image($input, '128/128', '500*1000', null, $output);

/*
no cropping, just porpotional resize to the width
*/
ph()->Disk->Image($input, null, '500*0', null, $output);

There are other good alternatives like Asido and WideImage but this one works for me, and since it is a simple method with only one dependency you can easily use it standalone:

function Image($input, $crop = null, $scale = null, $merge = null, $output = null, $sharp = true)
{
    if (isset($input, $output) === true)
    {
        if (is_string($input) === true)
        {
            $input = @ImageCreateFromString(@file_get_contents($input));
        }

        if (is_resource($input) === true)
        {
            $size = array(ImageSX($input), ImageSY($input));
            $crop = array_values(array_filter(explode('/', $crop), 'is_numeric'));
            $scale = array_values(array_filter(explode('*', $scale), 'is_numeric'));

            if (count($crop) == 2)
            {
                $crop = array($size[0] / $size[1], $crop[0] / $crop[1]);

                if ($crop[0] > $crop[1])
                {
                    $size[0] = round($size[1] * $crop[1]);
                }

                else if ($crop[0] < $crop[1])
                {
                    $size[1] = round($size[0] / $crop[1]);
                }

                $crop = array(ImageSX($input) - $size[0], ImageSY($input) - $size[1]);
            }

            else
            {
                $crop = array(0, 0);
            }

            if (count($scale) >= 1)
            {
                if (empty($scale[0]) === true)
                {
                    $scale[0] = round($scale[1] * $size[0] / $size[1]);
                }

                else if (empty($scale[1]) === true)
                {
                    $scale[1] = round($scale[0] * $size[1] / $size[0]);
                }
            }

            else
            {
                $scale = array($size[0], $size[1]);
            }

            $image = ImageCreateTrueColor($scale[0], $scale[1]);

            if (is_resource($image) === true)
            {
                ImageFill($image, 0, 0, IMG_COLOR_TRANSPARENT);
                ImageSaveAlpha($image, true);
                ImageAlphaBlending($image, true);

                if (ImageCopyResampled($image, $input, 0, 0, round($crop[0] / 2), round($crop[1] / 2), $scale[0], $scale[1], $size[0], $size[1]) === true)
                {
                    $result = false;

                    if ((empty($sharp) !== true) && (is_array($matrix = array_fill(0, 9, -1)) === true))
                    {
                        array_splice($matrix, 4, 1, (is_int($sharp) === true) ? $sharp : 16);

                        if (function_exists('ImageConvolution') === true)
                        {
                            ImageConvolution($image, array_chunk($matrix, 3), array_sum($matrix), 0);
                        }
                    }

                    if ((isset($merge) === true) && (is_resource($merge = @ImageCreateFromString(@file_get_contents($merge))) === true))
                    {
                        ImageCopy($image, $merge, round(0.95 * $scale[0] - ImageSX($merge)), round(0.95 * $scale[1] - ImageSY($merge)), 0, 0, ImageSX($merge), ImageSY($merge));
                    }

                    foreach (array('gif' => 0, 'png' => 9, 'jpe?g' => 90) as $key => $value)
                    {
                        if (preg_match('~' . $key . '$~i', $output) > 0)
                        {
                            $type = str_replace('?', '', $key);
                            $output = preg_replace('~^[.]?' . $key . '$~i', '', $output);

                            if (empty($output) === true)
                            {
                                header('Content-Type: image/' . $type);
                            }

                            $result = call_user_func_array('Image' . $type, array($image, $output, $value));
                        }
                    }

                    return $result;
                }
            }
        }
    }

    else if (count($result = @GetImageSize($input)) >= 2)
    {
        return array_map('intval', array_slice($result, 0, 2));
    }

    return false;
}

Watermarking and converting / displaying (instead of saving) images is also supported.

独﹏钓一江月 2024-11-17 02:58:58

在提问之前先使用搜索。关于保持宽高比< /a> 和关于裁剪

Jus use search, before ask questions. About keeping aspect ratio and about cropping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文