Re: scaling and uploading the pt 2

From: Date: Thu, 20 Sep 2007 17:54:04 +0000
Subject: Re: scaling and uploading the pt 2
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-262302@lists.php.net to get a copy of this message
Hulf wrote:
This is the full code so far. The files are saving but they are not resizing. Can anyone help? I need to get this $myimage = imagejpeg($thumb); into $target_path directory. H. ------------------------------------------ if(isset($_POST['_upload']) && $_FILES['userfile']['size'] > 0) { $imageinfo = getimagesize($_FILES['userfile']['tmp_name']); echo $width=$imageinfo[0]; echo $height=$imageinfo[1]; $newwidth = 300; $newheight = 200; // Load $thumb = imagecreatetruecolor($newwidth, $newheight); $source = imagecreatefromjpeg($_FILES['userfile']['tmp_name']); // Resize $myimage = imagecopyresized($thumb, $source, 0, 0, 0, 0, 300, 200, $width, $height);
Don't assign to $myimage here. imagecopyresized() returns a boolean.
// Output $myimage = imagejpeg($thumb);
Same here.
$target_path = "../property_images/$property_id/".basename( $_FILES['userfile']['name']);
I can't see how you're able to save the file using this path. move_uploaded_file() requires a path from server root (not DOCUMENT_ROOT).
$img_url= $property_id."/".basename( $_FILES['userfile']['name']); if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) { /* echo "The file ". basename( $_FILES['userfile']['name']).
    " has been uploaded";*/
} else{
    echo "There was an error uploading the file, please try again!";
}
Here, you're saving the originally-uploaded file, not the re-sized version you created. You want to pass the path to imagejpeg() to have it save the new image at the destination (the 3rd param is the quality setting). Do: // note, also, that you had the dest. width & height hard-coded. imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); $target_path = // resolve this if (imagejpeg($thumb, $target_path)) { echo 'Huzzah!'; } else { echo 'back to php-general ...'; } brian

« previous php.general (#262302) next »