我正在开发一个允许用户上传和销售不同尺寸的艺术品的网站。我想知道自动处理不同文件大小的最佳方法是什么。我很好奇的几点:
如何定义不同的尺寸类别(小、中、大),以便我能够以成比例的尺寸动态地重新调整图像的大小。
我应该存储不同大小的实际 jpeg 以供下载吗?或者更容易生成这些不同的大小以供即时下载
我的缩略图会比你的平均缩略图大一些,我应该存储第二个“缩略图图像”,上面覆盖有网站水印吗?或者再一次,即时生成?
非常感谢所有意见,建议!
我正在开发一个允许用户上传和销售不同尺寸的艺术品的网站。我想知道自动处理不同文件大小的最佳方法是什么。我很好奇的几点:
如何定义不同的尺寸类别(小、中、大),以便我能够以成比例的尺寸动态地重新调整图像的大小。
我应该存储不同大小的实际 jpeg 以供下载吗?或者更容易生成这些不同的大小以供即时下载
我的缩略图会比你的平均缩略图大一些,我应该存储第二个“缩略图图像”,上面覆盖有网站水印吗?或者再一次,即时生成?
非常感谢所有意见,建议!
我用aarongriffin.co.uk做这样的事情。
在那里,一些图像在第一次被请求时动态调整大小,然后存储;而其他是在上传时生成的。倾向于成组请求的图像(即缩略图)在上传时生成,而倾向于一次显示一个的图像是动态生成的。这对我来说效果很好,但这是一个没有太多流量的网站。
我在 Python 和 Django 中工作,所以我为此使用 sorl-thumbnail。在 PHP 中,您可以访问各种 imagecreatefrom* 函数,它们(有点)做同样的事情。
我生成带水印的照片版本(如果应该为特定相册加水印),并存储这些版本而不是未加水印的副本。
您可以为此检查 php 缩略图。这是一个可能有用的代码片段。
<?php
# Constants
define(IMAGE_BASE, '/var/www/html/mbailey/images');
define(MAX_WIDTH, 150);
define(MAX_HEIGHT, 150);
# Get image location
$image_file = str_replace('..', '', $_SERVER['QUERY_STRING']);
$image_path = IMAGE_BASE . "/$image_file";
# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
$img = @imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
$img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
$img = @imagecreatefrompng($image_path);
}
# If an image was successfully loaded, test the image for size
if ($img) {
# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
# Copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}
# Create error image if necessary
if (!$img) {
$img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}
# Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
?>