<div id="input_file"> <div id="iframe_container"> /*aqui estou passando por parâmetro logo*/ <iframe src="upload.php?imagem=logo" frameborder="0"></iframe> </div> <div id="images_containerlogo"> Logo </div> </div>
upload.php
<?php error_reporting(E_ALL); if (isset($_FILES['image'])) { $ftmp = $_FILES['image']['tmp_name']; $oname = $_FILES['image']['name']; $fname = "images/".$_FILES['image']['name']; if(move_uploaded_file($ftmp, $fname)) { ?> <html><head> <script> var par = window.parent.document; /*aqui pego por get o parametro*/var images = par.getElementById('images_container<?$_GET['imagem'];?>'); var imgdiv = images.getElementsByTagName('div')[<?=(int)$_POST['imgnum']?>]; var image = imgdiv.getElementsByTagName('img')[0]; imgdiv.removeChild(image); var image_new = par.createElement('img'); image_new.src = 'resize.php?pic=<?=$oname?>'; image_new.className = 'loaded'; imgdiv.appendChild(image_new); </script> </head></html> <?php exit(); } } ?> <html><head> <script language="javascript"> function upload() { var par = window.parent.document; // hide old iframe var iframes = par.getElementsByTagName('iframe'); var iframe = iframes[iframes.length - 1]; iframe.className = 'hidden'; // create new iframe var new_iframe = par.createElement('iframe'); new_iframe.src = 'upload.php'; new_iframe.frameBorder = '0'; par.getElementById('iframe_container').appendChild(new_iframe); // add image progress /*pego o parametro novamente*/var images = par.getElementById('images_container<?$_GET['imagem'];?>'); var new_div = par.createElement('div'); var new_img = par.createElement('img'); new_img.src = 'indicator.gif'; new_img.className = 'load'; new_div.appendChild(new_img); images.appendChild(new_div); // send var imgnum = images.getElementsByTagName('div').length - 1; document.iform.imgnum.value = imgnum; setTimeout(document.iform.submit(),5000); } </script> <style> #file { width: 350px; } </style> <head><body><center> <form name="iform" action="" method="post" enctype="multipart/form-data"> <input id="file" type="file" name="image" onchange="upload<?$_GET['imagem'];?>()" /> /*chamo a pagina com parametro*/ <input type="hidden" name="imgnum" /> </form> </center></html>
resize.php
<?php if($_GET['pic']) { $img = new img('images/'.$_GET['pic']); $img->resize(); $img->show(); } class img { var $image = ''; var $temp = ''; function img($sourceFile) { if(file_exists($sourceFile)) { $this->image = ImageCreateFromJPEG($sourceFile); } else { $this->errorHandler(); } return; } function resize($width = 100, $height = 100, $aspectradio = true) { $o_wd = imagesx($this->image); $o_ht = imagesy($this->image); if(isset($aspectradio)&&$aspectradio) { $w = round($o_wd * $height / $o_ht); $h = round($o_ht * $width / $o_wd); if(($height-$h)<($width-$w)) { $width =& $w; } else { $height =& $h; } } $this->temp = imageCreateTrueColor($width,$height); imageCopyResampled($this->temp, $this->image, 0, 0, 0, 0, $width, $height, $o_wd, $o_ht); $this->sync(); return; } function sync() { $this->image =& $this->temp; unset($this->temp); $this->temp = ''; return; } function show() { $this->_sendHeader(); ImageJPEG($this->image); return; } function _sendHeader() { header('Content-Type: image/jpeg'); } function errorHandler() { echo "error"; exit(); } function store($file) { ImageJPEG($this->image,$file); return; } function watermark($pngImage, $left = 0, $top = 0) { ImageAlphaBlending($this->image, true); $layer = ImageCreateFromPNG($pngImage); $logoW = ImageSX($layer); $logoH = ImageSY($layer); ImageCopy($this->image, $layer, $left, $top, 0, 0, $logoW, $logoH); } } ?> Tudo funcionando blz, mas nao consigo fazer algumas modificações para melhor ajuste na página: Quero colocar um disabled=true no onchange do botão para q nao permitisse mais de um upload, mas nao funciona pois ele chama a função upload() tb.. Ou se alguém souber outra forma para nao permitir mais de um upload agradeço..
Apenas comentei a linha q chama a upload.php q fica dentro da função uploar() e funcionou, dessa forma apenas permite um upload.
//new_iframe.src = 'upload.php';
Estou tendo agora o seguinte erro d js:
É nulo ou não é um objeto.
Q acontece quando passo src="upload.php?imagem=logo" no iframe nao reconhece o parametro imagem q estou passando.
Dai dei um alert em par.getElementById('images_container<?$_GET['imagem'];?>') e aparece null, no caso nao esta recebendo o valor do parametro.. como posso fazer isto?
Tentei assim e nao deu certo tb var images = par.getElementById('images_container' + <?$_GET['imagem'];?>);
Edição feita por: Josy, 10/08/2007, 10:36.