javascript - 如何使用户上传的图片自动调整到我的 PHP 网站上的特定宽度和高度而不破坏尺寸?
问题描述
我创建了一个用户可以上传图片的网站,但我没有指定他们可以上传的图片尺寸。那么,如何让用户上传的图片自动调整到数据库中的特定宽度和高度,然后才能将其提取并显示在我的网站上?
解决方案
早上好。
我想你可以使用这样的函数:
function resize_my_image_to_max_900($my_image){
$control_width = imagesx($my_image); // Store original width value
$control_height = imagesy($my_image); // Store original height value
$n_width = $control_width; // Default, case value <= 900
$n_height = $control_height; // Default, case value <= 900
if($n_width >= 900 || $n_height >= 900):
while(($n_width >= 900) || ($n_height >= 900)):
$n_width = $control_width - ($control_width * 0.25);
$n_height = $control_height - ($control_height * 0.25);
if($n_width < 900 || $n_height < 900): break; endif;
endwhile;
endif;
$new_image = imagecreatetruecolor($n_width, $n_height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagecopyresampled($new_image, $my_image, 0, 0, 0, 0, $n_width, $n_height, $control_width, $control_height);
}
祝您有美好的一天和出色的工作。
推荐阅读
- amazon-redshift - 运行查询以查找计数差异时出错
- css - Scss 使用循环创建动态混合
- kernel - 从 rocko 更新到 thud 后 .dtb 文件丢失
- r - 在 ggplot/stan 的跟踪图面板中更改单个图的标题
- sql - 检索员工的主要和次要功能
- javascript - 为什么 req.query.name 返回未定义?
- android - 我想在 android kotlin 的 recyclerView 内创建一个微调器,当进入 ArrayAdapter 时,错误显示
- javascript - 无法在 beforeCreate 挂钩中设置日期 - Strapi (beta14)
- sql - Postgres:简化 SQL 查询以摆脱子选择
- firefox - vscode:将调试器附加到Firefox