【问题标题】:How to re-size an image while display?如何在显示时调整图像大小?
【发布时间】:2012-07-13 03:59:52
【问题描述】:

在网站上显示图片时是否可以调整图片大小?/图片上传后如何调整图片大小?

在添加时,我将原始图像上传到原始文件夹并创建 1 个缩略图上传到拇指文件夹。但是在网站上,我需要在很多不同维度的地方显示图像。所以我需要重新调整图像的大小以显示所需的大小以避免图像缩小。

或者我是否需要在上传时为我需要的所有尺寸创建图像?

【问题讨论】:

  • "或者我是否需要为上传时需要的所有尺寸创建图像?" -- 为什么不呢?
  • 是的,更喜欢先调整/裁剪尽可能多的尺寸,因为这对 apache 来说是一项繁重的工作。
  • 是的,在上传阶段调整大小。
  • 我总共需要 8 个不同尺寸的图像,所以我是否需要创建 8 个文件夹并在添加时需要为每个尺寸重新调整大小?我认为我们可以在显示时重新调整图像大小...

标签: php image codeigniter image-resizing


【解决方案1】:

当然,这是可能的。此外,这可能是最好的方法:根据请求创建必要的缩略图。如果您的网站处于开发阶段,您将永远猜不到明天将创建设计师的维度。您将一次又一次地返回上传功能以适应新设计。消除设计和逻辑之间的硬依赖是值得的。

我不确定 codeignitor。无论如何,在你的模板中使用这样的东西:

class Image {

    public $filename;
    public $caption;

    /**
     * Return full path to image.
     * @return string path to file to make thumb
     */
    public function fullPath() {
        return "data/files/{$this->filename}";
    }

    /**
     * Renders HTML IMG for thumb of given size.
     * 
     * @param int $width max width, set to -1, if not important
     * @param type $height max height, set to -1, if not important
     * @return string html tag for image with correct width and height attributes
     */
    public function htmlTag($width, $height) {
        $t = $this->getThumb($width, $height);
        return "<img src=\"{$t}\" alt=\"{$this->caption}\" width=\"{$width}\" height=\"{$height}\" />";
    }

    /**
     * Get/create thumb image
     * @param int $width width of the image
     * @param int $height height of the image
     * @return string path to the image
     */
    public function getThumb(&$width, &$height) {
        $currentImage = $this->fullPath();

        $thumbFilename = md5($this->path . $width . $height) . '.png';

        $thumbDir = 'data/thumbs/';
        $thumbFilename = "{$thumbDir}/{$thumbFilename}";

        // thumb already done?
        if (is_file($thumbFilename)) {
            // get real size to create correct html img tag
            if ($width<0 || $height<0) {
                $size = getimagesize($thumbFilename);
                $width = $size[0];
                $height = $size[1];
            }
            return $thumbFilename;
        }

        $ext = strtolower(pathinfo($currentImage, PATHINFO_EXTENSION));
        if ($ext == 'jpeg' || $ext == 'jpg') {
            $source = imagecreatefromjpeg($currentImage);
        } else if ($ext == 'gif') {
            $source = imagecreatefromgif($currentImage);
        } else if ($ext == 'png') {
            $source = imagecreatefrompng($currentImage);
        }

        $currentWidth = imagesx($source);
        $currentHeight = imagesy($source);

        // the sizes which we really will apply (default setup)
        $realWidth = $width;
        $realHeight = $height;
        $realX = 0;
        $realY = 0;

        // decide regarding cutting
        // if all params > 0, cuttin will be done
        $cut = FALSE;
        if ($width > 0 && $height > 0) {
            $cut = TRUE;
        } else if ($width < 0) { // width is not important, set proportion to that
            $width = $realWidth = round($currentWidth * $height / $currentHeight);
        } else if ($height < 0) { // height is not imporant
            $height = $realHeight = round($currentHeight * $width / $currentWidth);
        }

        if ($cut) {
            $kw = $currentWidth / $width;
            $kh = $currentHeight / $height;

            $k = $kw < $kh ? $kw : $kh;

            $realWidth = round($currentWidth / $k);
            $realHeight = round($currentHeight / $k);

            if ($kh < $kw) {
                $realX = round(($realWidth - $width) / 2) * $k;
            } else {
                $realY = round(($realHeight - $height) / 2) * $k;
            }
        }

        $virtual = imagecreatetruecolor($width, $height);
        imagealphablending($virtual, false);
        $col = imagecolorallocatealpha($virtual, 0, 0, 0, 127);
        imagefill($virtual, 0, 0, $col);
        imagesavealpha($virtual, true);

        imagecopyresampled($virtual, $source, 0, 0, $realX, $realY, $realWidth, $realHeight, $currentWidth, $currentHeight);

        // create file
        imagepng($virtual, $thumbFilename);

        return $thumbFilename;
    }
}

用法:

$image = new Image();
$image->filename = "image.jpeg"; // really stored in 'data/files/image.jpg', let's say 300x400px
$image->caption = "My Image";

// get thumb 50x50: left and right parts of image will be cut off
echo $image->htmlTag(50, 50);

// get thumb of width 100 (height does not matter, keep proportions)
echo $image->htmlTag(100, -1);

// get thumb of height 100 (width does not matter, keep proportions)
echo $image->htmlTag(-1, 100);

【讨论】:

    【解决方案2】:

    Arun Jain 的答案以方便的方式解决了您问题的技术/实践方面。

    更一般地说,当您处理此类计算机密集型任务时,大多数时候以懒惰的方式来做是个好主意。至少从 CPU 的角度来看。

    原因是您永远不知道给定的图像是否会在实际请求之前以给定的分辨率加载。一旦请求它,大多数时候在每个请求上重新计算它比存储它以供假设的以后使用更昂贵。 一种常见的设计是在请求时计算/调整图像大小(如果缓存中尚不可用),然后存储以供以后使用。

    timthumb 库似乎可以很好地处理所有这些问题,因为我自己没有使用过。

    我在库中做了一些非常基本的检查,它似乎考虑到了安全性,但我想强调它被宣传为测试版软件。

    【讨论】:

      【解决方案3】:

      如果您获得高度和宽度比例并且以相同的比例动态调整新的高度和宽度,则可以在不缩小图像的情况下重新调整图像大小。但是当您需要小图像时,如果您加载大图像并使用高度和宽度以小尺寸显示。这对用户来说是不必要的加载时间。

      【讨论】:

      • 那么,有什么替代解决方案吗?
      • Alternate 只是制作不同大小的缩略图。否则你必须承认网站加载性能问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 2022-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-12
      • 2017-06-21
      相关资源
      最近更新 更多