Good image uploader with thumbnails?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • doffer

    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 konfigurasjonsf il - denne filen må finnes for at
    systemet skal laste
    if(!file_exists ("../global_config.p hp")){
    echo("global_co nfig.php finnes ikke");
    die;
    }
    require("../global_config.p hp");

    //Last inn globale funksjoner

    if(!file_exists ("../global_function s.php")){
    echo("global_fu nctions.php finnes ikke");
    die;
    }
    require("../global_function s.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,$fi lename,$new_w,$ new_h){
    $system=explode ('.',$name);
    if (preg_match('/jpg|jpeg/',$system[1])){
    $src_img=imagec reatefromjpeg($ name);
    }
    if (preg_match('/png/',$system[1])){
    $src_img=imagec reatefrompng($n ame);
    }

    $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=ImageC reateTrueColor( $thumb_w,$thumb _h);
    imagecopyresamp led($dst_img,$s rc_img,0,0,0,0, $thumb_w,$thumb _h,$old_x,$old_ y);


    if (preg_match("/png/",$system[1]))
    {
    imagepng($dst_i mg,$filename);
    } else {
    imagejpeg($dst_ img,$filename);
    }
    imagedestroy($d st_img);
    imagedestroy($s rc_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("%2 0", "_", strtolower($new _name) ) ) );

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

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



    endre("temp/$clean_name","t humb/thumb_$clean_na me",125,125);
    endre("temp/$clean_name","b ilder/$clean_name",55 0,550);


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


    $query = "INSERT INTO `portfolio_bruk ere_doffer` (`id`, `cat_id`,
    `url`, `url_full`, `tittel`, `beskrivelse`, `om`) VALUES ('',
    '$cat_id', 'thumb/thumb_$clean_na me', 'bilder/$clean_name', '$tittel',
    '$beskrivelse', '')";
    mysql_query($qu ery) or die('Error, insert query failed');
    echo "$clean_nam e ble lagt til";


    }/*else{

    //Print Error Message
    echo "<small>Fil en
    <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;rrel se: $size<br
    />Tmp: $tmp";

    } */


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

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
    enctype="multip art/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($ka tegorier);
    while ($row= @mysql_fetch_ar ray($cat_result at)) {
    $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="beskrivel se"></textarea></td>
    </tr>
    </table>

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

Working...