04. januar 2006 - 23:09
Der er
4 kommentarer og
1 løsning
upload billede til hjemmeside
Er her nogle som ved hvordan man uploader et billede til en hjemmeside. Det ville være super cool, hvis at billedet bliver komprimeret under uploadningen, fx til en form for thumpnail, eller på anden vis bliver mindre. Bare så folk ikke behøver at komprimere billedet før de uploader.
Håber at her er nogle experter som har nogle forslag til hvordan jeg løser dette problem;)
05. januar 2006 - 11:56
#2
Prøv at se nedenstående. Det er fra den anviste side.. er der ikke nogle fejl med fx: < , eller skal det se sådan ud?
<HTML>
<form enctype="multipart/form-data" method="post" action="update.php3" name="form1">
<input type="hidden" name="MAX_FILE_SIZE" value="102400">
<input type="file" name="photo" accept="image/jpeg">
<input type="hidden" name="id" value="12345">
<input type=submit value="Send">
</form>
</HTML>
05. januar 2006 - 12:08
#3
Det har jeg nu ændret til nedenstående. Det må være sådan det skal se ud. Men hvordan sætter jeg det ind på en side. Det vil ikke blive vist. Jeg bruger selvfølgelig en php side.
<?HTML>
<?form enctype="multipart/form-data" method="post" action="update.php3" name="form1"?>
<?input type="hidden" name="MAX_FILE_SIZE" value="102400"?>
<?input type="file" name="photo" accept="image/jpeg"?>
<?input type="hidden" name="id" value="12345"?>
<?input type=submit value="Send"?>
<?/form?>
<?/HTML?>";
05. januar 2006 - 12:12
#4
hvordan skal nedenstående sættes ind i filer, og hvordan skal opdelingen være? Jeg tror umiddelbart at det er rimelig nemt, bare ikke for mig;)
Photo upload, Browser
Standard HTML support file upload.
<HTML>
<form enctype="multipart/form-data" method="post" action="update.php3" name="form1">
<input type="hidden" name="MAX_FILE_SIZE" value="102400">
<input type="file" name="photo" accept="image/jpeg">
<input type="hidden" name="id" value="12345">
<input type=submit value="Send">
</form>
</HTML>
Photo upload, validation at server
The browser's validation is far from perfect. So, the PHP script at server side must recheck the input.
<?php
$sPhotoFileName = $_FILES['photo']['name']; // get client side file name
if ($sPhotoFileName) // file uploaded
{ $aFileNameParts = explode(".", $sPhotoFileName);
$sFileExtension = end($aFileNameParts); // part behind last dot
if ($sFileExtension != "jpg"
&& $sFileExtension != "JPEG"
&& $sFileExtension != "JPG")
{ die ("Choose a JPG for the photo");
}
$nPhotoSize = $_FILES['photo']['size']; // size of uploaded file
if ($nPhotoSize == 0)
{ die ("Sorry. The upload of $sPhotoFileName has failed.
Search a photo smaller than 100K, using the button.");
}
if ($nPhotoSize > 102400)
{ die ("Sorry.
The file $sPhotoFileName is larger than 100K.
Advice: reduce the photo using a drawing tool.");
}
// read photo
$sTempFileName = $_FILES['photo']['tmp_name']; // temporary file at server side
$oTempFile = fopen($sTempFileName, "r");
$sBinaryPhoto = fread($oTempFile, fileSize($sTempFileName));
// Try to read image
$nOldErrorReporting = error_reporting(E_ALL & ~(E_WARNING)); // ingore warnings
$oSourceImage = imagecreatefromstring($sBinaryPhoto); // try to create image
error_reporting($nOldErrorReporting);
if (!$oSourceImage) // error, image is not a valid jpg
{ die ("Sorry.
It was not possible to read photo $sPhotoFileName.
Choose another photo in JPG format.");
}
}
?>
Create thumbnail
<?php
$nWidth = imagesx($oSourceImage); // get original source image width
$nHeight = imagesy($oSourceImage); // and height
// create small thumbnail
$nDestinationWidth = 80;
$nDestinationHeight = 60;
//$oDestinationImage = imagecreatetruecolor($nDestinationWidth, $nDestinationHeight);
$oDestinationImage = imagecreate($nDestinationWidth, $nDestinationHeight);
/*$oResult = imagecopyresampled(
$oDestinationImage, $oSourceImage,
0, 0, 0, 0,
$nDestinationWidth, $nDestinationHeight,
$nWidth, $nHeight); // resize the image
*/
imagecopyresized(
$oDestinationImage, $oSourceImage,
0, 0, 0, 0,
$nDestinationWidth, $nDestinationHeight,
$nWidth, $nHeight); // resize the image
ob_start(); // Start capturing stdout.
imageJPEG($oDestinationImage); // As though output to browser.
$sBinaryThumbnail = ob_get_contents(); // the raw jpeg image data.
ob_end_clean(); // Dump the stdout so it does not screw other output.
?>
Store Thumbnail in database
<?php
$sBinaryThumbnail = addslashes($sBinaryThumbnail);
$oDatabase = mysql_connect("localhost", "your_database_user_id", "your_database_password");
mysql_select_db("your_database_name", $oDatabase);
$sQuery = "UPDATE your_table
SET thumbnail = '$sBinaryThumbnail',
WHERE id = '$nId'";
mysql_query($sQuery, $oDatabase);
?>
Show Thumbnail
Thumbnail:
<HTML>
<img src="thumbnail.php?id=12345" width=80 height=60>
</HTML>
The PHP thumbnail script pretends to be a JPG file, sends an image to the browser.
<?php
header("Content-type: image/jpeg"); // act as a jpg file to browser
$oDatabase = mysql_connect("localhost", "your_database_user_id", "your_database_password");
mysql_select_db("your_database_name", $oDatabase);
$sQuery = "SELECT thumbnail
FROM your_table
WHERE id = $nId";
$oResult = mysql_query($sQuery, $oDatabase);
$oRow = mysql_fetch_array($oResult);
$sJpg = $oRow["thumbnail"];
echo $sJpg;
?>