Skip to content

Commit

Permalink
start project
Browse files Browse the repository at this point in the history
  • Loading branch information
yiiman-dev committed Jan 2, 2022
1 parent 8e98a0b commit 5cb1847
Show file tree
Hide file tree
Showing 138 changed files with 12,589 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
.idea/
composer.lock
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# upload-manager
Yii library for manage uploads
## ENV variables
``YII_PUBLIC_HTML_DIR`` public_html directory path **
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "amintado/yii-lib-upload-manager",
"description": "Library for Yii2 framework and manage/edit uploaded images/files",
"license": "BSD-3-Clause",
"autoload": {
"psr-4": {
"YiiMan\\LibUploadManager\\": "src/"
}
},
"authors": [
{
"name": "YiiMan",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=7.2",
"yiisoft/yii2": "2.*",
"ext-fileinfo": "*",
"yiiman/yii-module-setting": "0.0.*",
"ext-json": "*",
"ext-gd": "*"
}
}
192 changes: 192 additions & 0 deletions src/lib/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php
/**
* Created by YiiMan.
* Programmer: gholamreza beheshtian
* Mobile:09353466620
*
* Site:http://yiiman.ir
* Date: 8/24/2018
* Time: 9:25 PM
*/

namespace YiiMan\LibUploadManager\lib;

use yii\base\Component;
use YiiMan\Setting\module\components\Options;

/**
* این کلاس فایل های پوشه های آپلود را به شکلی ساده و بازگردانی می کند
*
*
* sample use syntax : Yii::$app->File->{folder name}->{file name}
*
*
* or
*
*
* Yii::$app->File->{folder name}->{folder name}->{file Name}
*
*
* this is very easy is'nt it?
* Class File
* @package YiiMan\LibUploadManager
*/
class File extends Component {
public $address;
public $options;
public $uploadDir;
public function init() {

$this->options = new Options();

$this->uploadDir=realpath( $this->options->UploadDir);
if (empty($this->uploadDir)){
mkdir( $this->uploadDir.'/upload');
}
}
public function __get( $name ) {

// $out= parent::__get( $name ); // TODO: Change the autogenerated stub

if (empty( $out)){

return $this->get( $name);
}
}

/**
* will return all files name list as array in this folder
*/
public function AllFilesName(){
return $this->getFileList();
}

/**
* @param string $name
* این تابع محتوای یک فایل را بازگردانی می کند
*
* @return bool|Object1|string
*/
public function oneFile($name){
$path=$this->address.'/'. $name;

if (file_exists( $path)){
if (is_file( $path)){
return file_get_contents( $path);
}else{
return $this->get($path);
}
}else{
return '';
}
}

/**
* @param $name
*
* @return $this|string
*/
private function get($name) {
/* < محاسبه ی مسیر های فایل > */
{
if (empty( $this->address)){
$path=$this->uploadDir.'/'.$name;
$this->address=$path;
}else{
$path=$this->address.'/'.$name;
$this->address=$path;
}
}
/* < ایجاد کردن پوشه در صورت وجود نداشتن > */
{
if (!file_exists($this->address)){
mkdir( $this->address);
}
}
/* </ ایجاد کردن پوشه در صورت وجود نداشتن > */

/* </ محاسبه ی مسیر های فایل > */
return $this;
}

protected function getFileList() {
$dir=$this->address;
// array to hold return value
$retval = [];

// add trailing slash if missing
if ( substr( $dir, - 1 ) != "/" ) {
$dir .= "/";
}

// open pointer to directory and read list of files
try{

$d = @dir( $dir );
if (empty($d))return null;
}catch (\Exception $e){
return null;
}
while ( false !== ( $entry = $d->read() ) ) {
// skip hidden files
if ( $entry{0} == "." ) {
continue;
}
if ( is_dir( "{$dir}{$entry}" ) ) {
$retval[$entry] = [
'name' => "{$entry}",
'type' => filetype( "{$dir}{$entry}" ),
'size' => 0,
'lastmod' => filemtime( "{$dir}{$entry}" )
];
} elseif ( is_readable( "{$dir}{$entry}" ) ) {
$retval[$entry] = [
'name' => "{$entry}",
'type' => mime_content_type( "{$dir}{$entry}" ),
'size' => filesize( "{$dir}{$entry}" ),
'lastmod' => filemtime( "{$dir}{$entry}" )
];
}
}
$d->close();

return $retval;
}

/**
* با اعلام نام آیتم موجود در فرم، فایل را گرفته و به صورت بیس 64 ذخیره می کند
*/
public function saveFile( $attribute ) {

/* < Get File From Post And Save In server > */
{
$file = UploadedFile::getInstanceByName( $attribute );
$path = $this->address . "/" . $attribute . '.base64';
$file->saveAs( $path );
$extention = $file->extension;
}
/* </ Get File From Post And Save In server > */


/* < Convert To base64 > */
{

$file = file_get_contents( $path );
$file=base64_encode( $file);
$file = 'data:image/' . $extention . ';base64,' . $file;
}
/* </ Convert To base64 > */


/* < Write To File > */
{

$myfile = fopen( $path , "w" ) or die( "به پوشه ی فایل ها یا خود فایل های ذخیره شده دسترسی نداریم." );
fwrite( $myfile , $file );
fclose( $myfile );
}
/* </ Write To File > */

return $attribute . '.base64';
}
}
53 changes: 53 additions & 0 deletions src/lib/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright (c) 2018.
* Author: YiiMan
* Programmer: gholamreza beheshtian
* mobile: 09353466620
* WebSite:http://yiiman.ir
*
*
*/

/**
* Created by PhpStorm.
* User: amintado
* Date: 7/29/2018
* Time: 4:52 PM
*/

namespace YiiMan\LibUploadManager\lib;


use yii\base\Component;
use yii\web\View;

class Image extends Component {

public function begin($view){

$js=<<<JS
function init() {
var imgDefer = document.getElementsByTagName('img');
for (var i=0; i<imgDefer.length; i++) {
if(imgDefer[i].getAttribute('data-src')) {
imgDefer[i].setAttribute('src',imgDefer[i].getAttribute('data-src'));
}
}
}
window.onload = init;
JS;


/**
* @var $view \yii\web\View
*/
$view->registerJs( $js,View::POS_BEGIN);
}

public function img($url){
$sign="data:image/png;base64,";
$tag='<img src="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" data-src="your-image-is-here">';
}
}
Loading

0 comments on commit 5cb1847

Please sign in to comment.