I am working on this Laravel 5.4 Social Network Script and when I post something the avatar is being displayed "animated" but when it is being saved it is converted to an static image.
I have this in the controller TimelineController#createPost:
...
use App\Media;
use App\Post;
use Intervention\Image\Facades\Image;
...
public function createPost(Request $request)
{
$input = $request->all();
$post = Post::create($input);
...
if ($request->file('post_images_upload_modified')) {
foreach ($request->file('post_images_upload_modified') as $postImage) {
$strippedName = str_replace(' ', '', $postImage->getClientOriginalName());
$photoName = date('Y-m-d-H-i-s').$strippedName;
$avatar = Image::make($postImage->getRealPath());
$avatar->save(storage_path().'/uploads/users/gallery/'.$photoName, 60);
$media = Media::create([
'title' => $photoName,
'type' => 'image',
'source' => $photoName,
]);
$post->images()->attach($media);
}
}
...
}
...
It uses GD Library to process the images internally.
The file Image.php on /vendor/intervention/image/src/Intervention/Image/:
#method \Intervention\Image\Image make(mixed $source) Universal factory method to create a new image instance from source, which can be a filepath, a GD image resource, an Imagick object or a binary image data.
/**
* Starts encoding of current image
*
* #param string $format
* #param integer $quality
* #return \Intervention\Image\Image
*/
public function encode($format = null, $quality = 90)
{
return $this->driver->encode($this, $format, $quality);
}
/**
* Saves encoded image in filesystem
*
* #param string $path
* #param integer $quality
* #return \Intervention\Image\Image
*/
public function save($path = null, $quality = null)
{
$path = is_null($path) ? $this->basePath() : $path;
if (is_null($path)) {
throw new Exception\NotWritableException(
"Can't write to undefined path."
);
}
$data = $this->encode(pathinfo($path, PATHINFO_EXTENSION), $quality);
$saved = #file_put_contents($path, $data);
if ($saved === false) {
throw new Exception\NotWritableException(
"Can't write image data to path ({$path})"
);
}
// set new file info
$this->setFileInfoFromPath($path);
return $this;
}
The stored image is a GIF but resized despite it is not specified:
Do you know how to make the image animated when saved?
Related
I'm making an app in Laravel 5.7 . I want to upload image in database through it and I want to show it from database.
I have tried different methods around the Internet as I was getting issues in
Intervention\Image\Facades\Image
I followed many advices from Internet make changes in config.app
made changes in Composer
At the end used
use Intervention\Image\Facades\Image as Image;
So I get resolved from issue "Undefined class Image"
but now I' m getting issues as "Undefined class File",
Method getClientOriginalExtension not found.
Method Upsize, make not found.
My code is
<?php
namespace App\Http\Controllers;
use File;
use Intervention\Image\Facades\Image as Image;
use App\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
//
protected $user;
/**
* [__construct description]
* #param Photo $photo [description]
*/
public function __construct(
User $user )
{
$this->user = $user;
}
/**
* Display photo input and recent images
* #return view [description]
*/
public function index()
{
$users = User::all();
return view('profile', compact('users'));
}
public function uploadImage(Request $request)
{
$request->validate([
'image' => 'required',
'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
//check if image exist
if ($request->hasFile('image')) {
$images = $request->file('image');
//setting flag for condition
$org_img = $thm_img = true;
// create new directory for uploading image if doesn't exist
if( ! File::exists('images/originals/')) {
$org_img = File::makeDirectory('images/originals/', 0777, true);
}
if ( ! File::exists('images/thumbnails/')) {
$thm_img = File::makeDirectory('images/thumbnails', 0777, true);
}
// loop through each image to save and upload
foreach($images as $key => $image) {
//create new instance of Photo class
$newPhoto = new $this->user;
//get file name of image and concatenate with 4 random integer for unique
$filename = rand(1111,9999).time().'.'.$image->getClientOriginalExtension();
//path of image for upload
$org_path = 'images/originals/' . $filename;
$thm_path = 'images/thumbnails/' . $filename;
$newPhoto->image = 'images/originals/'.$filename;
$newPhoto->thumbnail = 'images/thumbnails/'.$filename;
//don't upload file when unable to save name to database
if ( ! $newPhoto->save()) {
return false;
}
// upload image to server
if (($org_img && $thm_img) == true) {
Image::make($image)->fit(900, 500, function ($constraint) {
$constraint->upsize();
})->save($org_path);
Image::make($image)->fit(270, 160, function ($constraint) {
$constraint->upsize();
})->save($thm_path);
}
}
}
return redirect()->action('UserController#index');
}
}
Please suggest me any Image Upload code without updating repositories or suggest me how can I remove issues from this code.
The beginning of time read below link because laravel handled create directory and hash image and put directory
laravel file system
then read file name when stored on directory and holds name on table field when need image retrieve name field and call physical address on server
$upload_id = $request->file('FILENAME');
$file_name = time().$upload_id->getClientOriginalName();
$destination =
$_SERVER["DOCUMENT_ROOT"].'/adminbusinessplus/storage/uploads';
$request->file('FILENAME')->move($destination, $file_name);
$string="123456stringsawexs";
$extension = pathinfo($upload_id, PATHINFO_EXTENSION);
$path = $destination.'/'.$file_name;
$public =1;
$user_id = $request->logedin_user_id;
$hash = str_shuffle($string);
$request->user_id = $request->logedin_user_id;
$request->name = $file_name;
$request->extension = $extension;
$request->path = $path;
$request->public = $public;
$request->hash = $hash;
//$request INSERT INTO MODEL uploads
$file_id = Module::insert("uploads", $request);
Im saving a request to my database from my vue js via;
public function store(Request $request)
{
//validate
$this->validate($request, [
'name' => 'required',
'description' => 'required',
'price' => 'required'
]);
//get image
$exploded = explode(',', $request->cover_image);
$decoded = base64_decode($exploded[1]);
if(str_contains($exploded[0],'jpeg'))
$extension = 'jpg';
else
$extension = 'png';
$fileName = str_random().'.'.$extension;
$path = public_path().'/cover_images/'.$fileName;
file_put_contents($path, $decoded);
//save
$product = new Product;
$product->name = $request->input('name');
$product->description = $request->input('description');
$product->price = $request->input('price');
$product->cover_image = $fileName;
if($product->save()) {
return new ProductsResource($product);
}
}
How can I validate the base64 image? Is my procedure in saving the image coming from vue js is in correct way or is there a better way? please let me know. Thanks im just new to laravel and vue js hoping to learn more
You should add this function to your custom helper :
if (!function_exists('validate_base64')) {
/**
* Validate a base64 content.
*
* #param string $base64data
* #param array $allowedMime example ['png', 'jpg', 'jpeg']
* #return bool
*/
function validate_base64($base64data, array $allowedMime)
{
// strip out data uri scheme information (see RFC 2397)
if (strpos($base64data, ';base64') !== false) {
list(, $base64data) = explode(';', $base64data);
list(, $base64data) = explode(',', $base64data);
}
// strict mode filters for non-base64 alphabet characters
if (base64_decode($base64data, true) === false) {
return false;
}
// decoding and then reeconding should not change the data
if (base64_encode(base64_decode($base64data)) !== $base64data) {
return false;
}
$binaryData = base64_decode($base64data);
// temporarily store the decoded data on the filesystem to be able to pass it to the fileAdder
$tmpFile = tempnam(sys_get_temp_dir(), 'medialibrary');
file_put_contents($tmpFile, $binaryData);
// guard Against Invalid MimeType
$allowedMime = array_flatten($allowedMime);
// no allowedMimeTypes, then any type would be ok
if (empty($allowedMime)) {
return true;
}
// Check the MimeTypes
$validation = Illuminate\Support\Facades\Validator::make(
['file' => new Illuminate\Http\File($tmpFile)],
['file' => 'mimes:' . implode(',', $allowedMime)]
);
return !$validation->fails();
}
}
Then extend the base64_image validation in your AppServiceProvider in boot() method :
use Illuminate\Support\Facades\Validator;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
...
Validator::extend('base64_image', function ($attribute, $value, $parameters, $validator) {
return validate_base64($value, ['png', 'jpg', 'jpeg', 'gif']);
});
}
Now you can use it in your validation rules like this :
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'photo' => 'required|base64_image'
];
}
There is a crazybooot/base64-validation package that handles base64 validation.
For installation instructions and more details see:
https://github.com/crazybooot/base64-validation
I am using Ionic framework for mobile app development. The Yii2 API code below can be used for file upload, but it doesn't work. It shows the following errors:
i) Undefined offset: 0.
ii) yii\db\BaseActiveRecord->save()
public function actionNew() {
$model = new Apiprofile();
$userid = $_REQUEST['user_id'];
$photo = $_FILES['photo'];
$model->user_id = $userid;
$model->photo = $photo;
$name = $model->user_id;
$model->file = UploadedFile::getInstance($model, 'photo');
if($model->file) {
$model->file->saveAs('uploads/photos/'.$name.'.'.$model->file->extension);
$model->photo = $name.'.'.$model->file->extension;
$model->save();
}
$name = $model->user_id;
if($model->save()) {
echo json_encode(array('status'=>1,'data'=>$model->attributes),JSON_PRETTY_PRINT);
} else {
echo json_encode(array('status'=>0,'error_code'=>400,'errors'=>$model->errors),JSON_PRETTY_PRINT);
}
}
Hi if you want I will share my helper class that I used for working with images in Yii2 REST.
In the base folder of your application create folder components and inside of that folder create two folders helpers and objects.
-->assets
-->componests
----->helpers
----->objects
-->config
-->...
after that create class FileUpload inside of objects folder and put this code inside.
<?php
namespace app\components\objects;
class FileUpload
{
public $error=[];
public $isSuccess=false;
public $file;
public $ready_path;
public $file_type;
public $file_size;
public $file_extension;
public $file_tmp_name;
public $file_name;
public $save_path;
public function __construct($file,$save_path,$ready_path,$required=false)
{
if(!isset($_FILES[$file])){
if($required){
$this->error=['message'=>$file.' is required'];
}
return $this;
}
$this->save_path=$save_path;
$this->ready_path=$ready_path;
$this->file_type = strtolower($_FILES[$file]['type']);
$this->file_name = $_FILES[$file]['name'];
$this->file_tmp_name=$_FILES[$file]['tmp_name'];
$this->file_size = $_FILES[$file]['size'];
$this->file_extension=pathinfo($this->file_name, PATHINFO_EXTENSION);
}
public function setError($error){
if(empty($this->error)){
$this->error=$error;
}
}
}
Then inside of helpers folder create class FileUploadHelper and then put this code inside.
<?php
namespace app\components\helpers;
use app\components\objects\FileUpload;
use Yii;
class FileUploadHelper
{
private $allowed_files;
private $file_size;
const IMAGE='image';
const FILE='file';
/** File Upload
* #param string $file_name
* #param string $save_path
* #param string $ready_path
* #param string $type
* #param bool $required
* #return \app\components\objects\FileUpload
*/
public static function fileUpload($file_name,$save_path,$ready_path,$type,$required=false){
$image=new FileUpload($file_name,$save_path,$ready_path,$required);
if($type==self::FILE){
$allowed_files=Yii::$app->params['allowed_files'];
$file_size=Yii::$app->params['file_max_size'];
}else{
$allowed_files=Yii::$app->params['allowed_files'];
$file_size=Yii::$app->params['file_max_size'];
}
$dir = realpath(Yii::$app->basePath);
if(in_array($image->file_type,$allowed_files)
&&$image->file_size<$file_size){
$filename = $file_name.'_'.md5(uniqid(time()).time() . '_' . date('YmdHis')) . '.' . $image->file_extension;
$file = $dir . $image->save_path . $filename;
if(move_uploaded_file($image->file_tmp_name, $file)){
$image->file=$image->ready_path. $filename;
$image->isSuccess=true;
$image->setError(['message'=>'file_upload_success']);
}else{
$image->setError(['message'=>'error_try_again']);
}
}else{
$image->setError(['message'=>'file_should_be_no_more_than_given_size']);
}
return $image;
}
/** Delete File
* #param string $ready_file
*/
public static function deleteImage($ready_file){
$dir = realpath(Yii::$app->basePath);
if (strpos($ready_file, 'default') === false){
if(is_file($dir.'/web/'.$ready_file)){
unlink($dir.'/web/'.$ready_file);
}
}
}
}
That's all needed. Below I will give you example
public function actionEditAvatar($id)
{
$product = Product::findOne($id);
if( $product ){
$old_avatar=$product->avatar;
$image=FileUploadHelper::fileUpload(ProductForm::AVATAR,Yii::$app->params['product_path'],Yii::$app->params['product_ready_path'],FileUploadHelper::IMAGE);
if($image->isSuccess) {
$product->avatar = $image->file;
if($product->save()){
FileUploadHelper::deleteImage($old_avatar);
return $product->avatar;
}
}
return $image->error;
}
throw new NotFoundHttpException;
}
The code above from a real project. FileUploadHelper has two static classes which are "fileUpload" and "deleteImage".
FileUploadHelper requires fileUpload('file_name','save_path','ready_path','type')
'save_path' is where the file will be saved.
'ready_path' is how the ready URL should be like.
they are in Yii::$app->params[];
You can check if image succeeded or not by attribute isSuccess. If you have the error you can access them by attribute error. The ready file can be accessed by attribute file. You can delete image via static function deleteImage('saved_image_url'); All of them are used at above action. Please see it.
By the way, here the params that I used. Do not forget to to create folders inside web/uploads and change names of the folders like in config file.
return [
'adminEmail' => 'admin#example.com',
'allowed_images' => [ 'image/jpeg', 'image/gif', 'image/png' ],
'allowed_files' => [ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/msword', 'application/pdf','image/jpeg', 'image/gif', 'image/png'],
'image_max_size' => 2097152,
'file_max_size' => 8388608,
'owner_document_path' => '/web/uploads/owner_document/',
'owner_document_ready_path' => 'uploads/owner_document/',
'product_path' => '/web/uploads/product/',
'product_ready_path' => 'uploads/product/',
'complain_photo_path' => '/web/uploads/complain_photo/',
'complain_photo_ready_path' => 'uploads/complain_photo/',
'owner_path' => '/web/uploads/owner/',
'owner_ready_path' => 'uploads/owner/',
'staff_path' => '/web/uploads/staff/',
'staff_ready_path' => 'uploads/staff/',
];
I want to get image from the user and then rename it and after that i want to save the renamed image name into the database. here is my controller codes. im using intervention package. i can save the photo to the destination folder correctly after renaming it but i cant save the name of the photo into my database after renaming. what will be the code?
public function store(UserRequest $request)
{
$farmer = User::create([
'name' => $request->name,
'phone' => $request->phone,
'address' => $request->address,
'nid' => $request->nid,
'dob' => $request->dob,
'remarks' => $request->remarks,
'division_id' => $request->division_id,
'district_id' => $request->district_id,
'upazila_id' => $request->upazila_id,
'farmer_point_id' => $request->farmer_point_id,
'user_type_id' => 3 // 3 is for farmer
]);
$image = Image::make($request->profile_picture);
$image->resize(250, 272);
$image->save(public_path("uploads/Farmers/farmer_$farmer->id.jpg"));
return redirect("farmer/{$farmer->id}");
}
The ideal thing to do is to upload the image first and then save the file path to database.
Ideally, it will be better to extract your upload logic into a separate stand-alone class. You can use use the below as a guide.
<?php
Class UploadImage
{
/**
* UploadPostImage constructor.
* .
* #param UploadedFile $postedImage
*
*/
public function __construct(UploadedFile $postedImage)
{
$this->postedImage = $postedImage;
}
/**
* Create the filename
*
*/
public function getFilename()
{
$dt = Carbon::now();
$timestamp = $dt->getTimestamp();
$this->filename = $timestamp . '_' . $this->postedImage->getClientOriginalName();
}
/**
* Create the image and return the path
*
* #param $path
* #param int $width
* #return mixed
*/
public function createImage($path, $width = 400)
{
// Upload the image
$image = Image::make($this->postedImage)
->resize(250, 272);
$image->save(public_path($path . $this->filename, 60));
return $path . $this->filename;
}
}
In your controller, you can then call this class
$uploadImage = new Image(UploadedFile $file);
$uploadImage->getFilename();
$data['image'] = uploadImage->createImage('your-upload-path');
// Add other data into the $data array, then save to database.
$data['phone'] = $request->name,
$data['address'] = $request->address
// Add other data then pass it into User::create()
When you make a call to createImage() in your controller, the path is returned to you and you can save this in your database.
I hope this helps!
I want to click on an image opens the window for selecting a new image, and change the screen, now I want to change this picture in the bank, but this pending on an error:
Catchable Fatal Error: Argument 1 passed to
Delivve\WebBundle\Entity\User::setFile() must be an instance of
Symfony\Component\HttpFoundation\File\UploadedFile, string given,
called in
/home/delivve-webservice/src/Delivve/WebBundle/Controller/UserController.php
I do not know whether the error is in the path image that I'm taking?
$("#bundle_user_file").change(function () {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.active-img').attr('src', e.target.result);
ajax_formData(e.target.result);
};
reader.readAsDataURL(this.files[0]);
}
});
function ajax_formData(image) {
var path = "{{ path("submit_image_user", {"userId" : owner.id}) }}";
alert(image);
$.post(path, {image: image}, function (data) {
alert(data.message);
}, "json");
}
public function submitImageAction(Request $request, $userId){
$this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
$em = $this->getDoctrine()->getManager();
$entity = $this->getUser();
if ($entity->getId() != $userId) {
$response = new JsonResponse(
array(
'message' => "Não tem permissão de alterar esses dados"
), 400);
return $response;
}
if ($base64Content = $request->request->get('image')) {
$filePath = tempnam(sys_get_temp_dir(), 'UploadedFile');
$file = fopen($filePath, "w");
stream_filter_append($file, 'convert.base64-decode');
fwrite($file, $base64Content);
$meta_data = stream_get_meta_data($file);
$path = $meta_data['uri'];
fclose($file);
$entity->setFile($path);
$entity->upload();
$em->persist($entity);
$em->flush();
return new JsonResponse(array('message' => 'Success!'), 200);
}
$response = new JsonResponse(
array(
'message' => "imagem não encontrada"
), 400);
return $response;
}
these are the methods in my class
user.php
/**
* #return string
*/
public function getPictureUrl()
{
return $this->pictureUrl;
}
/**
* #param string $pictureUrl
*/
public function setPictureUrl($pictureUrl)
{
$this->pictureUrl = $pictureUrl;
}
/**
* Get file.
*
* #return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Set file.
*
* #param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Relative path.
* Get web path to upload directory.
*
* #return string
*/
public function getUploadPath()
{
return 'uploads/pictures';
}
/**
* Absolute path.
* Get absolute path to upload directory.
*
* #return string
*/
protected function getUploadAbsolutePath()
{
return __DIR__ . '/../../../../web/' . $this->getUploadPath();
}
/**
* Relative path.
* Get web path to a cover.
*
* #return null|string
*/
public function getPictureWeb()
{
return null === $this->getPictureUrl()
? null
: $this->getUploadPath() . '/' . $this->getPictureUrl();
}
/**
* Get path on disk to a cover.
*
* #return null|string
* Absolute path.
*/
public function getPictureAbsolute()
{
return null === $this->getPictureUrl()
? null
: $this->getUploadAbsolutePath() . '/' . $this->getPictureUrl();
}
/**
* Upload a cover file.
*/
public function upload()
{
if (null === $this->getFile()) {
return;
}
$filename = $this->getFile()->getClientOriginalName();
$this->getFile()->move($this->getUploadAbsolutePath(), $filename);
$this->setPictureUrl($filename);
$this->setFile();
}
the file I get comes from the type base64, I have to turn it into a uploadfile
$file = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAA..."
I would greatly appreciate it if someone could help me to do this submition of asynchronous file.
public function submitImageAction(Request $request, $userId)
{
$this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
$em = $this->getDoctrine()->getManager();
$entity = $this->getUser();
if ($entity->getId() != $userId) {
$response = new JsonResponse(
array(
'message' => "Não tem permissão de alterar esses dados"
), 400);
return $response;
}
if ($base64Content = $request->request->get('image')) {
$dat = preg_split("/,/", $base64Content);
if (($fileData = base64_decode($dat[1])) === false) {
$response = new JsonResponse(
array(
'message' => "Base64 decoding error."
), 400);
return $response;
}
$fileName = $entity->getUploadPath() . "/" . uniqid() . ".jpeg";
if (file_put_contents($fileName, $fileData)) {
$entity->setPictureUrl($fileName);
$em->persist($entity);
$em->flush();
return new JsonResponse(array('message' => 'Success!'), 200);
}
}
$response = new JsonResponse(
array(
'message' => "imagem não encontrada"
), 400);
return $response;
}
the error was in the driver only need to hit like sending the path to the User