1

我有一个使用 Wordpress 允许用户提交图像的竞赛网站。有 700 多个条目,每个条目最多 6 个图像,每个图像的最大文件大小为 3mb。

然后我将所有条目列为帖子,然后在每个帖子上显示图像。

每个图像都使用the_field('image1');the_field('image2');等显示the_field('image3');

我遇到的问题是该页面尝试以 3mb 的速度加载 6 张图像。有没有办法即时优化图像并以 50% 的质量显示?

4

4 回答 4

3

您可以在从数据库(或文件)中提取图片后即时调整图片大小,而无需创建另一个临时文件。大概你有一个从数据库中获取图像的 URL。

$pdo = new PDO("somewhere");
$sql = "SELECT image_data FROM pictures WHERE id=1";

$stmt = $pdo->query( $sql );
$obj = $stmt->fetch(PDO::FETCH_OBJ);

$img = imagecreatefromstring($obj->image_data);

header('Content-Type: image/jpeg');
imagejpeg($img, NULL, 50);

imagedestroy($img);

If you pass NULL as a file name to imagejpeg it will write the jpeg to standard out. The 3rd parameter is the quality level, in this case 50%.

于 2013-10-04T14:39:54.090 回答
0

图片上传后,可以保存2张图片。1.原始图像 2.图像的低分辨率副本

因此,当用户查看页面时,最初会显示低分辨率图像,而在后台下载原始图像。

你可以很容易地找到一些现成的代码来通过 php 调整图像大小。像这个。 http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/

于 2013-04-07T20:17:58.353 回答
0
$file =  $_GET['file'];//URL with img parameter http://localhost/fk/test.php?file=1.jpeg
$quality = 40; //Change What quality you needed
$location = "images/".$file;
$image = imagecreatefromjpeg($file);
imagejpeg($image, $location, $quality) ;
header('Content-Type: image/jpeg');
    header('Content-Length: ' . filesize($location));
    header('Content-Disposition: inline; filename="'.$file.'"');
    echo file_get_contents($location);
于 2021-08-02T12:22:21.263 回答
-1

无法即时调整图像大小。对于这种拒绝服务的自我攻击,只有直接的方法可以让 CPU 窒息。

上传后,图像必须预先调整到合理的大小和质量。世界上的每个网站都是这样做的。

于 2013-04-07T19:47:21.657 回答