I'm new in php and I'd just like to create a simple website, for that I'd like to resize and upload an image to a server. I'm able to resize it but I don't know how to upload it. Here is my code:
<td><form action="uploadpic.php" method="POST" enctype="multipart/form-data" />
<input type="file" name="user_image" id="user_image" />
<input name = "button" type = "button" id = "button"
value = "Küldés" onclick="subm(this.form,'_blank');">
</form>
</td>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function subm(f,newtarget){
f.submit();
}
</script>
And uploadpic.php:
$target_path = "images/clients/";
$target_path = $target_path . basename( $_FILES['user_image']['name']);
function resize_image($file, $w, $h, $crop=FALSE) {
list($width, $height) = getimagesize($_FILES['user_image']['tmp_name']);
echo "original width ". $width." height ". $height;
$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;
}
}
echo "new width ". $newwidth." height ". $newheight;
//$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
//imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
function print_var_name($var) {
foreach($GLOBALS as $var_name => $value) {
if ($value === $var) {
return $var_name;
}
}
return false;
}
$img = resize_image($target_path, 50, 50);
if(move_uploaded_file(print_var_name($img), $target_path)) {
echo "The file ". basename( $_FILES['user_image']['name']).
"Image has been uploaded";
} else{
echo "Not uploaded because of error ".print_var_name($img)['error'];
}
After I did a var_dump($_FILES);
I got this:
array(1) { ["user_image"]=> array(5) { ["name"]=> string(10) "000oks.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(14) "/tmp/phpt7HaO1" ["error"]=> int(0) ["size"]=> int(606917) } }
The resize funciton is works fine after that I have created a new variable and another funciton to get name of the variable. In move_uploaded_file
the first parameter has to be a string: the name of the file which is now img
and the second parameter is the path. What am I doing wrong?