Good image uploader with thumbnails?



I want to make a portfoliosystem where user can register and get their
own portfolio... I've started the developer work, but I'm stuck on the
image upload part... I'm experiencing some problems getting the picture
resized and thumbnailed... I'm on a apache server running php 5 with
8MB php_memory.

When uploading, the script works fine most times when uploading small
files (below 100kb and sometimes up close to 300kb too), but when
uploading files bigger than 300kb it only reloads the page, and the
form fields are empty.

Does any of you know of a smoothly working script for uploading,
resizing and thumbnailing? I need it to write a line to the database
too, but that's no problem writing... Excuse my bad english, but I'm
norwegian :)

If you're intrested, the code I have now is pasted below:

<?

ini_set("memory_limit","16M");

//Last inn global konfigurasjonsfil - denne filen må finnes for at
systemet skal laste
if(!file_exists("../global_config.php")){
echo("global_config.php finnes ikke");
die;
}
require("../global_config.php");

//Last inn globale funksjoner

if(!file_exists("../global_functions.php")){
echo("global_functions.php finnes ikke");
die;
}
require("../global_functions.php");

//Hent ut brukernavn

//Koble til databse

db_connect();


$dir = "temp/"; //Change this to the correct dir RELATIVE TO WHERE
THIS SCRIPT IS, or /full/path/

//MIME types to allow, Gif, jpeg, zip ::Edit this to your liking
$types =
array("image/png","image/x-png","image/gif","image/jpeg","image/pjpeg");


// Nothing to edit below here.

//Opprett thumbnail-funksjonen

function endre($name,$filename,$new_w,$new_h){
$system=explode('.',$name);
if (preg_match('/jpg|jpeg/',$system[1])){
$src_img=imagecreatefromjpeg($name);
}
if (preg_match('/png/',$system[1])){
$src_img=imagecreatefrompng($name);
}

$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) {
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) {
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y) {
$thumb_w=$new_w;
$thumb_h=$new_h;
}

$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);


if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}


//Check to determine if the submit button has been pressed
if(isset($_POST['submit'])) {

//Shorten Variables
$tmp_name = $_FILES['upload']['tmp_name'];
$new_name = $_FILES['upload']['name'];
$cat_id = $_POST['cat_id'];
$tittel = $_POST['tittel'];
$beskrivelse = $_POST['beskrivelse'];
$fullpath = "$dir$path/";
$temppath = "bilder/";
$fullpath = str_replace("..", "", str_replace("\.", "",
str_replace("//", "/", $fullpath)));
$clean_name = ereg_replace("[^a-z0-9._]", "", str_replace(" ", "_",
str_replace("%20", "_", strtolower($new_name) ) ) );

// lets see if we are uploading a file or doing a dir listing
//Sjekk MIME-typen
if ((in_array($_FILES['upload']['type'], $types)) and
(!file_exists($temppath.$clean_name))){

//Opprett mapper som er nødvendige
if (!is_dir($fullpath)){
mkdir("$fullpath", 0777);
}
if (!is_dir("thumb/")){
mkdir("thumb/", 0777);
}
if (!is_dir("temp/")){
mkdir("temp/", 0777);
}
//Flytt filen til /temp-mappen
move_uploaded_file($tmp_name,$fullpath . $clean_name);



endre("temp/$clean_name","thumb/thumb_$clean_name",125,125);
endre("temp/$clean_name","bilder/$clean_name",550,550);


//Slett originalfilen
$ourFileName = "temp/$clean_name";
$ourFileHandle = fopen($ourFileName, 'w') or die("kunne ikke
&aring;pne fil for sletting");
fclose($ourFileHandle);
unlink($ourFileName);


$query = "INSERT INTO `portfolio_brukere_doffer` (`id`, `cat_id`,
`url`, `url_full`, `tittel`, `beskrivelse`, `om`) VALUES ('',
'$cat_id', 'thumb/thumb_$clean_name', 'bilder/$clean_name', '$tittel',
'$beskrivelse', '')";
mysql_query($query) or die('Error, insert query failed');
echo "$clean_name ble lagt til";


}/*else{

//Print Error Message
echo "<small>Filen
<strong><em>{$_FILES['upload']['name']}</em></strong> finnes fra
f&oslash;r av</small><br />";
//Debug
$name = $_FILES['upload']['name'];
$type = $_FILES['upload']['type'];
$size = $_FILES['upload']['size'];
$tmp = $_FILES['upload']['name'];

echo "Navn: $name<br />Type: $type<br />St&oslash;rrelse: $size<br
/>Tmp: $tmp";

} */


} else {
echo 'Ready to upload your file';
} ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data">

<fieldset>
<legend>Last opp bilde</legend>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Bilde som skal lastes opp </td>
<td>
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<input type="file" name="upload" />&nbsp;</td>
</tr>
<tr>
<td>Tittel</td>
<td><input name="tittel" type="text" maxlength="100">&nbsp;</td>
</tr>
<tr>
<td>Kategori</td>
<td>
<select name="cat_id">
<option selected>Velg en kategori</option>
<option>- - -</option>
<?
$kategorier = "SELECT * FROM `portfolio_cat` LIMIT 0 , 100";
$cat_resultat = mysql_query($kategorier);
while ($row= @mysql_fetch_array($cat_resultat)) {
$cat_id = $row["id"]; //hent ut kategori-id
$cat_navn = $row["name"]; //hent ut kategori-navn

//Send informasjonen ut til leseren


echo("<option value=\"$cat_id\">$cat_navn</option>");


$count++ ;
}
?>
</select>
</td>
</tr>
<tr>
<td>Beskrivelse</td>
<td><textarea name="beskrivelse"></textarea></td>
</tr>
</table>

<input type="submit" name="submit" value="Last opp" />
</fieldset>
</form>

.


Quantcast