Php 如何在显示图像时重新调整图像大小?
在网站中显示图像时是否可以重新调整图像大小?/上传图像后如何重新调整图像大小 添加时,我正在将原始图像上载到原始文件夹,并创建一个缩略图上载到thumb文件夹。但在这个网站上,我需要在这么多不同维度的地方显示图像。因此,我需要重新调整图像的大小,使其达到显示所需的大小,以避免图像收缩Php 如何在显示图像时重新调整图像大小?,php,image,codeigniter,image-resizing,Php,Image,Codeigniter,Image Resizing,在网站中显示图像时是否可以重新调整图像大小?/上传图像后如何重新调整图像大小 添加时,我正在将原始图像上载到原始文件夹,并创建一个缩略图上载到thumb文件夹。但在这个网站上,我需要在这么多不同维度的地方显示图像。因此,我需要重新调整图像的大小,使其达到显示所需的大小,以避免图像收缩 或者我需要为上传时所需的所有尺寸创建一个图像吗?如果获得高度和宽度比率,并以相同比率动态调整新高度和宽度,则可以在不收缩的情况下重新调整图像大小。但当您需要小图像时,如果您加载大图像并使用高度和宽度以小尺寸显示。用
或者我需要为上传时所需的所有尺寸创建一个图像吗?如果获得高度和宽度比率,并以相同比率动态调整新高度和宽度,则可以在不收缩的情况下重新调整图像大小。但当您需要小图像时,如果您加载大图像并使用高度和宽度以小尺寸显示。用户不需要加载时间。如果获得高度和宽度比率,并以相同比率动态调整新高度和宽度,则可以在不收缩的情况下重新调整图像大小。但当您需要小图像时,如果您加载大图像并使用高度和宽度以小尺寸显示。这对于用户来说是不必要的加载时间。当然,这是可能的。此外,这可能是最好的方法:根据请求创建必要的缩略图。如果你的网站处于开发阶段,你永远也猜不到明天设计师会从哪些方面入手。您将一次又一次地返回上载功能,以适应新的设计。消除设计和逻辑之间的硬依赖是值得的 我不确定是否有共同点火器。无论如何,请在模板中使用以下内容:
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;
}
}
当然,这是可能的。此外,这可能是最好的方法:根据请求创建必要的缩略图。如果你的网站处于开发阶段,你永远也猜不到明天设计师会从哪些方面入手。您将一次又一次地返回上载功能,以适应新的设计。消除设计和逻辑之间的硬依赖是值得的 我不确定是否有共同点火器。无论如何,请在模板中使用以下内容:
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;
}
}
Arun Jain的回答以便捷的方式解决了您问题的技术/实践方面 更一般地说,当你处理这样的计算机密集型任务时,大多数时候用懒惰的方式是个好主意。至少从CPU的角度来看 原因是,在实际请求之前,您永远不知道是否会以给定的分辨率加载给定的图像。一旦它被请求,在大多数情况下,在每次请求时重新计算它要比存储它以备将来假想使用花费更高。 一种常见的设计是在请求时计算/调整图像大小(如果缓存中尚不可用),然后存储以供以后使用 timthumb库似乎可以很好地处理所有这些问题,尽管我自己没有使用过它
我在图书馆里做了一些非常基本的检查,它似乎考虑到了安全性,但我想强调的是,它被宣传为测试版软件。阿伦·贾恩的答案以一种简便的方式解决了您的问题的技术/实践方面 更一般地说,当你处理这样的计算机密集型任务时,大多数时候用懒惰的方式是个好主意。至少从CPU的角度来看 原因是,在实际请求之前,您永远不知道是否会以给定的分辨率加载给定的图像。一旦它被请求,在大多数情况下,在每次请求时重新计算它要比存储它以备将来假想使用花费更高。 一种常见的设计是在请求时计算/调整图像大小(如果缓存中尚不可用),然后存储以供以后使用 timthumb库似乎可以很好地处理所有这些问题,尽管我自己没有使用过它
我在库中做了一些非常基本的检查,它似乎考虑到了安全性,但我想强调的是,它被宣传为beta软件。“或者我应该在上传时为我需要的所有维度创建一个图像吗?”--为什么不?是的,希望调整/裁剪尽可能多的大小,只要你可以先使用,因为这对apache来说将是一项繁重的工作。是的,在上传阶段调整大小。我需要总共8个不同维度的图像,所以我需要创建8个文件夹,并在添加时需要重新调整每个维度的大小吗?我想我们可以在显示时重新调整图像的大小…”或者我需要在上传时为所有需要的尺寸创建一个图像吗?--为什么不?是的,我希望调整/裁剪尽可能多的尺寸,因为这对apache来说是一个沉重的工作负担。是的,在上传阶段进行调整。我需要8个不同尺寸的图像,因此,我是否需要创建8个文件夹,并需要在添加时重新调整每个维度的大小?我认为我们可以在显示时重新调整图像的大小…所以,有没有其他解决方案?alternate只是制作不同大小的缩略图。否则,你必须承认网站加载性能问题。那么,有没有其他解决方案?alternate只是制作不同大小的缩略图。否则,您必须承认网站加载性能问题。