php - 使用 GD 调整 PHP 图像大小到许多 CPU 时间
问题描述
我有一个关于 PHP GD 的脚本,它具有简单的功能,如何在我的网站上调整实时图像的大小,但是这个过程“吃掉”了我所有的 CPU 时间……我使用 4 核的 VPS,这还不够。
从图像标记中,我<img src="image.php?file=XXX&width=XXX&height=XXX" alt="" />
使用图像名称、新宽度和新高度调用 PHP 文件 ()……然后在 PHP 中进行以下操作:
ob_start();
ini_set('max_execution_time', 0);
set_time_limit(0);
error_reporting(E_ALL^E_NOTICE^E_DEPRECATED);
date_default_timezone_set('Europe/Sofia');
header('Content-type: image/jpeg');
$url = $_GET['file'];
if(!empty($url) && file_exists($url) && is_file($url) && is_readable($url)){
$width = $_GET['width'];
$height = $_GET['height'];
list($width_orig, $height_orig) = getimagesize($url);
if(empty($width)) $width = $width_orig/($height_orig/$height);
if(empty($height)) $height = $height_orig/($width_orig/$width);
$ratio_orig = $width_orig/$height_orig;
if($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($url);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, null, 100);
imagedestroy($image_p);
imagedestroy($image);
}
ob_end_flush();
我不明白为什么要“吃”来匹配 CPU 时间……我的原始图像尺寸是 1920x2880 像素。如何减少我的 CPU 使用率?有任何想法吗?
解决方案
推荐阅读
- c++ - 在 CUDA 内核模板函数中,如何测试类型?
- typescript - tsc --build 多次输出相同的错误
- flutter - 如何在管理状态(GetX)的同时使用动画?
- c# - 在 C# 中,如何使用反射添加 FluentValidation 的实例?
- macos - vscode 的扩展指南是开源的吗?
- sql - 如何在一个语句中使用单个值 UPSERT 多行?
- math - 百分位数和分位数的区别以及如何计算
- javascript - 完成时自动更新表单字段
- c++ - 可变类成员(元组或其他)的参数包扩展
- nao-robot - 我无法将我的 NAO 机器人与 Wi-Fi 网络连接