- Seme Framework
- Credits
- Version 3.2.X
- Issue
- Deployment
WideImage Library
Seme Framework running well with WideImage, read more about WideImage .
Installation
Download the WideImage library, and then put wideimage
directory under app/kero/lib
.
kero
└── lib
└── wideimage
└── ...
Load the WideImage
For loading WideImage into a controller class, simply using lib method in a controller.
...
public function __construct(){
parent:: __construct();
...
$this->lib("wideimage", 'inc');
...
}
..
Example Usage
This is full example for upload image and then create thumbnail using WideImage
for image resizer.
/**
* 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(SENEROOT.$targetdir);
if (empty($targetdircheck)) {
if (PHP_OS == "WINNT") {
if (!is_dir(SENEROOT.$targetdir)) {
mkdir(SENEROOT.$targetdir);
}
} else {
if (!is_dir(SENEROOT.$targetdir)) {
mkdir(SENEROOT.$targetdir, 0775);
}
}
}
$tahun = date("Y");
$targetdir = $targetdir.DIRECTORY_SEPARATOR.$tahun;
$targetdircheck = realpath(SENEROOT.$targetdir);
if (empty($targetdircheck)) {
if (PHP_OS == "WINNT") {
if (!is_dir(SENEROOT.$targetdir)) {
mkdir(SENEROOT.$targetdir);
}
} else {
if (!is_dir(SENEROOT.$targetdir)) {
mkdir(SENEROOT.$targetdir, 0775);
}
}
}
$bulan = date("m");
$targetdir = $targetdir.DIRECTORY_SEPARATOR.$bulan;
$targetdircheck = realpath(SENEROOT.$targetdir);
if (empty($targetdircheck)) {
if (PHP_OS == "WINNT") {
if (!is_dir(SENEROOT.$targetdir)) {
mkdir(SENEROOT.$targetdir);
}
} else {
if (!is_dir(SENEROOT.$targetdir)) {
mkdir(SENEROOT.$targetdir, 0775);
}
}
}
$sc->status = 998;
$sc->message = 'Invalid file extension uploaded';
if (in_array($fileext, array("gif", "jpg", "png","jpeg"))) {
$filecheck = SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename.'.'.$fileext;
if (file_exists($filecheck)) {
unlink($filecheck);
$rand = rand(0, 999);
$filename = "$id-$ke-".$rand;
$filecheck = SENEROOT.$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'], SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename);
if (is_file(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename) && file_exists(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename)) {
if (@mime_content_type(SENEROOT.$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(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filethumb) && is_file(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filethumb)) {
unlink(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filethumb);
}
if (file_exists(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename) && is_file(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename)) {
WideImage::load(SENEROOT.$targetdir.DIRECTORY_SEPARATOR.$filename)->reSize(370)->saveToFile(SENEROOT.$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;
}