/var/www/vhosts/ihelp.ro/httpdocs/src/Model/Table
Edit: /var/www/vhosts/ihelp.ro/httpdocs/src/Model/Table/GalleryImagesTable.php (11671B)
setTable('gallery_images');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->addBehavior('Tree');
$this->addBehavior('Josegonzalez/Upload.Upload', [
'image_path' => [
'path' => 'webroot{DS}img{DS}uploads{DS}images{DS}{field-value:model}{DS}{field-value:foreign_uuid}{DS}',
'fields' => [
'dir' => 'upload_dir',
],
'nameCallback' => function ($table, $entity, $data, $field, $settings) {
$ext = pathinfo($data->getClientFilename(), PATHINFO_EXTENSION);
return uniqid() . '_' . rand(1, 9999) . '_' . date('Ymd_Hms') . '.' . $ext;
},
'transformer' => function ($table, $entity, $data, $field, $settings, $filename) {
/**
* test start 2
*
* */
$extension = pathinfo($filename, PATHINFO_EXTENSION);
// Store the thumbnail in a temporary file
$tmp = tempnam(sys_get_temp_dir(), 'upload') . '.' . $extension;
$backgroundWidth = 800;
$backgroundHeight = 600;
$imagine = new \Imagine\Gd\Imagine();
$size = new \Imagine\Image\Box($backgroundWidth, $backgroundHeight);
$mode = \Imagine\Image\ImageInterface::THUMBNAIL_FLAG_NOCLONE;
$x = 0;
$y = 0;
$palette = new \Imagine\Image\Palette\RGB();
$background = $imagine->create(new \Imagine\Image\Box($backgroundWidth, $backgroundHeight), $palette->color('#ededed', 100));
$photo = $imagine->open($data->getStream()->getMetadata('uri'))->thumbnail($size, $mode);
$srcImgWidth = $photo->getSize()->getWidth();
$srcImgHeight = $photo->getSize()->getHeight();
// daca width <800
if ($backgroundWidth > $srcImgWidth) {
$x = $backgroundWidth / 2 - $srcImgWidth / 2;
} else if ($backgroundHeight > $srcImgHeight) {
$y = $backgroundHeight / 2 - $srcImgHeight / 2;
}
$background->paste($photo, new \Imagine\Image\Point($x, $y))->save($tmp); // test end 2
/**
* test start 1
*
* */
// $extension = pathinfo($filename, PATHINFO_EXTENSION);
// // Store the thumbnail in a temporary file
// $tmp = tempnam(sys_get_temp_dir(), 'upload') . '.' . $extension;
// $finalW = 800;
// $finalH = 600;
// $finalWHRatio = $finalW / $finalH;
// $mode = \Imagine\Image\ImageInterface::FILTER_LANCZOS;
// $imagine = new \Imagine\Gd\Imagine();
// $image = $imagine->open($data->getStream()->getMetadata('uri'));
// $srcImgWidth = $image->getSize()->getWidth();
// $srcImgHeight = $image->getSize()->getHeight();
// $srcWHRatio = $srcImgWidth / $srcImgHeight;
// $resizedH = '';
// $resizedW = '';
// if ($srcWHRatio > $finalWHRatio) {
// $resizedW = $srcWHRatio * $finalH;
// $resizedH = $finalH;
// $x_crop = $resizedW / 2 - ($finalW / 2);
// $y_crop = 0;
// $imagine
// ->open($data->getStream()->getMetadata('uri'))
// ->resize(new \Imagine\Image\Box($resizedW, $resizedH), $mode)
// ->crop(new Point($x_crop, $y_crop), new Box($resizedW, $$resizedH))
// ->save($tmp);
// } else {
// $resizedW = $finalH;
// $resizedH = $finalW / $srcWHRatio;
// $x_crop = 0;
// $y_crop = $resizedH / 2 - $finalH / 2;
// $imagine
// ->open($data->getStream()->getMetadata('uri'))
// ->resize(new \Imagine\Image\Box($resizedW, $resizedH), $mode)
// ->crop(new Point($x_crop, $y_crop), new Box($resizedW, $resizedH))
// ->save($tmp);
// } //test end 1
/**
* start default
*
* */
// $extension = pathinfo($filename, PATHINFO_EXTENSION);
// // Store the thumbnail in a temporary file
// $tmp = tempnam(sys_get_temp_dir(), 'upload') . '.' . $extension;
// // Use the Imagine library to DO THE THING
// $size = new \Imagine\Image\Box(800, 600);
// $mode = \Imagine\Image\ImageInterface::THUMBNAIL_INSET;
// $imagine = new \Imagine\Gd\Imagine();
// // Save that modified file to our temp file
// $imagine->open($data->getStream()->getMetadata('uri'))
// ->thumbnail($size, $mode)
// ->crop(new Point(0, 0), $size)
// ->save($tmp);
// end default
// Now return the original *and* the thumbnail
return [
$data->getStream()->getMetadata('uri') => $filename,
$tmp => 'resize-' . $filename,
];
},
'keepFilesOnDelete' => false,
],
]);
$this->belongsTo('ParentGalleryImages', [
'className' => 'GalleryImages',
'foreignKey' => 'parent_id',
]);
$this->hasMany('ChildGalleryImages', [
'className' => 'GalleryImages',
'foreignKey' => 'parent_id',
]);
$this->belongsTo('Causes', [
'foreignKey' => 'foreign_key'
]);
$this->belongsTo('Products', [
'foreignKey' => 'foreign_key'
]);
}
/**
* Default validation rules.
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->integer('id')
->allowEmptyString('id', null, 'create');
$validator
->integer('foreign_key')
->requirePresence('foreign_key', 'create')
->notEmptyString('foreign_key');
$validator
->scalar('model')
->maxLength('model', 64)
->requirePresence('model', 'create')
->notEmptyString('model');
$validator
->scalar('name')
->maxLength('name', 255)
->allowEmptyString('name');
$validator
->scalar('description')
->allowEmptyString('description');
$validator
->scalar('copyright')
->maxLength('copyright', 255)
->allowEmptyString('copyright');
$validator->setProvider('upload', \Josegonzalez\Upload\Validation\DefaultValidation::class);
$validator
->requirePresence('image_path', 'create', __d('admin', 'Acest câmp este obligatoriu'))
->allowEmptyFile('image_path');
$validator
->add('image_path', 'isFileUpload', [
'rule' => ['isFileUpload'],
'message' => __d('admin', 'Selectați imaginea'),
'provider' => 'upload',
'last' => true,
])
->add('image_path', 'fileExtensions', [
'rule' => ['mimeType', ['image/jpeg', 'image/png', 'image/jpeg']],
'message' => __d('admin', 'Fișierele trebuie să fie .png .jpg'),
'last' => true,
])
->add('image_path', 'fileBelowMaxSize', [
'rule' => ['isBelowMaxSize', 1024 * 1024 * 1],
'message' => __d('admin', 'Fișierul trebuie să aibă sub 1 MB'),
'provider' => 'upload'
])
->add('image_path', 'fileAboveMinWidth', [
'rule' => ['isAboveMinWidth', 800],
'message' => __d('admin', 'Această imagine trebuie să aibă o lățime de cel puțin 800 de pixeli'),
'provider' => 'upload'
])
->add('image_path', 'fileAboveMinHeight', [
'rule' => ['isAboveMinHeight', 600],
'message' => __d('admin', 'Această imagine trebuie să aibă cel puțin 600 de pixeli înălțime'),
'provider' => 'upload'
]);
return $validator;
}
/**
* Default validation rules.
*/
public function validationFeaturedImage(Validator $validator): Validator
{
$validator->setProvider('upload', \Josegonzalez\Upload\Validation\DefaultValidation::class);
$validator
->requirePresence('image_path', 'create', __d('admin', 'Acest câmp este obligatoriu'))
->allowEmptyFile('image_path');
$validator
->add('image_path', 'isFileUpload', [
'rule' => ['isFileUpload'],
'message' => __d('admin', 'Selectați imaginea'),
'provider' => 'upload',
'last' => true,
])
->add('image_path', 'fileExtensions', [
'rule' => ['mimeType', ['image/jpeg', 'image/png', 'image/jpeg']],
'message' => __d('admin', 'Fișierele trebuie să fie .png .jpg'),
'last' => true,
])
->add('image_path', 'fileBelowMaxSize', [
'rule' => ['isBelowMaxSize', 1024 * 1024 * 1],
'message' => __d('admin', 'Fișierul trebuie să aibă sub 1 MB'),
'provider' => 'upload'
])
->add('image_path', 'fileAboveMinWidth', [
'rule' => ['isAboveMinWidth', 800],
'message' => __d('admin', 'Această imagine trebuie să aibă o lățime de cel puțin 800 de pixeli'),
'provider' => 'upload'
])
->add('image_path', 'fileAboveMinHeight', [
'rule' => ['isAboveMinHeight', 600],
'message' => __d('admin', 'Această imagine trebuie să aibă cel puțin 600 de pixeli înălțime'),
'provider' => 'upload'
]);
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->existsIn(['parent_id'], 'ParentGalleryImages'), ['errorField' => 'parent_id']);
return $rules;
}
}