创建图片库

发布于 2024-08-13 15:58:41 字数 504 浏览 4 评论 0 原文

我想为我的网站创建一个图片库,并从网站的管理面板上传这些图片。我将在一个非常小的尺寸的页面中显示所有图片,并且我将为每张图片分开一个地方,以显示大尺寸的所选图片(从小图片中)。

我知道两种方法:

  1. 我创建两张图片以从管理面板上传,一张大一张小,尺寸固定。所以我会把大的放在单独的大空间里。小的地方就会显示小。因此,网站管理员应该创建 2 张图片。

  2. 我知道的第二种方法是使用PHP的GD库来只上传大图片,并且在每个地方都有一个PHP函数将调整大图片的大小并获得我需要的画廊的2个尺寸。

第一种方法的缺点是,网站用户在上传之前应使用 Photoshop 或其他一些工具。我们一致认为,这对于网站用户来说并不是一件令人愉快的事情。

第二种方法也不好,因为 GD 通过降低质量来调整图片大小。这种损失是不可接受的,因为损失太大了。

有什么方法可以在保持接近原始图像质量的同时进行调整大小操作?

或者还有什么比这两种方法更好的方法?

I want to create a gallery of pictures for my website and upload those pictures from the administration panel of the website. I am going to show all pictures in a page with a very small size, and one place I will separate for each picture to show the selected picture (from small ones) with big size.

I know two ways of doing that:

  1. I create 2 pictures for uploading from administration panel, one big and one small with a fixed size. So the big one I will load in the separate big space. And small will be shown in the small places. So the administrator of the site should CREATE 2 pics.

  2. The second way I know is to use the GD library of PHP to be able to upload only the big picture, and in each place a PHP function will resize the big picture and get the 2 sizes that I need for the gallery.

The first method's disadvantage is, that the user of the site should use Photoshop or some other tool before uploading. Let’s agree that this is not a pleasant thing for the site user.

The second approach is not good either, as GD resizes the picture by lowering quality. And that loss is inadmissible because it is too much.

What is a way that does that resize operation while maintaining close to the original picture quality?

Or what is another approach that is better that these two?

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

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

发布评论

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

评论(8

旧人九事 2024-08-20 15:58:41

GD 不会比 Photoshop 更损失质量。只需确保您使用的是 imagecopyresampled() 而不是 imagecopyresized(),因为它不会进行任何类型的重新采样。然后以足够高的质量保存生成的图像;对于 JPEG,约为 80。

然后在 Photoshop 中进行类似的调整大小,您会发现没有太大差异。您还可以使用 GD 来锐化缩略图,这会给它们带来一点额外的清晰度。

编辑

我制作了一个工具来比较不同的调整大小方法,尝试一下并亲自看看什么是最好的方法:http://www.ulmanen.fi/stuff/downsample/

GD doesn't lose quality any more than Photoshop. Just make sure you are using imagecopyresampled() and NOT imagecopyresized() as that does not do any kind of resampling. Then save the resulting image in high enough quality; for JPEG's, that's around 80.

Then do a similar resize in Photoshop and you'll see there's not much difference. You can also use GD to sharpen your thumbnails, that'll give them a little extra crispness.

EDIT

I made a tool to compare different resize methods, try it out and see for yourself what is the best method: http://www.ulmanen.fi/stuff/downsample/

枕花眠 2024-08-20 15:58:41

使用如下函数:

Image('/path/to/original.image', '1/1', '150*', './thumb.jpg'); // thumb, width = 150 px
Image('/path/to/original.image', null, '600*', './full.jpg'); // full, width = 600 px

如果愿意,您可以通过第二个参数 (w/h) 指定裁剪比例,还可以通过第三个参数 (w*h) 指定调整后图像的宽度和/或高度。

function Image($source, $crop = null, $scale = null, $destination = null)
{
    $source = @ImageCreateFromString(@file_get_contents($source));

    if (is_resource($source) === true)
    {
        $size = array(ImageSX($source), ImageSY($source));

        if (isset($crop) === true)
        {
            $crop = array_filter(explode('/', $crop), 'is_numeric');

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

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

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

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

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

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

        if (isset($scale) === true)
        {
            $scale = array_filter(explode('*', $scale), 'is_numeric');

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

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

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

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

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

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

            if (ImageCopyResampled($result, $source, 0, 0, $crop[0] / 2, $crop[1] / 2, $scale[0], $scale[1], $size[0], $size[1]) === true)
            {
                ImageConvolution($result, array(array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
                ImageJPEG($result, $destination, 90);
            }
        }
    }

    return false;
}

图像会自动增强(锐化)并保存为高质量的 JPEG。

尽情享受吧!

Use the following function like this:

Image('/path/to/original.image', '1/1', '150*', './thumb.jpg'); // thumb, width = 150 px
Image('/path/to/original.image', null, '600*', './full.jpg'); // full, width = 600 px

You can specify the crop ratio via the second argument (w/h) if you wish, you can also specify the width and/or height of the resized image via the third argument (w*h).

function Image($source, $crop = null, $scale = null, $destination = null)
{
    $source = @ImageCreateFromString(@file_get_contents($source));

    if (is_resource($source) === true)
    {
        $size = array(ImageSX($source), ImageSY($source));

        if (isset($crop) === true)
        {
            $crop = array_filter(explode('/', $crop), 'is_numeric');

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

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

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

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

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

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

        if (isset($scale) === true)
        {
            $scale = array_filter(explode('*', $scale), 'is_numeric');

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

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

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

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

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

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

            if (ImageCopyResampled($result, $source, 0, 0, $crop[0] / 2, $crop[1] / 2, $scale[0], $scale[1], $size[0], $size[1]) === true)
            {
                ImageConvolution($result, array(array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
                ImageJPEG($result, $destination, 90);
            }
        }
    }

    return false;
}

The image is automatically enhanced (sharpened) and saved as a JPEG with high quality.

Enjoy!

老子叫无熙 2024-08-20 15:58:41

为什么不使用 phpThumb()

  • 您只需要为图像标签生成 src 链接。
  • 自动缓存。
  • 提供了很多操作图像的选项。请参阅演示页面。
  • 使用 ImageMagick 并作为后备 GD,您不必关心(但您可以)。
  • 几乎所有东西都可以配置。请参阅:phpThumb.config.php

Why not use phpThumb()?

  • You only need to generate src-links for your image tags.
  • Automatic cache.
  • Offers a lot of options to manipulate images. See the demo page.
  • Uses ImageMagick and as fallback GD, that you do not have to care about (but you can).
  • Nearly everything can be configured. See: phpThumb.config.php.
乖乖兔^ω^ 2024-08-20 15:58:41

您可以使用 ImageMagick 代替 GD 以获得可能更好的结果。我不确定您需要与您的网站进行何种程度的集成,但我建议您查看开源 Gallery 项目在开始编码之前。

You can use ImageMagick instead of GD to get probably better results. I'm not sure what level of integration with your website you need but I suggest checking the open source Gallery project before you start coding.

ζ澈沫 2024-08-20 15:58:41

输出图像时使用 imagejpeg 的质量参数怎么样?

How about using the quality argument of imagejpeg when outputting your image?

一生独一 2024-08-20 15:58:41

我使用这个简单的 shell 脚本,它需要从 ImageMagick 进行转换

#!/bin/sh

W=256
T="Some recent photos"

cat <<EOF
<HTML>
<HEAD>
<TITLE>$T</TITLE>
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
<BODY STYLE="font-family: sans-serif">
<H1>$T</H1>
Click on the photos to enlarge<P>
EOF

for i
do
convert -resize $W "$i" "T$i"
cat <<EOF
<A HREF="$i"><IMG SRC="T$i" WIDTH=$W></A>
EOF
done

cat <<EOF
</BODY></HTML>
EOF

I use this simple shell script, which needs convert from ImageMagick:

#!/bin/sh

W=256
T="Some recent photos"

cat <<EOF
<HTML>
<HEAD>
<TITLE>$T</TITLE>
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
<BODY STYLE="font-family: sans-serif">
<H1>$T</H1>
Click on the photos to enlarge<P>
EOF

for i
do
convert -resize $W "$i" "T$i"
cat <<EOF
<A HREF="$i"><IMG SRC="T$i" WIDTH=$W></A>
EOF
done

cat <<EOF
</BODY></HTML>
EOF
从﹋此江山别 2024-08-20 15:58:41

我将使用一个文件上传,然后使用 ImageMagick 在服务器上创建缩略图
(http://www.imagemagick.org/script/index.php)

I would use one file upload and then use ImageMagick to create the thumbnail on the server
(http://www.imagemagick.org/script/index.php)

她说她爱他 2024-08-20 15:58:41

如果您对图像的存储位置不挑剔,则可以使用 Flickr PHP API。由于 flickr 提供了各种尺寸的图像,因此您可以自行设计图库的布局。

查看 http://phpflickr.com/

我已经在一个项目中使用了它,并且对它的简单性感到惊讶曾是。您基本上可以通过该 API 执行所有操作,并且还可以为多个用户执行此操作,因此您将为自己和其他人节省大量时间。更不用说空间了!-)

If you're not picky about where the images are stored, you could use the Flickr PHP API. Since flickr provides the images in a variety of sizes, it leaves you with making the layout for the gallery.

Check out http://phpflickr.com/

I've used this for a project and was surprised at how easy it was. You can basically do every operation through that API, and you can do it for several users too, so you're going to save yourself and others lots of time. And not to mention space!-)

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