Skip to content

Commit 10c77eb

Browse files
committed
first commit
0 parents  commit 10c77eb

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

assets/Yii2CamAsset.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace rplvrtha\camera\assets;
3+
4+
use yii\web\AssetBundle;
5+
6+
class Yii2CamAsset extends AssetBundle
7+
{
8+
public $sourcePath = '@vendor/rplvrtha/yii2-camera/assets';
9+
public $js = [
10+
'js/camera.js',
11+
];
12+
public $depends = [
13+
'yii\web\JqueryAsset',
14+
];
15+
}

assets/js/camera.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
$(document).ready(function() {
2+
const options = window.cameraWidgetOptions || {};
3+
const openCameraBtn = $('#openNativeCamera');
4+
const fileInput = $('#fileInput');
5+
const previewImage = $('#previewImage');
6+
7+
// Gunakan opsi dari PHP
8+
const uploadUrl = options.uploadUrl;
9+
const retakeText = options.retakeText || 'Ambil Ulang Gambar';
10+
const onSuccess = options.onSuccess;
11+
const onError = options.onError;
12+
13+
openCameraBtn.on('click', function() {
14+
fileInput.click();
15+
});
16+
17+
fileInput.on('change', function(event) {
18+
const file = event.target.files[0];
19+
if (!file) {
20+
return;
21+
}
22+
23+
const reader = new FileReader();
24+
reader.onload = function(e) {
25+
openCameraBtn.text(retakeText);
26+
previewImage.attr('src', e.target.result).show();
27+
28+
const formData = new FormData();
29+
formData.append('imageFile', file);
30+
formData.append('_csrf', yii.getCsrfToken());
31+
32+
$.ajax({
33+
url: uploadUrl,
34+
method: 'POST',
35+
data: formData,
36+
processData: false,
37+
contentType: false,
38+
success: function(response) {
39+
if (onSuccess && typeof window[onSuccess] === 'function') {
40+
window[onSuccess](response);
41+
} else {
42+
console.log('Gambar berhasil diunggah:', response);
43+
}
44+
},
45+
error: function(xhr, status, error) {
46+
if (onError && typeof window[onError] === 'function') {
47+
window[onError](error);
48+
} else {
49+
console.error('Error saat mengunggah gambar:', error);
50+
}
51+
}
52+
});
53+
};
54+
reader.readAsDataURL(file);
55+
});
56+
});

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "rplvrtha/yii2-camera",
3+
"version": "v0.25.09-dev",
4+
"description": "A simple camera extension for Yii2 that uses the built-in camera on your phone.",
5+
"type": "yii2-extension",
6+
"keywords": ["yii2", "camera", "widget", "file", "upload"],
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "rplvrtha",
11+
"email": "radityaloviartha@email.com"
12+
}
13+
],
14+
"require": {
15+
"php": ">=7.4",
16+
"yiisoft/yii2": "^2.0.42"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"rplvrtha\\camera\\assets\\": "assets/",
21+
"rplvrtha\\camera\\": "src/"
22+
}
23+
}
24+
}

src/Yii2Cam.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace rplvrtha\camera;
4+
5+
use rplvrtha\camera\assets\Yii2CamAsset;
6+
use yii\base\Widget;
7+
use yii\helpers\Html;
8+
use yii\helpers\Json;
9+
use yii\web\View;
10+
11+
class Yii2Cam extends Widget
12+
{
13+
public $uploadUrl;
14+
public $onSuccess = null;
15+
public $onError = null;
16+
public $buttonText = 'Buka Kamera';
17+
public $retakeText = 'Ambil Ulang Gambar';
18+
public $previewWidth = '100%';
19+
public $previewClass = '';
20+
21+
public function run()
22+
{
23+
Yii2CamAsset::register($this->getView());
24+
25+
$options = Json::encode([
26+
'uploadUrl' => $this->uploadUrl,
27+
'onSuccess' => $this->onSuccess,
28+
'onError' => $this->onError,
29+
'retakeText' => $this->retakeText,
30+
]);
31+
32+
$this->getView()->registerJs("
33+
window.cameraWidgetOptions = {$options};
34+
", View::POS_HEAD);
35+
36+
$html = Html::tag(
37+
'div',
38+
Html::img('', [
39+
'id' => 'previewImage',
40+
'class' => 'img-fluid d-block mb-3 ' . $this->previewClass,
41+
'style' => 'display: none; width: ' . $this->previewWidth . ';',
42+
'alt' => 'Pratinjau Gambar'
43+
]) .
44+
Html::button($this->buttonText, [
45+
'id' => 'openNativeCamera',
46+
'class' => 'btn btn-primary btn-lg w-100'
47+
]) .
48+
Html::fileInput('imageFile', null, [
49+
'accept' => 'image/*',
50+
'capture' => 'environment',
51+
'id' => 'fileInput',
52+
'style' => 'display: none;'
53+
]),
54+
['class' => 'camera-container p-4']
55+
);
56+
57+
return $html;
58+
}
59+
}

0 commit comments

Comments
 (0)