what I have is a simple form to upload an image to the server, then I want to save both the original image to a folder, and the thumbnail to the same folder with a thumb_ in the name. The form works, the original image saves, but the thumbnail doesn't seem to be working at all, if someone run over it, and throw a sugestion or to I would be greatly appreciative.
the error seems to be within the following lines of code (I'm assuming)
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($imgfile);
imagecopyresized($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
or
$thufile = "/thum_$final_filename";
if (!move_uploaded_file($image_p, $uploaddir . $thufile)) {
//if (!copy($image_p, "$thufile")){
$upload_errors = array( '<li>File successfully uploaded</li>', '<li>Uploaded file exceeds the allowed size</li>', '<li>Uploaded file exceeds the server\'s allowed size</li>','<li>File was only partially uploaded, please try again</li>', '<li>No file was received. Upload again.</li>' );
$returnmsg = "There was a problem saving the file: ";
$returnmsg .= $upload_errors[$thufile['error']];
}
here's the whole scripts code I have so far:
<html>
<?php
function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$width = 100;
$height = 100;
if ($REQUEST_METHOD == "POST")
{
$uploaddir = ".";
/*== get file extension (fn at bottom of script) ==*/
/*== checks to see if image file, if not do not allow upload ==*/
$pext = getFileExtension($imgfile_name);
$pext = strtolower($pext);
if (($pext != "jpg") && ($pext != "jpeg"))
{
print "<h1>ERROR</h1>Image Extension Unknown.<br>";
print "<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY<br><br>";
print "The file you uploaded had the following extension: $pext</p>\n";
/*== delete uploaded file ==*/
unlink($imgfile);
exit();
}
//-- RE-SIZING UPLOADED IMAGE
$imgsize = GetImageSize($imgfile);
$width_orig = $imgsize[0];
$height_orig = $imgsize[1];
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($imgfile);
imagecopyresized($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
//$test = imagecreatefromstring($image_p);
//$test = image
/*== setup final file location and name ==*/
/*== change spaces to underscores in filename ==*/
$final_filename = str_replace(" ", "_", $imgfile_name);
$newfile = $uploaddir . "/$final_filename";
$thufile = "/thum_$final_filename";
/*== do extra security check to prevent malicious abuse==*/
if (is_uploaded_file($imgfile))
{
/*== move file to proper directory ==*/
if (!copy($imgfile,"$newfile"))
{
print "Error Uploading File.";
exit();
}
if (!move_uploaded_file($image_p, $uploaddir . $thufile)) {
//if (!copy($image_p, "$thufile")){
$upload_errors = array( '<li>File successfully uploaded</li>', '<li>Uploaded file exceeds the allowed size</li>', '<li>Uploaded file exceeds the server\'s allowed size</li>','<li>File was only partially uploaded, please try again</li>', '<li>No file was received. Upload again.</li>' );
$returnmsg = "There was a problem saving the file: ";
$returnmsg .= $upload_errors[$thufile['error']];
}
/*if (!move_uploaded_file($image_p, $thufile))
{
print "Error Uploading Thumbnail File.";
exit();
}*/
}
print("$returnmsg <HR>");
print("filename: " . $final_filename . "---------> Size: " . $imgsize[0] . " x " . $imgsize[1] . " <HR>");
print("$test2");
}
?>
</head>
<body bgcolor="#FFFFFF">
<h2>Upload an Image</H2>
<form action="<?=$SCRIPT_NAME; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<p>Upload Image: <input type="file" name="imgfile"><br>
<font size="1">Click browse to upload a local file</font><br>
<br>
<input type="submit" value="Upload Image">
</form>
</body>
</html>