Fluesopp
Fluesopp

Reputation: 93

PHP Unable to convert variables to int

I'm struggling with converting variables from $_POST[] to int (long) in order to satisfy a function which takes long.

This function requires some input variables ($width and $height) which are both long. The script gets these variables from $_POST[] and they are of course string upon fetching. I have tried several methods for converting these variables into either float, int and long:

$variable = (float) $_POST["variable"];

and

$variable = $_POST["variable"] + 0;

and

settype($variable, "float");

But I still get the same error in error.log no matter what I do:

PHP Warning: imagecreatetruecolor() expects parameter 1 to be long, string given in /bla bla bla/resize_image.php on line 30

It's gotten to the point where I'm tired of looking for solutions on Google since nothing seems to convert the damn thing anyway. So I'm asking you guys if there's something I'm overlooking or if this is even possible.

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;

}

I'm in desperate need!!

Upvotes: 0

Views: 297

Answers (2)

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21502

The warning

PHP Warning: imagecreatetruecolor() expects parameter 1 to be long, string given...

means that the first parameter is a string. Let's look at the function call in resize_image.php:

$dst = imagecreatetruecolor($newwidth, $newheight);

The first argument is $newwidth which is assigned to either $w, or $h*$r. The result of multiplication is always a number (float or integer). However, $w is passed to the function without type casting:

if (isset($_POST["cropped"])) {
    $cropped = $_POST["cropped"];
}

// ...

$data = resize_image($url, $cropped, $width, $height, $_POST["type"]);

There is no type casting for the $cropped (the second argument) within the function, too.

So you need to cast $w to integer either in the function call, or within resize_image. It is better to sanitize parameters within the function body:

function resize_image($file, $w, $h, $crop=FALSE, $type) {
  $w = (int)$w;
  $h = (int)$h;
  // ...

Ah, and you probably didn't mean to pass $cropped as $w.

Upvotes: 1

Alvin Bunk
Alvin Bunk

Reputation: 7764

I'm wondering if the post is actually numeric. Maybe try:

$int = (is_numeric($_POST['variable']) ? (int)$_POST['variable'] : 0);

Returns 0 if not numeric, you can modify as needed.

Upvotes: 0

Related Questions