Youtube video in laravel - laravel

I'm using Laravel Video Embed(https://github.com/Merujan99/laravel-video-embed) so i can get the embeded code from a youtube url.It works just fine but i want to know is there any possibility to change the width and height properties of the embeded video in the view ?
my code in the controller:
public static function getvideo($id)
{
$url = Blog::where('id',$id)->value('video_link');
return LaravelVideoEmbed::parse($url);
}
my view:
<div>
{!!App\Http\Controllers\HomeController::getvideo($blog->id)!!}
</div>

You can pass attributes to the embed like so:
$params = [];
$attributes = [
'height' => 100px,
'width' => 200px,
];
LaravelVideoEmbed::parse($url, ['YouTube'], $params, $attributes)
Which will work dynamically.

Related

Cannot get thumbnail in rss feed in laravel

I am using willvincent feed reader to parse rss feeds, But i cannot seem to get the thumbail of the images,
Here is my code
Route::get('feed', function(Request $request) {
$f = FeedsFacade::make('http://www.cbn.com/cbnnews/us/feed/');
// $results = [
// 'image' => $f->get_image_url(),
// ];
foreach($f->get_items(0, $f->get_item_quantity()) as $item) {
$i['title'] = $item->get_title();
$i['thumbnail'] = $item->get_thumbnail();
$i['description'] = $item->get_description();
$i['content'] = $item->get_content();
$i['link'] = $item->get_link();
$i['date'] = $item->get_date();
$results['items'][] = $i;
}
dd($results);
})->name('feed');
Thumbnail always return null, will appreciate anyone's help
Here is how we call image and other contents from rss feed
NOTE: To get image, it is important that the site you fetching rss feeds, use to provide image too in their feed. Else won't get image in your fetched feed (there might be some trick to still fetch image but I am not aware of it for now). For example, google news feed don't provide image so you won't get image from google news feed.
Here is example of one site who use to provide image.
//In your CONTROLLER file
$feed = Feeds::make('https://globalnews.ca/feed/');
$data = array(
'title' => $feed->get_title(),
'permalink' => $feed->get_permalink(),
'items' => $feed->get_items(),
);
return view('view_file', $data)
//OR in case you are calling array value to show on view then it would be like this
return view('view_file', array(
'name' => $var
), $data) //notice "$data" at end of array.
Now after done on controller part, this is how you will call in view file
#foreach ($items as $item)
//GET IMAGE
#if($enclosure = $item->get_enclosure())
<img src="{{$enclosure->get_thumbnail()}}">
#endif
//GET TITLE
<div class="news-title">{{ $item->get_title() }}</div>
//GET DESCRIPTION
{{$item->get_description()}}
//NOTE: this will bring html. TO show as output instead of html you can either use {!! !!) or "strip_tags" function. And "substr" function to show first 100 characters only as small description
{{ substr(strip_tags($item->get_description()), 0, 100) }}
//GET LINK/URL OF NEWS
Read More
#endforeach
Feed which whose example I shown and also reference
https://packagist.org/packages/willvincent/feeds

How to display multiple images that stored as array in laravel?

please help me to display image that stored as array in database
my controller:
( filename is my image column name in database)
public function store(Request $request)
{
$this->validate($request, [
'filename' => 'required',
'filename.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
// Start multiple image upload code
if($request->hasfile('filename'))
{
foreach($request->file('filename') as $image)
{
$name=$image->getClientOriginalName();
$image->move(public_path().'/images/', $name);
$data[] = $name;
}
}
// End multiple image upload code
$houses= new House();
$houses->division = $request->input('division');
$houses->city = $request->input('city');
$houses->area = $request->input('area');
$houses->owner_name = $request->input('owner_name');
$houses->house_name = $request->input('house_name');
$houses->type = $request->input('type');
$houses->from = $request->input('from');
$houses->rent = $request->input('rent');
$houses->phone = $request->input('phone');
$houses->address = $request->input('address');
$houses->description = $request->input('description');
$houses->filename=json_encode($data); **// This for image upload**
$houses->save();
return back()->with('success', 'Your House has been successfully');
}
image column
First array cast the filename column in your model. For more: Array & JSON Casting
protected $casts = [
'filename' => 'array',
];
After that, whenever you fetch filename from House model, laravel will automatically convert the json data to array. Loop through the array and display the images.
$house = House::find(1);
foreach($house->filename as $filename) {
echo public_path().'/images/', $filename;
}
If you want to retrieve then display the images stored as an array what I'd suggest is casting the filename attribute to json/array in the model.
You can do that in your House model like this:
class House extends Model
{
protected $casts = [
'filename' => 'array',
];
}
To display it in a view you can loop through the casted array:
#foreach($house->filename as $image)
<img src="{{ url('link/to/assets/') . $image }}"
#endforeach
See more in the docs about casting here:
https://laravel.com/docs/5.7/eloquent-mutators#array-and-json-casting
A side point on image uploads, you may want to give uploaded files a unique name rather than using the user's filename so there's no conflict if a user uploads a document with the same name twice.
Laravel handles this all for you in the store() method seen here: https://laravel.com/docs/5.7/requests#storing-uploaded-files. You can create your own names if preferred using storeAs().

Drupal 7 - Trying to add form to list view

sorry if this has been asked before, I looked around but haven't found this specific question on StackOverFlow.com.
I have a view called 'view-post-wall' which I'm trying to add the form that submits posts to this view called 'post' via ajax submit, though I haven't begun adding ajax yet.
My module's name is 'friendicate'
I don't understand what I'm missing here, I'm following a tutorial and have been unable to get past this issue for 2 days now.
I don't get any errors either.
Here is the module code in full
function _form_post_ajax_add() {
$form = array();
$form['title'] = array(
'#type' => 'textfield',
'#title' => 'Title of post',
);
$form['body'] = array(
'#type' => 'textarea',
'#title' => 'description',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit post',
);
return $form;
}
function post_ajax_preprocess_page(&$variables) {
//krumo($variables);
$arg = arg();
if($arg[0] == 'view-post-wall') {
$variables['page']['content']['system_main']['main']['#markup'] = drupal_render(drupal_get_form('_form_post_ajax_add'));
}
}
There are multiple ways to accomplish this, and I'll outline those methods below. Also, if nothing works from my suggestions below, it's possible that you have an invalid form function name. Im not sure if that throws off Drupal or not. The correct format for the function name should end in _form and contain the arguments $form and $form_state, like so:
_form_post_ajax_add_form($form, &$form_state) { ... }
Also, if you want to use a hook, Steff mentioned in a comment to your question that you'll need to use your module name in the function name.
friendicate_preprocess_page(&$variables) { ... }
Ok, now for a few ideas how to get the form on the page.
Block
You can create a custom block within your module, and then assign it to a region in admin/structure/blocks
<?php
/**
* Implements hook_block_info().
*/
function friendicate_block_info() {
$blocks = array();
$blocks['post_ajax'] = array(
'info' => t('Translation Set Links'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function friendicate_block_view($delta = '') {
$block = array();
if ($delta == 'post_ajax') {
$form = drupal_get_form('_form_post_ajax_add_form');
$block['content'] = $form;
}
return $block;
}
Clear the cache and your block should appear in admin/structure/blocks
Views attachment before/after
You can add markup before and after a view using the Views hook hook_views_pre_render()
<?php
/**
* Implements hook_view_pre_render().
*/
function frendicate_views_pre_render(&$view) {
if($view->name == 'view_post_wall') { // the machine name of your view
$form = drupal_get_form('_form_post_ajax_add_form');
$view->attachment_before = render($form);
}
}
Or maybe use view post render
function friendicate_views_post_render(&$view, &$output, &$cache) {
//use the machine name of your view
if ($view->name == 'view_post_wall') {
$output .= drupal_render(drupal_get_form('_form_post_ajax_add'));
}
}

how to validate image dimension before insert into database in laravel

Iam new in laravel .im trying to validate dimensions of image .i want dimensions minimum(width=100,height=50).iam using validation code in controller.php is here
'galimg'=>'required|max:200kb|DimensionMin(300,300)|Mimes:jpeg,jpg,gif,png
,pneg'
but DimensionMin(300,300) is not work....i think custom validation rule is possible ..but i dont know how to use it ?and where ? this is my controller.php code
public function getgallery()
{
$validate=Validator::make(Input::all(),array(
'galname'=>'required|max:20',
'galimg'=>'required|max:400kb|Dimensionmin(300,300)|Mimes:jpeg,jpg,gif,png
,pneg'));
if($validate->fails())
{ return Redirect::route('getgallery')
->withErrors($validate)->withInput(); }
else
{ $max_image = 3;
if(ForumGallery::all()->count() < $max_image)
{ $file=Input::file('galimg');
$filename=$file->getClientOriginalName();
$file->move('uploads',$filename);
ForumGallery::create(['galname'=>Input::get('galname'),
'galimg'=>$filename]);
return Redirect::route('addgallery');
}
else
{return Redirect::route('gallery')
->with('success','Max Image Upload Reached!');
} }}
you can use this code
function getImgSize(imgSrc) {
var newImg = new Image();
newImg.onload = function() {
var height = newImg.height;
var width = newImg.width;
alert ('The image size is '+width+'*'+height);
}
newImg.src = imgSrc; // this must be done AFTER setting onload
}`
Here you get Image height and width compare it with your required dimention.
refer http://tiku.io/questions/4060613/how-to-validate-image-size-and-dimensions-before-saving-image-in-database
$v = Validator::make($data, array(
'email' => 'required|email',
'games' => 'required|numeric',
));
Assume you have some other fields to validate as well. Put them in $v.
Now you need to add custom validation rules.
$v->sometimes('galimg', 'required', function($input)
{
//psudo code, here validate your image
if($input.length >= 300) return false;
return true;
});
Put image-related manipulation into the function.
Hope this helps.
$validate=Validator::make(Input::all(),array(
'galname'=>'required|max:20',
'galimg'=>'required|max:400kb|Mimes:jpeg,jpg,gif,png
,pneg'));
$validate->sometimes('galimg', 'required', function($input)
{
//psudo code, here validate your image
return imagesx($input) > 300 && imagesy($input) > 300;
});
You can use this awesome library for detecting your image dimension here
afte done installing you can use it in your controller like this one :
$validate=Validator::make(Input::all(),array(
'galname'=>'required|max:20',
'galimg'=>'required|mimes:jpeg,jpg,gif,png,pneg|image_size:1200,800'));
the rules should be 1200 wide and 800 tall or width = 1200 and height = 800
Note : the dimension is in pixels
hope it helps.

Magento category edit page and ajax

I am adding some extra html to some input attributes in the category edit area by creating a new attribute input renderer type in my modules mysql install script:
$installer->addAttribute(
'catalog_category',
'mymodule_category_popularity',
array(
'label' => 'My Label',
'group' => 'My Group',
'type' => 'int',
'class' => 'validate-number',
'required' => false,
'default' => 50,
'input_renderer' => 'mymodule/adminhtml_catalog_category_widget_slider',
)
);
And the renderer:
class Mynamespace_Mymodule_Block_Adminhtml_Catalog_Category_Widget_Slider extends Varien_Data_Form_Element_Text
{
public function getAfterElementHtml()
{
$html = parent::getAfterElementHtml();
return $html." <div id=\"slider\"></div>";
}
}
And the javascript:
var $j = jQuery.noConflict();
$j(document).ready(function(){
$j( "#slider" ).slider();
});
This works fine, the plugins get loaded correctly and so does jQuery (as I have loaded it using my layout xml for the module). I see a slider. However, after the page has completely finished (the Magento ajax has re-loaded the category edit tabs) my slider dissapears. This, I assume, is because the javascript code is not getting ran again.
I have tried adding my javascript to the core edit.phtml (as there is other Magento JS there) however I just get an error: $j('#slider).slider() is not a function. This is because the content is getting loaded via ajax.
So, how do I make sure that I can use $j('#slider).slider() in the category edit area? Confusing I know.
I accomplished this by creating a javascript function:
function loadedInput() {
$('#slider').slider();
}
And calling that function in the attribute renderer:
class Mynamespace_Mymodule_Block_Adminhtml_Catalog_Category_Widget_Slider extends Varien_Data_Form_Element_Text
{
public function getAfterElementHtml()
{
$html = parent::getAfterElementHtml();
return $html." <div id=\"slider\"></div><script>loadedInput();</script>";
}
}

Resources