Fixes on ImageField and FileForm

This commit is contained in:
Antonio de la Rosa 2015-09-09 22:13:55 +02:00
parent f5d9fbd308
commit fa2fcf359b
2 changed files with 123 additions and 394 deletions

View file

@ -50,55 +50,40 @@ class ImageField extends PhangoField {
//Check if the image is correct..
function check($image)
function check($fake_image)
{
//Only accept jpeg, gif y png
if(isset($_FILES[$this->name_component]['tmp_name']))
{
//Rewrite old_image
$name_image=$_FILES[$this->name_file]['name'];
$manager = new ImageManager(array('driver' => $this->driver));
if($image=$manager->make($_FILES[$this->name_file]['tmp_name'])!=false)
{
//$with=
//if(make('foo.jpg')->resize(300, 200)->save('bar.jpg');
if($this->thumb)
{
$base_name_image=basename($name_image);
foreach($this->img_width as $prefix => $width)
{
$image->reset();
//In nexts versions, save in tmp and move with ftp copy.
if(!$image->fit($width)->encode('jpg', $this->quality_jpeg)->save($this->path.'/'.$prefix.'_'.$base_name_image.'.jpg'))
{
$this->std_error=I18n::lang('common', 'cannot_save_images', 'Cannot save images, please, check permissions');
}
}
}
$file_name=$this->name_component.'_file';
$old_image='';
if($this->update)
{
//Check the image for delete.
$query=$model[$this->name_model]->select(array($this->name_component), 1);
//This field is used only for a row
$old_image=Webmodel::$model[$this->name_model]->select_a_row_where(array($this->name_component), 1)[$this->name_component];
while(list($old_image)=$model[$this->name_model]->fetch_row($query))
}
if(isset($_FILES[$file_name]['tmp_name']))
{
if(trim($_FILES[$file_name]['tmp_name'])!=='')
{
$name_image=$_FILES[$file_name]['name'];
$manager = new ImageManager(array('driver' => $this->driver));
if( ($image=$manager->make($_FILES[$file_name]['tmp_name']))!=false)
{
if($old_image!='')
{
if(!@unlink($this->path.'/'.$old_image))
@ -117,6 +102,37 @@ class ImageField extends PhangoField {
}
}
}
$image->backup();
//$with=
//if(make('foo.jpg')->resize(300, 200)->save('bar.jpg');
if($this->thumb)
{
$base_name_image=basename($name_image);
$file_extension=pathinfo($base_name_image, PATHINFO_EXTENSION);
$base_name_image=str_replace('.'.$file_extension, '', $base_name_image);
foreach($this->img_width as $prefix => $width)
{
$image->reset();
//In nexts versions, save in tmp and move with ftp copy.
if(!$image->fit($width)->encode('jpg', $this->quality_jpeg)->save($this->path.'/'.$prefix.'_'.$base_name_image.'.jpg'))
{
$this->std_error=I18n::lang('common', 'cannot_save_images', 'Cannot save images. Please, check permissions');
}
}
@ -126,16 +142,16 @@ class ImageField extends PhangoField {
$image->reset();
if($image->save($this->path.'/'.$prefix.'_'.$image_name))
if(!$image->save($this->path.'/'.$name_image))
{
$this->std_error=I18n::lang('common', 'cannot_save_images', 'Cannot save images, please, check permissions');
return false;
return '';
}
return $image_name;
return $name_image;
}
else
@ -143,324 +159,21 @@ class ImageField extends PhangoField {
$this->std_error=I18n::lang('common', 'no_valid_image', 'This image is wrong');
return false;
return '';
}
}
else
{
return $old_image;
}
}
$this->std_error=I18n::lang('common', 'no_image_found', 'No image uploaded, check enctype form');
return false;
/*
$file=$this->name_file;
$image=basename($image);
settype($_POST['delete_'.$file], 'integer');
if($_POST['delete_'.$file]==1)
{
//Delete old_image
$image_file=Utils::form_text($_POST[$file]);
if($image_file!='')
{
@unlink($this->path.'/'.$image_file);
foreach($this->img_width as $key => $value)
{
@unlink($this->path.'/'.$key.'_'.$image_file);
}
$image='';
}
}
if(isset($_FILES[$file]['tmp_name']))
{
if($_FILES[$file]['tmp_name']!='')
{
$arr_image=getimagesize($_FILES[$file]['tmp_name']);
$_FILES[$file]['name']=Utils::slugify(Utils::form_text($_FILES[$file]['name']));
if($this->prefix_id==1)
{
$func_token=$this->func_token;
$_FILES[$file]['name']=$func_token().'_'.$_FILES[$file]['name'];
}
$this->value=$_FILES[$file]['name'];
//Check size
if($this->min_size[0]>0 && $this->min_size[1]>0)
{
if($arr_image[0]<$this->min_size[0] || $arr_image[1]<$this->min_size[1])
{
$this->std_error=I18n::lang('common', 'image_size_is_not_correct', 'Image size is wrong').'<br />'.I18n::lang('common', 'min_size', 'Minimal size').': '.$this->min_size[0].'x'.$this->min_size[1];
$this->value='';
return '';
}
}
//Delete other image if exists..
if($image!='')
{
unlink($this->path.'/'.$image);
}
//gif 1
//jpg 2
//png 3
//Only gifs y pngs...
//Need checking gd support...
$func_image[1]='imagecreatefromgif';
$func_image[2]='imagecreatefromjpeg';
$func_image[3]='imagecreatefrompng';
if($arr_image[2]==1 || $arr_image[2]==2 || $arr_image[2]==3)
{
$image_func_create='imagejpeg';
switch($arr_image[2])
{
case 1:
//$_FILES[$file]['name']=str_replace('.gif', '.jpg', $_FILES[$file]['name']);
$image_func_create='imagegif';
break;
case 3:
//$_FILES[$file]['name']=str_replace('.png', '.jpg', $_FILES[$file]['name']);
$image_func_create='imagepng';
//Make conversion to png scale
$this->quality_jpeg=floor($this->quality_jpeg/10);
if($this->quality_jpeg>9)
{
$this->quality_jpeg=9;
}
break;
}
$move_file_func=$this->move_file_func;
if( $move_file_func ( $_FILES[$file]['tmp_name'] , $this->path.'/'.$_FILES[$file]['name'] ))
{
//Make jpeg.
$func_final=$func_image[$arr_image[2]];
$img = $func_final($this->path.'/'.$_FILES[$file]['name']);
//imagejpeg ( $img, $this->path.'/'.$_FILES[$file]['name'], $this->quality_jpeg );
//Reduce size for default if $this->img_width['']
if(isset($this->img_width['']))
{
if($arr_image[0]>$this->img_width[''])
{
$width=$this->img_width[''];
$ratio = ($arr_image[0] / $width);
$height = round($arr_image[1] / $ratio);
$thumb = imagecreatetruecolor($width, $height);
imagecopyresampled ($thumb, $img, 0, 0, 0, 0, $width, $height, $arr_image[0], $arr_image[1]);
$image_func_create ( $thumb, $this->path.'/'.$_FILES[$file]['name'], $this->quality_jpeg );
}
unset($this->img_width['']);
}
//Make thumb if specific...
if($this->thumb==1)
{
//Convert to jpeg.
foreach($this->img_width as $name_width => $width)
{
$ratio = ($arr_image[0] / $width);
$height = round($arr_image[1] / $ratio);
if(isset($this->img_minimal_height[$name_width]))
{
if($height<$this->img_minimal_height[$name_width])
{
//Need recalculate the adecuate width and height.
$height=$this->img_minimal_height[$name_width];
$ratio=($arr_image[1] / $height);
$width=round($arr_image[0]/$ratio);
//$width=
}
}
$thumb = imagecreatetruecolor($width, $height);
imagecopyresampled ($thumb, $img, 0, 0, 0, 0, $width, $height, $arr_image[0], $arr_image[1]);
$image_func_create ( $thumb, $this->path.'/'.$name_width.'_'.$_FILES[$file]['name'], $this->quality_jpeg );
;
//imagepng ( resource $image [, string $filename [, int $quality [, int $filters ]]] )
}
}
//unlink($_FILES[$file]['tmp_name']);
//Unlink if exists image
if(isset($_POST[$file]))
{
if($_POST[$file]!='')
{
$image_file=Utils::form_text($_POST[$file]);
if($image_file!='')
{
@unlink($this->path.'/'.$image_file);
foreach($this->img_width as $key => $value)
{
@unlink($this->path.'/'.$key.'_'.$image_file);
}
$image='';
}
}
}
return $_FILES[$file]['name'];
//return $this->path.'/'.$_FILES[$file]['name'];
}
else
{
$this->std_error=I18n::lang('common', 'error_cannot_upload_this_image_to_the_server', 'Error: Cannot upload this image to the server');
if(DEBUG==1)
{
$this->std_error.=' Image origin '.$_FILES[$file]['tmp_name'].' in this path '.$this->path;
}
return '';
}
}
else
{
$this->std_error.=I18n::lang('error_model', 'img_format_error', 'Img format error, only accept gif, jpg and png formats');
}
}
else if($image!='')
{
return $image;
}
}
else if($image!=='')
{
if(file_exists($this->path.'/'.$image))
{
$this->value=$this->path.'/'.$image;
return $image;
}
else
{
$this->std_error=I18n::lang('error_model', 'check_error_enctype_for_upload_file', 'Please, check enctype form of file form');
return '';
}
}
else
{
$this->std_error=I18n::lang('error_model', 'check_error_enctype_for_upload_file', 'Please, check enctype form of file form');
}
*/
$this->value='';
return '';
@ -481,15 +194,13 @@ class ImageField extends PhangoField {
}
function process_delete_field($model, $name_field, $conditions)
function process_delete_field($model, $name_field)
{
//die;
$query=$model->select($conditions, array($name_field));
$query=$model->select(array($name_field));
while(list($image_name)=webtsys_fetch_row($query))
while(list($image_name)=$model->fetch_row($query))
{
if( file_exists($this->path.'/'.$image_name) && !is_dir($this->path.'/'.$image_name) )
@ -571,6 +282,14 @@ class ImageField extends PhangoField {
}
function get_parameters_default()
{
$this->form_loaded->file_url=$this->url_path;
}
}
?>

View file

@ -3,6 +3,7 @@
namespace PhangoApp\PhaModels\Forms;
use PhangoApp\PhaModels\Forms\BaseForm;
use PhangoApp\PhaUtils\Utils;
/**
* Basic class for create forms
@ -17,9 +18,18 @@ class FileForm extends BaseForm{
$this->type='file';
$this->enctype=1;
$this->file_url='';
}
public function form()
{
return '<input type="'.$this->type.'" class="'.$this->css.'" name="'.$this->name.'_file" value=""> <a href="'.$this->file_url.'/'.$this->default_value.'">'.Utils::form_text($this->default_value).'</a><input type="hidden" name="'.$this->name.'" value="'.$this->setform($this->default_value).'" />';
}
}