PHP无法将变量转换为int
我正在努力将变量从$_POST[]转换为int(long),以满足需要较长时间的函数 此函数需要一些输入变量($width和$height),它们都是长的。脚本从$\u POST[]获取这些变量,当然,在获取时它们是字符串。我尝试了几种方法将这些变量转换为float、int和long:PHP无法将变量转换为int,php,Php,我正在努力将变量从$_POST[]转换为int(long),以满足需要较长时间的函数 此函数需要一些输入变量($width和$height),它们都是长的。脚本从$\u POST[]获取这些变量,当然,在获取时它们是字符串。我尝试了几种方法将这些变量转换为float、int和long: $variable = (float) $_POST["variable"]; 及 及 但无论我做什么,我仍然会在error.log中得到相同的错误: PHP Warning: imagecreatetruec
$variable = (float) $_POST["variable"];
及
及
但无论我做什么,我仍然会在error.log中得到相同的错误:
PHP Warning: imagecreatetruecolor() expects parameter 1 to be long, string given in /bla bla bla/resize_image.php on line 30
我已经厌倦了在谷歌上寻找解决方案,因为不管怎么说,似乎没有什么能改变这该死的东西。所以我想问你们,我是否忽略了什么,或者这是否可能
get_image.php
$url = "../../" . $_POST["url"];
$cropped = false;
$width = 0;
$height = 0;
if (isset($_POST["cropped"])) {
$cropped = $_POST["cropped"];
}
if (isset($_POST["width"])) {
$width = $_POST["width"];
}
if (isset($_POST["height"])) {
$height = $_POST["height"];
}
// Get image
$type = pathinfo($url, PATHINFO_EXTENSION);
$data = file_get_contents($url);
if ($width > 0 && $height > 0) {
include "Classes/resize_image.php";
settype ( $width , "float" );
settype ( $height , "float" );
$data = resize_image($url, $cropped, $width, $height, $_POST["type"]);
}
$base64 = base64_encode($data);
echo $base64;
调整大小_image.php(类)
}
我急需帮助 我想知道这篇文章是否真的是数字。 也许可以试试:
$int = (is_numeric($_POST['variable']) ? (int)$_POST['variable'] : 0);
如果不是数字,则返回0,您可以根据需要进行修改。警告
PHP警告:ImageCreateTureColor()要求参数1很长,字符串给定
表示第一个参数是字符串。让我们看看resize_image.php中的函数调用:
$dst = imagecreatetruecolor($newwidth, $newheight);
第一个参数是$newwidth
,分配给$w
或$h*$r
。乘法的结果总是一个数字(浮点或整数)。但是,$w
在不进行类型转换的情况下传递给函数:
if (isset($_POST["cropped"])) {
$cropped = $_POST["cropped"];
}
// ...
$data = resize_image($url, $cropped, $width, $height, $_POST["type"]);
函数中的$cropped
(第二个参数)也没有类型转换
因此,您需要在函数调用中或在resize\u image
中将$w
强制转换为整数。最好清除函数体中的参数:
function resize_image($file, $w, $h, $crop=FALSE, $type) {
$w = (int)$w;
$h = (int)$h;
// ...
啊,您可能不是有意传递$cropped
,因为$w
您是否尝试过floatval()
?看起来您调用函数时出错了<代码>$data=resize_image($url、$crapped、$width、$height、$_POST[“type”])
应该是$data=resize_image($url、$width、$height、$crapped、$_POST[“type”])代码>谢谢。你我真不敢相信我一直都错过了。。现在好了!
$dst = imagecreatetruecolor($newwidth, $newheight);
if (isset($_POST["cropped"])) {
$cropped = $_POST["cropped"];
}
// ...
$data = resize_image($url, $cropped, $width, $height, $_POST["type"]);
function resize_image($file, $w, $h, $crop=FALSE, $type) {
$w = (int)$w;
$h = (int)$h;
// ...