Reszta działa, to znaczy wczutuje pliki, zmienia ich rozdzielczość i tworzy miniaturkę.
plik upload
if(isset($_FILES['upload']) === true) { for($i=0; $i<count($_FILES['upload']['name']); $i++) { if(is_file($_FILES['upload']['tmp_name'][$i])) { define('DESTINATION', '../pliki/'); define('RESIZEBY', 'w'); define('RESIZETO', $liczbaszer); define('RESIZETOM', 300); define('QUALITY', 90); require_once 'image.class.php'; $image = new Image($_FILES['upload']['tmp_name'][$i]); $plik=$_FILES['upload']['name'][$i]; $ext = getExtension($plik); $image->destination = DESTINATION.$ostid.".".$ext; $image->destinationm = DESTINATION.$ostid."m.".$ext; $image->constraint = RESIZEBY; $image->size = RESIZETO; $image->sizem = RESIZETOM; $image->quality = QUALITY; $plik = $image->render($ext); $plik = $ostid.".".$ext; $ostid++; } } echo 'Pliki graficzne zostały załadowane na serwer.'; } ?> <form action="upload.php" method="post" accept-charset="utf-8" enctype="multipart/form-data"> <p>Wybierz grafiki:<input type="file" name="upload[]" multiple="true" id="idupload" onChange="listing();"/><p> <p>Szerokość:<input type="text" name="width" value=""/></p> <iframe id="progress_iframe" src="" style="display:none;" scrolling="no" frameborder="0"></iframe> <? $upload_id="1234"; ?> <input type="hidden" name="UPLOAD_IDENTIFIER" id="uid" value="1234"> <p><input type="submit" value="Upload" onClick="function set() { f=document.getElementById('progress_iframe'); f.style.display='block'; f.src='uploadprogress.php?id=<?=$upload_id?>';} setTimeout(set);" /></p> </form> <ul id="lista"><li></li></ul> <!--lista plików do pobrania--> <script type="text/javascript"> function listing() { var input = document.getElementById("idupload"); var ul = document.getElementById("lista"); while (ul.hasChildNodes()) { ul.removeChild(ul.firstChild); } for (var i = 0; i < input.files.length; i++) { var li = document.createElement("li"); li.innerHTML = input.files[i].name; ul.appendChild(li); } if(!ul.hasChildNodes()) { var li = document.createElement("li"); li.innerHTML = 'Nie wybrano żadnych plików'; ul.appendChild(li); } } </script>
plik image.class.php:
class Image { var $max_file_size = 2097152; #10MB var $errors; public $destination = ''; public $constraint = ''; public $size = ''; public $quality = ''; public $cropwidth = false; public $cropheight = false; public $align; public function __construct($imagefilepath) { $this->tmp_name = $imagefilepath; $this->width = $imagedata[0]; // szerokosc obrazka wczytanego $this->height = $imagedata[1]; $this->type = $imagedata['mime']; $this->is_valid = $this->is_valid_type(); $this->is_valid = $this->is_valid_size(); } public function is_valid_type() { { return true; } else { return false; } } public function is_valid_size() { if ($this->image_size['size'] <= $this->max_file_size) { return true; } else { return false; } } public function render($ext) { $destname = $this->destination; $destnamem = $this->destinationm; $constraint = $this->constraint; $new_side = $this->size; // wymiary $new_sidem = $this->sizem; // wymiary miniatórki $cropw = $this->cropwidth; $croph = $this->cropheight; $quality = $this->quality; // echo"DESTNAME: $destname, DRSTNAMEM: $destnamem<br />"; if ($constraint == "h") { $new_w = ($new_side / $this->height) * $this->width; $new_wm = ($new_sidem / $this->height) * $this->width; // M $new_h = $new_side; $new_hm = $new_sidem; // M $canvasw = ($cropw!=false) ? $cropw : $new_w; $canvash = ($croph!=false) ? $croph : $new_h; $canvas = imagecreatetruecolor($canvasw, $canvash); // echo"tworze CANVAS H...."; $canvaswm = ($cropw!=false) ? $cropw : $new_wm; // M $canvashm = ($croph!=false) ? $croph : $new_hm; // M $canvasm = imagecreatetruecolor($canvaswm, $canvashm); // M $x = ($cropw==false) ? 0 : -(($new_w - $cropw)/2); $y = ($croph==false) ? 0 : -(($new_h - $croph)/2); $xm = ($cropw==false) ? 0 : -(($new_wm - $cropw)/2); // M $ym = ($croph==false) ? 0 : -(($new_hm - $croph)/2); // M } else if ($constraint == "w") { $new_h = ($new_side / $this->width) * $this->height; $new_w = $new_side; $new_hm = ($new_sidem / $this->width) * $this->height; // M $new_wm = $new_sidem; //M $canvasw = ($cropw!=false) ? $cropw : $new_w; $canvash = ($croph!=false) ? $croph : $new_h; $canvas = imagecreatetruecolor($canvasw, $canvash); // echo"tworze CANVAS W...."; $canvaswm = ($cropw!=false) ? $cropw : $new_wm; // M $canvashm = ($croph!=false) ? $croph : $new_hm; //M $canvasm = imagecreatetruecolor($canvaswm, $canvashm); //M $x = ($cropw==false) ? 0 : -(($new_w - $cropw)/2); $y = ($croph==false) ? 0 : -(($new_h - $croph)/2); $xm = ($cropw==false) ? 0 : -(($new_wm - $cropw)/2); $ym = ($croph==false) ? 0 : -(($new_hm - $croph)/2); } else if ($constraint == "t") { if($this->height > $this->width) { $new_h = ($new_side / $this->width) * $this->height; $new_w = $new_side; $new_hm = ($new_sidem / $this->width) * $this->height; //M $new_wm = $new_sidem; // M $x = 0; $y = -(($new_h-$new_side)/2); $xm = 0; //M $ym = -(($new_hm-$new_sidem)/2); //M } else if($this->height <= $this->width) { $new_w = ($new_side / $this->height) * $this->width; $new_h = $new_side; $new_wm = ($new_sidem / $this->height) * $this->width; // M $new_hm = $new_sidem; // M $x = -(($new_w-$new_side)/2); $y = 0; $xm = -(($new_wm-$new_sidem)/2); //M $ym = 0; //M } $canvas = imagecreatetruecolor($new_side, $new_side); $canvasm = imagecreatetruecolor($new_sidem, $new_sidem); //M // echo"tworze CANVAS T...."; } else { $new_h = $this->height; $new_w = $this->width; $new_hm = $this->height; // M $new_wm = $this->width; //M $x = $y = 0; $xm = $ym = 0; //M $canvas = imagecreatetruecolor($new_w, $new_h); $canvasm = imagecreatetruecolor($new_wm, $new_hm);//M // echo"tworze CANVAS T ELSE...."; } switch($this->type) { case 'image/jpeg': $ic = imagecreatefromjpeg($this->tmp_name); break; case 'image/gif': $ic = imagecreatefromgif($this->tmp_name); break; case 'image/png': $ic = imagecreatefrompng($this->tmp_name); break; } $test2=imagecopyResampled($canvasm, $ic, $x, $y, 0, 0, $new_wm, $new_hm, $this->width, $this->height); //M $test1=imagecopyResampled($canvas, $ic, $x, $y, 0, 0, $new_w, $new_h, $this->width, $this->height); //if($test1==TRUE) $test1="TRUE"; else $test1="FALSE"; //if($test2==TRUE) $test2="TRUE"; else $test2="FALSE"; //echo"TEST1: $test1, TEST2(miniat)= $test2<br />"; if ($ext=='jpg' || $ext=='jpeg'){ imagejpeg($canvasm, $destnamem, $quality); //M imagejpeg($canvas, $destname, $quality); } if ($ext=='gif'){ imagegif($canvas, $destname); imagegif($canvasm, $destnamem); //M } if ($ext=='png'){ imagegif($canvas, $destname, $quality); imagegif($canvasm, $destnamem, $quality); //M } imagedestroy($canvas); imagedestroy($canvasm); return $destname; } public function done() { } }
uploadprogress.php
<html> <head> <META HTTP-EQUIV='REFRESH' CONTENT='1;URL='> </head> <body> Upload progress:<br /> <?php if(!$_GET['uid']) die; $info = uploadprogress_get_info($_GET['uid']); $kbytes_total = round($info['bytes_total'] / 1024); $kbytes_uploaded = round($info['bytes_uploaded'] / 1024); echo $kbytes_uploaded.'/'.$kbytes_total.' KB'; ?> </body> </html>