Forum Moderators: coopster
<?php
/*************************************************
<snip>*************************************************
?>
<html>
<head>
<title>Asynchronous image file upload without AJAX</title>
<style>
iframe {border-width: 0px;height: 600px;width: 400px;}
iframe.hidden {visibility: hidden;width:0px;height:0px;}
#main1 {overflow: hidden;margin: auto;width: 90%;height: 420px;border-style: solid;border-width: 1px;background-color: white;}
#images1 {width: 95%;height: auto;margin: 20px;}
#images1 div {margin: 10px;width: 130px;height: 130px;border-style: solid;border-width: 2px;border-color: #DEDFDE;float: left;overflow: hidden;}
#images1 div:hover {border-color: #D8F18C;}
#images1 img.load { margin-top:20px;}
.blacklink:link, .blacklink:alink, .blacklink:vlink, .blacklink:hover{color:#000000}
a.redlink{color: #96001C; text-decoration: none}
a:hover.redlink{color: #96001C; text-decoration: underline}
</style>
</head>
<body>
<table width="600" align="center" cellpadding="0" cellspacing="0" border="0">
<Tr><td><h1>PHPAsks.com</h1></td></Tr>
<Tr>
<td align="center" valign="middle">
<div id="main1">
<div id="iframe">
<iframe src="upload.php" frameborder="0"></iframe>
</div>
<div id="images1">
</div>
</div>
<div id="myDivLink"></div>
</td>
</Tr>
<tr>
<Td align="center">
Power by <a href="http://www.example.com" class="redlink">Example.com</a>
</Td>
</tr>
</table>
</body>
</html>
<?php
/**************************************
<snip>***************************************
if($_GET['pic']){
$img = new img('upload/'.$_GET['pic']);
$width=$_GET["width"];
$height=$_GET["height"];
$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);
}
}
?>
<?php
/**************************
<snip>***************************
sleep(2);
$ftmp = $_FILES['image']['tmp_name'];
$oname = $_FILES['image']['name'];
$extenssion = strstr($oname, ".");
$fname = 'upload/'.$_FILES['image']['name'];
if(move_uploaded_file($ftmp, $fname)){
?>
<html><head><script>
var par = window.parent.document;
var images = par.getElementById('images1');
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);
var bre=par.createElement('br');
imgdiv.appendChild(bre);
var myLink1 = par.createElement('a');
myLink1.href = "#";
myLink1.className = "blacklink";
myLink1.onclick=function(e){alert('x'+' Y: '+'usrname');}
myLink1.appendChild(par.createTextNode('Remove'));
imgdiv.appendChild(myLink1);
var oNewP = par.createElement('span');
var oText = par.createTextNode(" ");
oNewP.appendChild(oText);
imgdiv.appendChild(oNewP);
</script></head>
</html>
<?php
exit();
}
?><html><head>
<script language="javascript">
function upload(){
// hide old iframe
var par = window.parent.document;
var num = par.getElementsByTagName('iframe').length - 1;
var iframe = par.getElementsByTagName('iframe')[num];
iframe.className = 'hidden';
// create new iframe
var new_iframe = par.createElement('iframe');
new_iframe.src = 'upload.php';
new_iframe.frameBorder = '0';
par.getElementById('iframe').appendChild(new_iframe);
// add image progress
var images = par.getElementById('images1');
var new_div = par.createElement('div');
var new_img = par.createElement('img');
new_img.src = '/images/indicator.gif';
new_img.className = 'load';
new_div.appendChild(new_img);
images.appendChild(new_div);
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()" />
<input type="hidden" name="imgnum" />
</form>
</center></html>
[edited by: jatar_k at 2:29 pm (utc) on June 11, 2009]
[edit reason] Removed specifics [/edit]
you didn't look at my questions, the reason i asked about the form element name is
$_FILES['image']['tmp_name'];
the above is returning undefined, that could mean you aren't pulling from the right var name, a guess anyway, an answer to that question would help with what the next question should be
>> can i give you the files to see?
if we keep it in the thread then you can do the leg work and more members can see the thread to contribute.