upload en resize (Opgelost)
JeroenI - 14/03/2013 12:13 (laatste wijziging 14/03/2013 15:13)
PHP interesse
Hallo allemaal,
Ik wil via onderstaande code een foto kunnen uploaden, en direct resizen.
if(isset($_POST['submit']))
{
if (isset ($_FILES['new_image']))
{
$imagename = $_FILES['new_image']['name'];
$source = $_FILES['new_image']['tmp_name'];
$target = "avatar/".$imagename;
move_uploaded_file($source, $target);
$imagepath = $imagename;
$save = "avatar/" . $imagepath; //This is the new file you saving
list($width, $height) = getimagesize($target);
$modwidth = 100;
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagejpeg($tn, $save, 100) ;
unlink($target); //Delete our uploaded file
echo "Large image: <img src='avatar/".$save."'><br>";
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input name="new_image" id="new_image" size="30" type="file" class="fileUpload" />
<button name="submit" type="submit" class="submitButton">Upload Image</button>
</form>
if ( isset ( $_POST [ 'submit' ] ) ) {
if ( isset ( $_FILES [ 'new_image' ] ) ) {
$imagename = $_FILES [ 'new_image' ] [ 'name' ] ;
$source = $_FILES [ 'new_image' ] [ 'tmp_name' ] ;
$target = "avatar/" . $imagename ;
$imagepath = $imagename ;
$save = "avatar/" . $imagepath ; //This is the new file you saving
$modwidth = 100 ;
$diff = $width / $modwidth ;
$modheight = $height / $diff ;
$tn = imagecreatetruecolor( $modwidth , $modheight ) ;
$image = imagecreatefromjpeg( $file ) ;
imagecopyresampled( $tn , $image , 0 , 0 , 0 , 0 , $modwidth , $modheight , $width , $height ) ;
imagejpeg( $tn , $save , 100 ) ;
unlink ( $target ) ; //Delete our uploaded file
echo "Large image: <img src='avatar/" . $save . "'><br>" ; }
}
?>
< form action= "" method= "post" enctype= "multipart/form-data" >
< input name= "new_image" id= "new_image" size= "30" type= "file" class = "fileUpload" />
< button name= "submit" type= "submit" class = "submitButton" > Upload Image</ button>
</ form>
Nu krijg ik een error op regel 27 en 28, dat zijn deze twee:
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
namelijk error:
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: Filename cannot be empty in *** on line 27
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in *** on line 28
Ik weet eerlijk gezegd niet hoe ik dit kan fixen... ik werk niet zoveel met uploaden enzo, dus als iemand mij kan vertellen wat hier mis gaat ben ik erg blij.
Alvast bedankt!
2 antwoorden
Gesponsorde links
vinTage - 14/03/2013 12:32 (laatste wijziging 14/03/2013 12:34)
Nieuw lid
probeer je wel een jpg te uploaden?
edit : ah $file bestaat niet in je script.
JeroenI - 14/03/2013 12:38 (laatste wijziging 14/03/2013 15:53)
PHP interesse
Waar moet $file naartoe verwijzen dan ? Moet dat toevallig gekoppeld worden aan $_FILES['new_image']['name']; ?
[offtopic]
Vintage waarom sta jij op zowel online als offline?
edit
Ik heb nu dit
if(isset($_POST['submit'])){
if (isset ($_FILES['new_image'])){
$imagename = $_FILES['new_image']['name'];
$source = $_FILES['new_image']['tmp_name'];
$target = "avatar/".$imagename;
move_uploaded_file($source, $target);
$file = "avatar/" . $imagepath; //This is the original file
$imagepath = $imagename;
$save = "avatar/" . $imagepath; //This is the new file you saving
list($width, $height) = getimagesize($target);
$modwidth = 100;
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagejpeg($tn, $save, 100) ;
unlink($target); //Delete our uploaded file
echo "Large image: <img src='avatar/".$save."'><br>";
}
}
if ( isset ( $_POST [ 'submit' ] ) ) { if ( isset ( $_FILES [ 'new_image' ] ) ) { $imagename = $_FILES [ 'new_image' ] [ 'name' ] ;
$source = $_FILES [ 'new_image' ] [ 'tmp_name' ] ;
$target = "avatar/" . $imagename ;
$file = "avatar/" . $imagepath ; //This is the original file
$imagepath = $imagename ;
$save = "avatar/" . $imagepath ; //This is the new file you saving
$modwidth = 100 ;
$diff = $width / $modwidth ;
$modheight = $height / $diff ;
$tn = imagecreatetruecolor( $modwidth , $modheight ) ;
$image = imagecreatefromjpeg( $file ) ;
imagecopyresampled( $tn , $image , 0 , 0 , 0 , 0 , $modwidth , $modheight , $width , $height ) ;
imagejpeg( $tn , $save , 100 ) ;
unlink ( $target ) ; //Delete our uploaded file
echo "Large image: <img src='avatar/" . $save . "'><br>" ; }
}
maar krijg nu volgende errors:
Warning: imagecreatefromjpeg(avatar/) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in *** on line 28
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in *** on line 29
edit #2
Ik heb nu deze code, zonder foutmeldingen maar hij plaatst het bestand ook niet in de map avatar
if(isset($_POST['submit'])){
if (isset ($_FILES['new_image'])){
$imagename = $_FILES['new_image']['name'];
$source = $_FILES['new_image']['tmp_name'];
$target = "avatar/".$imagename;
move_uploaded_file($source, $target);
$imagepath = $imagename;
$file = "avatar/" . $imagepath; //This is the original file
$save = "avatar/" . $imagepath; //This is the new file you saving
list($width, $height) = getimagesize($target);
$modwidth = 100;
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($target) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagejpeg($tn, $save, 100) ;
unlink($target); //Delete our uploaded file
echo "Large image: <img src='avatar/".$save."'><br>";
}
}
if ( isset ( $_POST [ 'submit' ] ) ) { if ( isset ( $_FILES [ 'new_image' ] ) ) { $imagename = $_FILES [ 'new_image' ] [ 'name' ] ;
$source = $_FILES [ 'new_image' ] [ 'tmp_name' ] ;
$target = "avatar/" . $imagename ;
$imagepath = $imagename ;
$file = "avatar/" . $imagepath ; //This is the original file
$save = "avatar/" . $imagepath ; //This is the new file you saving
$modwidth = 100 ;
$diff = $width / $modwidth ;
$modheight = $height / $diff ;
$tn = imagecreatetruecolor( $modwidth , $modheight ) ;
$image = imagecreatefromjpeg( $target ) ;
imagecopyresampled( $tn , $image , 0 , 0 , 0 , 0 , $modwidth , $modheight , $width , $height ) ;
imagejpeg( $tn , $save , 100 ) ;
unlink ( $target ) ; //Delete our uploaded file
echo "Large image: <img src='avatar/" . $save . "'><br>" ; }
}
De rechten voor de map staan gewoon goed...
Gesponsorde links
Je moet ingelogd zijn om een reactie te kunnen posten.