在网站中显示图片时是否可以调整图片大小?/上传图片后如何调整图片大小?
添加时,我上传原始图像到原始文件夹,并创建1个缩略图上传到拇指文件夹。但在网站中,我需要在如此多的不同维度的地方显示图像。因此,我需要重新调整图像大小以显示所需的大小,以避免图像收缩。
或者,我是否需要在上传时为所有需要的维度创建一个图像?
发布于 2012-07-13 13:00:56
当然,这是可能的。此外,这可能是最好的方法:根据请求创建必要的缩略图。如果你的网站正处于开发阶段,你永远也猜不到明天会有什么维度的设计师出现。您将一次又一次地返回到您的上传功能,以适应新的设计。消除设计和逻辑之间的硬依赖是值得的。
我对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);发布于 2012-07-13 12:49:45
如果您获得高度和宽度比率,并以相同比率动态调整新的高度和宽度,则可以在不收缩的情况下重新调整图像大小。但当你需要小图像时,如果你加载大图像,并使用高度和宽度来显示小尺寸。这对用户来说是不必要的加载时间。
发布于 2012-07-13 13:01:16
Arun Jain的答案以一种方便的方式解决了你的问题的技术/实际方面。
更广泛地说,当您处理这样的计算机密集型任务时,大多数时候采用懒惰的方式是一个好主意。至少从CPU的角度来看是这样。
原因是,在实际请求之前,您永远不知道给定的图像是否会以给定的分辨率加载。一旦请求了它,在大多数情况下,在每次请求时重新计算它比存储它以供假想的以后使用的成本更高。一种常见的设计是在请求时计算图像/调整图像大小,如果在缓存中尚不可用,然后存储以供以后使用。
timthumb库似乎可以很好地处理所有这些问题,尽管我自己没有使用过它。
我在这个库中做了一些非常基本的检查,它似乎考虑到了安全性,但我想强调的是,它被宣传为测试版软件。
https://stackoverflow.com/questions/11463921
复制相似问题