- Seme Framework
- versi 4.0.3 (ID)
- Persyaratan
- Download & Install
- Pengaturan
- Tutorial
- Perutean URI
- Konstanta
- Variabel Global
- Model
- View
- Controller
- Library
- CLI (command line interface)
- Core
Library WideImage
WideImage dengan Seme Framework berjalan dengan baik, baca selengkapnya tentang WideImage .
Cara Install
Download library WideImage, buka direktori app/kero/lib
, kemudian buat direktori baru bernama wideimage
, setelah itu copy paste isi library-nya ke direktori tersebut.
kero
└── lib
└── wideimage
└── ...
Memanggil Library
Untuk memanggil library, gunakan metode lib di kelas controller.
...
public function __construct(){
parent:: __construct();
...
$this->lib("wideimage", 'inc');
...
}
..
Contoh Pengguaan
Berikut ini adalah contoh metode private yang menggunakan WideImage
untuk merubah ukuran gambar yang telah selesai diupload.
/**
* Method for handling file upload
* Only allowed .png, .jpeg, .jpg, .gif extension
* Max file size 2000000 bytes
* Unsupported WebP encoding image
* @param string $keyname the $_FILES key that send from html
* @param string $id the id of user, product, or uniqid
* @param string $ke sequencer
* @constructor
* @return object result with object, contain status, message, image, and thumb.
*/
private function __uploadImagex($keyname, $id, $ke="")
{
$sc = new stdClass();
$sc->status = 500;
$sc->message = 'Error';
$sc->image = '';
$sc->thumb = '';
if (isset($_FILES[$keyname]['name'])) {
if ($_FILES[$keyname]['size']>2000000) {
$sc->status = 301;
$sc->message = 'Ukuran gambar terlalu besar. Silakan pilih dengan ukuran kurang dari 2 MB';
$this->seme_log->write('User::__uploadImagex -- forceClose '.$sc->status.' '.$sc->message);
return $sc;
}
$filenames = pathinfo($_FILES[$keyname]['name']);
if (isset($filenames['extension'])) {
$fileext = strtolower($filenames['extension']);
} else {
$fileext = 'jpg';
}
if (!in_array($fileext, array("jpg","png","jpeg","gif"))) {
$sc->status = 303;
$sc->message = 'Invalid file extension, please try other image file.';
$this->seme_log->write('User::__uploadImagex -- forceClose '.$sc->status.' '.$sc->message);
return $sc;
}
$filename = "$id-$ke";
$filethumb = $filename.'-thumb';
$targetdir = 'media/upload/';
$targetdircheck = realpath(SEMEROOT.$targetdir);
if (empty($targetdircheck)) {
if (PHP_OS == "WINNT") {
if (!is_dir(SEMEROOT.$targetdir)) {
mkdir(SEMEROOT.$targetdir);
}
} else {
if (!is_dir(SEMEROOT.$targetdir)) {
mkdir(SEMEROOT.$targetdir, 0775);
}
}
}
$tahun = date("Y");
$targetdir = $targetdir.DIRECTORY_SEPARATOR.$tahun;
$targetdircheck = realpath(SEMEROOT.$targetdir);
if (empty($targetdircheck)) {
if (PHP_OS == "WINNT") {
if (!is_dir(SEMEROOT.$targetdir)) {
mkdir(SEMEROOT.$targetdir);
}
} else {
if (!is_dir(SEMEROOT.$targetdir)) {
mkdir(SEMEROOT.$targetdir, 0775);
}
}
}
$bulan = date("m");
$targetdir = $targetdir.DIRECTORY_SEPARATOR.$bulan;
$targetdircheck = realpath(SEMEROOT.$targetdir);
if (empty($targetdircheck)) {
if (PHP_OS == "WINNT") {
if (!is_dir(SEMEROOT.$targetdir)) {
mkdir(SEMEROOT.$targetdir);
}
} else {
if (!is_dir(SEMEROOT.$targetdir)) {
mkdir(SEMEROOT.$targetdir, 0775);
}
}
}
$sc->status = 998;
$sc->message = 'Invalid file extension uploaded';
if (in_array($fileext, array("gif", "jpg", "png","jpeg"))) {
$filecheck = SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename.'.'.$fileext;
if (file_exists($filecheck)) {
unlink($filecheck);
$rand = rand(0, 999);
$filename = "$id-$ke-".$rand;
$filecheck = SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename.'.'.$fileext;
if (file_exists($filecheck)) {
unlink($filecheck);
$rand = rand(1000, 99999);
$filename = "$id-$ke-".$rand;
}
}
$filethumb = $filename."-thumb.".$fileext;
$filename = $filename.".".$fileext;
move_uploaded_file($_FILES[$keyname]['tmp_name'], SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename);
if (is_file(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename) && file_exists(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename)) {
if (@mime_content_type(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename) == 'image/webp') {
$sc->status = 302;
$sc->message = 'WebP image format currently unsupported';
$this->seme_log->write('User::__uploadImagex -- forceClose '.$sc->status.' '.$sc->message);
return $sc;
}
$this->lib("wideimage/WideImage", 'wideimage', "inc");
if (file_exists(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filethumb) && is_file(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filethumb)) {
unlink(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filethumb);
}
if (file_exists(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename) && is_file(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename)) {
WideImage::load(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename)->reSize(370)->saveToFile(SEMEROOT.$targetdir.DIRECTORY_SEPARATOR.$filethumb);
$sc->status = 200;
$sc->message = 'Successful';
$sc->thumb = str_replace("//", "/", $targetdir.'/'.$filethumb);
$sc->image = str_replace("'\'", "/", $targetdir.'/'.$filename);
$sc->image = str_replace("//", "/", $targetdir.'/'.$filename);
} else {
$sc->status = 997;
$sc->message = 'Failed: file image not exists';
$this->seme_log->write('User::__uploadImagex -- forceClose '.$sc->status.' '.$sc->message);
}
} else {
$sc->status = 999;
$sc->message = 'Upload file failed';
$this->seme_log->write('User::__uploadImagex -- forceClose '.$sc->status.' '.$sc->message);
}
} else {
$sc->status = 998;
$sc->message = 'Invalid file extension uploaded';
$this->seme_log->write('User::__uploadImagex -- forceClose '.$sc->status.' '.$sc->message);
}
} else {
$sc->status = 988;
$sc->message = 'Keyname file does not exists';
$this->seme_log->write('User::__uploadImagex -- forceClose '.$sc->status.' '.$sc->message);
}
return $sc;
}