我正在开发一个网站,允许用户上传和销售不同尺寸的艺术作品。我想知道自动处理不同文件大小的最佳方法是什么。我很好奇的几点:
如何定义不同大小的类别(小,中,大),以便能够动态调整比例尺寸的图像。
我应该存储不同大小的实际jpeg以供下载吗?或者更容易生成这些不同的大小以便即时下载
我的缩略图会比平均缩略图大一些,我应该存储第二个“缩略图”,网站水印覆盖吗?或者再一次,即时产生这个?
非常感谢所有意见,建议!
答案 0 :(得分:2)
我使用aarongriffin.co.uk做了类似的事情。
在那里,一些图像在第一次被请求时动态调整大小,然后存储它们;而其他人则是在上传时生成的。在上传时生成倾向于以组(即缩略图)请求的图像,并且在运行中生成倾向于一次显示一个的图像。这对我来说效果很好,但这是一个没有太多流量的网站。
我正在使用Python和Django,因此我使用了sorl-thumbnail。在PHP中,您可以访问各种imagecreatefrom *函数,这些函数可以执行相同的操作。
我生成了水印版本的照片(如果特定的专辑应该加水印),并存储这些照片而不是非水印副本。
答案 1 :(得分:1)
你可以查看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);
?>