PHP 无法将变量转换为 int
PHP Unable to convert variables to int
我正在努力将变量从 $_POST[] 转换为 int (long) 以满足需要很长时间的函数。
此函数需要一些长输入变量($width 和 $height)。该脚本从 $_POST[] 获取这些变量,它们在获取时当然是字符串。我尝试了几种方法将这些变量转换为 float、int 和 long:
$variable = (float) $_POST["variable"];
和
$variable = $_POST["variable"] + 0;
和
settype($variable, "float");
但无论我做什么,我仍然在 error.log 中遇到同样的错误:
PHP Warning: imagecreatetruecolor() expects parameter 1 to be long, string given in /bla bla bla/resize_image.php on line 30
已经到了我厌倦了在 Google 上寻找解决方案的地步,因为似乎没有任何东西可以转换该死的东西。所以我问你们是否有我忽略的东西,或者这是否可能。
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;
resize_image.php (Class)
function resize_image($file, $w, $h, $crop=FALSE, $type) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
if ($type == "png") {
$src = imagecreatefrompng($file);
} else if ($type == "jpeg") {
$src = imagecreatefromjpeg($file);
}
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
我非常需要!!
我想知道 post 是否真的是数字。
也许试试:
$int = (is_numeric($_POST['variable']) ? (int)$_POST['variable'] : 0);
Returns0如果不是数字,可以根据需要修改。
警告
PHP Warning: imagecreatetruecolor() expects parameter 1 to be long, string given...
表示第一个参数是字符串。我们看一下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_image
中将 $w
转换为整数。最好在函数体内清理参数:
function resize_image($file, $w, $h, $crop=FALSE, $type) {
$w = (int)$w;
$h = (int)$h;
// ...
啊,你可能不是想将 $cropped
传递为 $w
。
我正在努力将变量从 $_POST[] 转换为 int (long) 以满足需要很长时间的函数。
此函数需要一些长输入变量($width 和 $height)。该脚本从 $_POST[] 获取这些变量,它们在获取时当然是字符串。我尝试了几种方法将这些变量转换为 float、int 和 long:
$variable = (float) $_POST["variable"];
和
$variable = $_POST["variable"] + 0;
和
settype($variable, "float");
但无论我做什么,我仍然在 error.log 中遇到同样的错误:
PHP Warning: imagecreatetruecolor() expects parameter 1 to be long, string given in /bla bla bla/resize_image.php on line 30
已经到了我厌倦了在 Google 上寻找解决方案的地步,因为似乎没有任何东西可以转换该死的东西。所以我问你们是否有我忽略的东西,或者这是否可能。
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;
resize_image.php (Class)
function resize_image($file, $w, $h, $crop=FALSE, $type) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
if ($type == "png") {
$src = imagecreatefrompng($file);
} else if ($type == "jpeg") {
$src = imagecreatefromjpeg($file);
}
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
我非常需要!!
我想知道 post 是否真的是数字。 也许试试:
$int = (is_numeric($_POST['variable']) ? (int)$_POST['variable'] : 0);
Returns0如果不是数字,可以根据需要修改。
警告
PHP Warning: imagecreatetruecolor() expects parameter 1 to be long, string given...
表示第一个参数是字符串。我们看一下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_image
中将 $w
转换为整数。最好在函数体内清理参数:
function resize_image($file, $w, $h, $crop=FALSE, $type) {
$w = (int)$w;
$h = (int)$h;
// ...
啊,你可能不是想将 $cropped
传递为 $w
。