Tutoriale PHP
  Comunitatea PHP Romania

 
Tutoriale PHP
Exemple PHP
Categorii Exemple » Exemplu

upload and rezize multiple images


Descriere:
sunt 4 fisere care fac upload la poze si apoi rezize in tr-un dosar `thumv`. primul fiser stabileste numarul de poze care sa se upload-eze... al doilea e form-ul de upload, in functie de primul fisier, al treilea fisier face upload-ul iar al patrulea face resize... sper sa poata fi adaptat. Am realizat acest script in urma discutiilor de pe forum-ul www.phpromania.net/forum. Codul trebuie optimizat pt anumite tipuri de imagini!
Trimis de:Octav Octavian
Voturi:112
Accesari:824

Sintaxa:
index.php
<form name=\"form1\" method=\"post\" action=\"index1.php\">
  <input name=\"nr_fis\" type=\"text\" id=\"nr_fis\" size=\"5\">
  <input type=\"submit\" name=\"Submit\" value=\"Submit\">
</form>

index1.php
<?php
require_once("../../lib/conf.php\");
if($_SESSION[\'oct_logat\'] != \'da\')
print \"<script>location.href=\'index.php\'; </script>\";
$_SESSION[\'nr_fisiere\'] = $_POST[\'nr_fis\'];

?>
<html>
<head>
<title>Upload file(s)</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
<script language=\"javascript\" src=\"../../java/java.js\" type=\"text/javascript\"></script>
</head>

<body>
<form name=\"form1\" enctype=\"multipart/form-data\" method=\"post\" action=\"upload.php\">
  <p>
  <?php $co = 0; for($i = 1; $i <= $_SESSION[\'nr_fisiere\']; $i++) {
  
$sqlu[$i] = \"SELECT * FROM `oct_port`\";
$resu[$i] = mysql_query($sqlu[$i]);
$portu[$i] = mysql_fetch_assoc($resu[$i]);

  $co++;   ?>
  <p style=\"padding:5px;\"<?php if($co % 2 == 0) print \" style=\'background-color:#E9E9E9\';\"; ?>>

<select name=\"titlu1<?php print $i; ?>\" id=\"titlu1<?php print $i; ?>\"
onChange=\"if(this.value == \'\') enableOb(document.getElementById(\'titlu<?php print $i; ?>\')); else disableOb(document.getElementById(\'titlu<?php print $i; ?>\'))\">
<option value=\"\">Imagini</option>
    <option value=\"pers\">* personale</option>
    <option value=\"alte\">* altele</option>
<option selected value=\"\">ALTELE</option>
<option>--- portofoliu ---</option>
<?php
do {
    print \"<option value=\'\".$portu[$i][\'tip\'].\"|\".$portu[$i][\'id\'].\"\'>\".$portu[$i][\'tip\'].\" | \".$portu[$i][\'adresa\'].\"</option>\" ;
} while ($portu[$i] = mysql_fetch_assoc($resu[$i]));
?>
</select>    


<input name=\"titlu<?php print $i; ?>\" type=\"text\" id=\"titlu<?php print $i; ?>\" size=\"20\" >
    <input name=\"primar<?php print $i; ?>\" type=\"checkbox\" id=\"primar<?php print $i; ?>\" value=\"primar\">
    primara<br>
    <input name=\"upfile<?php print $i; ?>\" type=\"file\" id=\"upfile<?php print $i; ?>\" size=\"30\"><br>
    <textarea name=\"desc<?php print $i; ?>\" cols=\"30\" rows=\"4\" onFocus=\"if( this.value == \'descriere\') this.value=\'\'; \">descriere</textarea>
</p>
  <?php } ?>
</p>
  <p>
    <input name=\"trimit\" type=\"submit\" id=\"trimit\" value=\"trimite\"> 
  | <a href=\"javascript:history.back()\">inapoi</a></p>
</form>
</body>
</html>
<?php
for($s = 1; $s <= $_SESSION[\'nr_fisiere\']; $s++)
mysql_free_result($resu[$s]);
?>

upload.php
<?php
require_once(\"../../lib/conf.php\");
require_once(\"../../lib/data.php\");
if($_SESSION[\'oct_logat\'] != \'da\')
print \"<script>location.href=\'index.php\'; </script>\";

$uploaddir = \'upload/\';
for($k = 1; $k<=$_SESSION[\'nr_fisiere\']; $k++) {
    $nume[$k] = basename($_FILES[\'upfile\'.$k][\'name\']);
    $extensie[$k] = substr($nume[$k], -3);
    $_SESSION[\'nume_f\'][$k] = md5(date(\"H:i:s\")) . rand(0,500) . $k . \".\" . $extensie[$k];
    $uploadfile[$k] = $uploaddir . $_SESSION[\'nume_f\'][$k];
    $_SESSION[\'fis_upl\'][$k] = $uploadfile[$k];
}

for($j = 1; $j<=$_SESSION[\'nr_fisiere\']; $j++) {
    if($_POST[\'titlu1\'.$j] == \'\') {
        $lac[$j] = $_POST[\'titlu\'.$j];
        $idu[$j] = 0;
    } else if($_POST[\'titlu1\'.$j] == \'pers\' || $_POST[\'titlu1\'.$j] == \'alte\') {
        $lac[$j] = $_POST[\'titlu1\'.$j];
        $idu[$j] = 0;
    } else {
        $arraiu[$j] = explode(\"|\", $_POST[\'titlu1\'.$j]);
        $lac[$j] = $arraiu[$j][0];
        $idu[$j] = $arraiu[$j][1];
    }
    if($nume[$j] != \'\') {
    $sql[$j] = \"INSERT INTO `oct_files`
    ( `idu`, `img` , `ce_e` , `data` , `tip` , `dimensiune` , `alt` ) 
    VALUES (\".$idu[$j].\", \'\".$_SESSION[\'nume_f\'][$j].\"\', \'\".$lac[$j].\"\', \'\".$data_r.\"\', \'\".$_POST[\'primar\'.$j].\"\', \'\".$_FILES[\'upfile\'.$j][\'size\'].\"\', \'\".$_POST[\'desc\'.$j].\"\'
    )\";
    if(mysql_query($sql[$j])) {
        if (move_uploaded_file($_FILES[\'upfile\'.$j][\'tmp_name\'], $uploadfile[$j]))
            echo \"<script>location.href=\'upload1.php\'</script>\";
        else echo \"<br>Nope !\";
    } else {
        die(mysql_query());
    }
    }
}

?>

upload1.php
<?php 
require_once(\"../../lib/conf.php\");
if($_SESSION[\'oct_logat\'] != \'da\')
print \"<script>location.href=\'index.php\'; </script>\";

error_reporting(E_ALL);
header(\'Content-type: image/jpeg\');

for($k = 1; $k<=$_SESSION[\'nr_fisiere\']; $k++) {

$filename[$k] = $_SESSION[\'fis_upl\'][$k];
a
list($width[$k], $height[$k]) = getimagesize($filename[$k]); 

if($width[$k] > $height[$k]) { 
    $newwidth[$k] = \"125px\"; 
    $newheight[$k] = \"93px\"; 
} else if($width[$k] < $height[$k]) { 
    $newwidth[$k] = \"69px\"; 
    $newheight[$k] = \"93px\"; 
} else if($width[$k] == $height[$k]) { 
    $newwidth[$k] = \"93px\";
    $newheight[$k] = \"93px\"; 


$thumb[$k] =  imagecreatetruecolor($newwidth[$k], $newheight[$k]) or die(); 
$exn[$k] = substr($filename[$k], -3);

switch($exn[$k]) { 
    case \"jpg\": 
        $source[$k] = imagecreatefromjpeg($filename[$k]) or die(); 
    break;
    case \"gif\": 
        $source[$k] = imagecreatefromgif($filename[$k]) or die(); 
    break; 
    case \"png\": 
        $source[$k] = imagecreatefrompng($filename[$k]) or die(); 
    break; 
}

imagecopyresampled($thumb[$k], $source[$k], 0, 0, 0, 0, $newwidth[$k], $newheight[$k], $width[$k], $height[$k]) or die(); 
if(imagejpeg($thumb[$k], \'upload/thumv/\'.$_SESSION[\'nume_f\'][$k], 100)) print \"<script>location.href=\'index.php\'; </script>\"; 
}
?>

Ultimele discutii in forum RSS Forum

Ultimele articole Ultimele articole

Top membri

Pirahna Pirahna
la birou
carco carco
Bucuresti
Birkoff Birkoff
Bucuresti
mihaitha mihaitha
Sibiu
Mascka Mascka
Braila
gabysolomon gabysolomon
Bacau
whooper whooper
Toronto ON
raul_ raul_
dechim dechim
Drobeta Turnu Severin
Amenthes Amenthes

Newsletter


Email:
 inscriere
 renuntare
 
 Arhiva newsletter

Site-ul lunii

blg
 Blgpunctro

Propune un site

Online

206 utilizatori online

Parteneriat



Copyright © 2001-2008 PHP Romania Add PHPRomania to Google Add PHPRomania to Del.icio.us Add PHPRomania to Stumbleupon Add PHPRomania to Yahoo! Add PHPRomania to Digg Add PHPRomania to Blink Remortgage | Auto Loans | Problem Mortgage | Broadband | Remortgaging
Ads: Partener Way2Web Nework: gazduire web | inregistrare domenii | web design | imobiliare | web hosting
Powered by Simplis