This is my way to upload a photo, create a thumbnail and show it using
an IFRAME as if it was an AJAX event
------------------------------------------------------------------------------------------------------------------------
The view with the Upload Form and an Iframe to display the thumbnail -
> edit.thtml
------------------------------------------------------------------------------------------------------------------------
<form action="<?php echo $html->url('/users/uploadphoto'); ?>"
enctype="multipart/form-data" method="post" target="upload">
<table>
<tr><td>File:</td></tr>
<tr><td><?php echo $html->file('picFile'); ?></td></tr>
<tr><td align="center"><?php echo $html->submit('Upload'); ?></
td></tr>
<tr><td><iframe src="" id="upload" name="upload" width="200"
height="100" frameborder="0"></iframe></td></tr>
</table>
</form>
------------------------------------------------------------------------------------------------------------------------
The part of the controller to Upload the photo and create a thumbnail
of it
------------------------------------------------------------------------------------------------------------------------
function thumbnail($file,$dir,$ancho=100,$alto=100,$gif_support=false,
$filename)
{
if(!file_exists($file))
{
printf("THUMBNAIL::File Error\n");
return false;
}
if(!is_dir($dir))
{
printf("THUMBNAIL::Dir Error\n");
return false;
}
$mini = $dir.$filename;
$origen = imageCreateFromJPEG($file);
$imgAncho = imageSx($origen);
$imgAlto = imageSy($origen);
$imagen = imageCreateTrueColor($ancho,$alto);
ImageCopyResampled($imagen,$origen,0,0,0,0,$ancho,$alto,$imgAncho,
$imgAlto);
imageJPEG($imagen,$mini);
return true;
}
function uploadphoto()
{
$this->set('icono','no');
$this->set('error','no');
if (is_uploaded_file($this->params['form']['picFile']['tmp_name']))
{
$uploadDir = '/var/www/xxxx/xxxx/httpdocs/app/webroot/img/
photos/'; // Your server's route
srand((double)microtime()*1000000);
$random = rand(10000000,99999999); // To make a random filename
$temporalname = $random.'.jpg';
$uploadFile = $uploadDir.$temporalname;
if ($this->params['form']['picFile']['type'] == 'image/pjpeg' ||
$this->params['form']['picFile']['type'] == 'image/jpeg')
{
if (move_uploaded_file($this->params['form']['picFile']
['tmp_name'],$uploadFile))
{
if(chmod($uploadFile, 0755))
{
$this->thumbnail($uploadFile,$uploadDir,96,96,false,
$temporalname);
$this->set('icono',$temporalname);
$this->Session->Write('nuevaimagen',$temporalname);
}
}
}
else
{
$this->set('error','si');
}
}
}
------------------------------------------------------------------------------------------------------------------------
The View that display the thumbnail in the IFRAME or error if it
wasn't a JPG file
------------------------------------------------------------------------------------------------------------------------
<div align="center">
<?php if($error != 'no') { echo 'File must be JPG'; } ?>
<?php if($icono != 'no') { echo $html->image(photos/'.$icono,
array('border'=>'0','align'=>'absmiddle')); } ?>
</div>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---