Yii booster TbImageColumn - setting Image attributes - php

Is there any way to add other attributes to the image tag generated by TbImageColoumn ?
The documentation is not very clear on how to set image attributes like height, width etc. It only mentions on how to add image src using imagePathExpression attribute,
My current column looks like this
.....
array(
'class'=>'bootstrap.widgets.TbImageColumn',
'imagePathExpression'=>'$data->getImage("large")',
'usePlaceKitten'=>false,
),
.....

Digging through the code it seems there is an attribute for setting html attributes to the generated tag using the imageOptions and to set attributes on generated td cell tag we can use the htmlOptions array from the base clase
/**
* TbImageColumn widget class
*
* #package booster.widgets.grids.columns
*/
class TbImageColumn extends CGridColumn
{
/**
* #var array the HTML options of the image tag
*/
public $imageOptions = array();
so to limit image width to 50px the column should be modified to
.....
array(
'class'=>'bootstrap.widgets.TbImageColumn',
'imagePathExpression'=>'$data->getImage("large")',
'headerHtmlOptions'=>array('style'=>'min-width: 50px;'),
'imageOptions'=>array('width'=>'50px'),
'usePlaceKitten'=>false,
),
.....

Related

Is there a way to have $args suggestions when using a template part?

I work in a WP project using template parts, as they are multiple buttons instance in this project i created a button template that can take multiple arguments as such:
<?
/**
* Default button template
*
*/
$container_classes = $args["container_classes"]; //additional container classes
$button_type = $args['button_type']; // link | button | modal
$elem_classes = $args['elem_classes']; //additional classes for the button
$url = $args['url'] ?? null; //provides the url target
$text = $args['text'] ?? null; // text to display inside the button
$text_color = $args['text_color'] ?? "white"; // text color
$bg_color = $args['bg_color'] ?? 'cta';
$target = $args['target'] ?? "_self"; //target attribute
$icon = $args['icon'] ?? null; //If there is an icon, link to the icon
$modal_slug = $args['modal_slug'] ?? 'null'
?>
<div class="button-<?= $button_type ?> flex <?= $container_classes ?>">
{... All the button specifications and other logical checks ... }
</div>
I would now like to use this template in multiple sections. But with all those $args i would like to have suggestions when typing in the arguments in the get_template_part() function.
Is there a way of defining the possible $args in the template-part file as i've seen it being used in functions for instance ?
Exemple of predefined params for a function:
function get_template_part( $slug, $name = null, $args = array() ) {
/**
* Fires before the specified template part file is loaded.
*
* The dynamic portion of the hook name, `$slug`, refers to the slug name
* for the generic template part.
*
* #since 3.0.0
* #since 5.5.0 The `$args` parameter was added.
*
* #param string $slug The slug name for the generic template.
* #param string|null $name The name of the specialized template.
* #param array $args Additional arguments passed to the template.
*/
where the #param notation defines expected parameters.
I'm trying to have suggestion proposed by VsCode when using a template part and trying to figure out a way for the $args to reproduce the params behavior in the case of a function.

Laravel custom attribute in Model for Scaling image using Cloudinary URL

In my web app I am using Cloudinary for image storing. Image uploading is working properly but i want to create a custom attribute for image so when getting back the image url from the database with some modification with width and height.
The link of a image: https://res.cloudinary.com/wokong/image/upload/v1568570430/storyHeader/cxjir4g9tkaa8xepxodg.jpg
which is stored in database but when it fetch from the database, It should come with some scaling so that It wont take much time for website loading.
here is my StoryModel:
class Story extends Model
{
use Commentable, Searchable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'title',
'summary',
'content',
'created_at',
'story_statuses_id',
'image', 'language',
'likes',
'views',
'url_key',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'is_public' => 'boolean',
];
I dont understand how to use it anyone can help?
Cloudinary supports runtime image resizing
As per their documentation instead of this
https://res.cloudinary.com/wokong/image/upload/v1568570430/storyHeader/cxjir4g9tkaa8xepxodg.jpg
use
https://res.cloudinary.com/wokong/image/upload/w_100,h_100,c_fit/v1568570430/storyHeader/cxjir4g9tkaa8xepxodg.jpg
As you can see, i have added /w_100,h_100,c_fit/ after upload to instruct Cloudinary to do the resizing on the fly
w is for width, h is for height and c is for scale type to be used while cropping
You can find the documentation here
UPDATE 1
Something like this should do it
$default_url = "https://res.cloudinary.com/wokong/image/upload/v1568570430/storyHeader/cxjir4g9tkaa8xepxodg.jpg";
$width = 100;
$height = 100;
echo str_replace("/upload", "/upload/w_".$width.",h_".$height.",c_fit", $default_url);
UPDATE 2
Your model will look like
class Story extends Model
{
use Commentable, Searchable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'title',
'summary',
'content',
'created_at',
'story_statuses_id',
'image', 'language',
'likes',
'views',
'url_key',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'is_public' => 'boolean',
];
public function getImageForWebAttribute()
{
$width = 100; // You can keep this info in app config
$height = 100;
// Here i am assuming `image` is where you store Cloudinary url
return str_replace("/upload", "/upload/w_".$width.",h_".$height.",c_fit", $this->image);
}
}
and you can call it like
$story->image_for_web
Documentation for laravel custom mutators can be found here
Instead of saving the whole URL of the uploaded image in your database, you can save the public_id returned in the upload response.
Then you can just use Cloudinary's SDK to generate the transformation URLs for you, by passing the desired transformation parameters to the cloudinary_url() Helper function, along with the image's public_id.
For example, let's assume that you want to generate a URL of an image which its public_id is sample. In addition, you want that the image will be delivered with the best format possible, based on the client's device and browser. You would also like to use Cloudinary's automatic quality algorithm and scale the image down to a width of 500px while retaining its original aspect ratio.
You can achieve it by using the following code:
$url = cloudinary_url('sample', array(
'fetch_format' => 'auto',
'quality' => 'auto',
'crop' => 'scale',
'width' => 500
));
echo $url; will print the following:
http://res.cloudinary.com/<cloud_name>/image/upload/c_scale,f_auto,q_auto,w_500/sample
If you wish to generate the whole <img> tag using the SDK, you can utilize cl_image_tag() as an alternative to the URL only output of cloudinary_url().
Here's the link to the relevant section in Cloudinary's documentation with some more examples on how to use both cl_image_tag() and cloudinary_url().
Okay just an additional help for ...
Say for instance you attached $images to a view from the controller in laravel
you could generate fixed width and height on the fly via the url and attach to bootstrap grid system..
`
<div class="col-6 col-md-4 col-lg-3">
<a href="{{$image->image_url}}">
#php
$url = $image->image_url;
#endphp
<img class="img img-fluid" src="{{ str_replace("/upload", "/upload/w_350,h_220,q_70,c_scale", $url) }}" alt="">
</a>
</div>
`
You could also set this attributes of the image via the controller method using
Cloudder::show(Cloudder::getPublicId(), ["width" => $width, "height"=>$height])

How can I Overwrite PHP file in Wordpress?

I am customizing the following theme.
https://github.com/inc2734/snow-monkey
And I'd like to add font - weight 300 to noto sans.
https://github.com/inc2734/snow-monkey/blob/9c817ca3c5176101db23185838260e2739163ebe/resources/src/css/foundation/_body/_body.php
"enqueue_noto_sans_jp.php" consists of the following code, only font-weight 400 is loaded.
<?php
/**
* #package mimizuku
* #author inc2734
* #license GPL-2.0+
*/
namespace Inc2734\Mimizuku_Core\Helper;
/**
* Enqueue Noto Sans JP
*
* #return void
*/
function enqueue_noto_sans_jp() {
wp_enqueue_style(
'noto-sans-jp',
'https://fonts.googleapis.com/css?family=Noto+Sans+JP&subset=japanese',
[],
wp_get_theme()->get( 'Version' )
);
}
I tried changing "enqueue_noto_sans_jp.php" directly as follows, but it was returned to the original timing at the most.
function enqueue_noto_sans_jp() {
wp_enqueue_style(
'noto-sans-jp',
'https://fonts.googleapis.com/css?family=Noto+Sans+JP:300,500,700&subset=japanese',
[],
wp_get_theme()->get( 'Version' )
);
}
Can I overwrite this with functions.php of the child theme?
Thanks your Help.
John,
Looks like you're on the right track using a child theme, but you only need to change the css part to load in your other fonts and apply css to the page to make use of them.
There is a really good guide to doing that here: https://nudgethemes.com/wordpress-how-to-change-your-theme-font/

Wordpress suggest with key value pair in admin

I have a meta box in which there is a text box that grabs the list of custom post type's title. So far its working fine but the problem is that i don't find a way to implement key/value autocomplete using wordpress built-in suggest plugin. Here is my code
/**
* Return list of artists for autocomplete. .
*
* #since 1.0.0
*/
public function list_artists() {
// This only send the post titles not the id
foreach($result as $k => $v) {
echo $v . "\n";
}
// This doesn't work and sends the whole text as json string
$results = array(1 => 'Raheel', 2 => 'Zain', 3 => 'Vicky');
echo json_encode($results);
die();
}
/**
* Provide a dashboard view for the plugin
*
* This file is used to markup the public-facing aspects of the plugin.
*
* #link http://example.com
* #since 1.0.0
*
* #package Songs_Cloud
* #subpackage Songs_Cloud/admin/partials
*/
function show_album_meta_box() { ?>
<label for="artist">Artist</label>
<input type="text" id="artist" name="artist">
<script type="text/javascript">
jQuery(function($) {
$("#artist").suggest(ajaxurl + "?action=sc_list_artists", { delay: 500, minchars: 2 });
});
</script>
<?php }
I am not willing to use dropdown because there can be 1000s for artists and it won't look good in the admin section.
Is there any possibility to achieve this using suggest plugin or any other approach that i can use ?
I think i should utilize Select2
That way i will just bind the dropdown dynamically and load the select2 plugin so that it give a nice interface to the admin section rather the long dropdown list.

Changing default pagination in Yii to 50

Working with yii framework and was wondering if there was a way of changing the default pagination. Currently it's at 10 elements, and I would like to make it 50.
You can set pagination by
$dataProvider = new CActiveDataProvider('model name',
array('pagination' => array(
'pageSize' => 50,
),
)
);
CPagination->$pageSize Number of items in each page. http://www.yiiframework.com/doc/api/CPagination#pageSize-detail
In yii/framework/web/CPagination.php, change
const DEFAULT_PAGE_SIZE=20;
/**
* #var string name of the GET variable storing the current page index. Defaults to 'page'.
*/
to
const DEFAULT_PAGE_SIZE=50;
/**
* #var string name of the GET variable storing the current page index. Defaults to 'page'.
*/
This will adjust the page, and pagination accordingly.

Categories