Change Vimeo/Youtube default size in Wordpress - php

How to change Vimeo/Youtube embed sizes?
Currently the default embed is very small 300x150, i need 540px minimum :(
Using snippet to filter in functions.php, the code no works.
function rezzz_embed_defaults($defaults) {
$defaults['width'] = 350;
$defaults['height'] = 200;
return $defaults;
}
add_filter('embed_defaults','rezzz_embed_defaults');
Please see screenshot -> http://i.imgur.com/kv8SPEA.png

Please see your code carefully. I have used your code but changed width and height.
function rezzz_embed_defaults($defaults) {
$defaults['width'] = 540;
$defaults['height'] = 304;
return $defaults;
}
add_filter('embed_defaults','rezzz_embed_defaults');
You need to provide width and height in proper ratio to use video from youtube, vimeo and other source.
You can see working demo of above code here

Related

How to set or apply a function in wordpress for a specific screen width?

I'm struggling with applying the wordpress function for a specific screen width. Below is the given php function which I wish to apply for a particular screen width:
function add_specific_menu_atts( $atts, $item, $args ) {
$menu_items = array(4167, 4181, 4359, 4407, 4413, 4423, 4440);
if (in_array($item->ID, $menu_items)) {
$atts['data-toggle'] = 'dropdown';
}
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_specific_menu_atts', 10, 3 );
I would really appreciate the much-needed help.
The main issue is that php does not have access to the screen width.
It would help if we knew what it is that you want to accomplish.
Also keep in mind that the user can resize the browser window after the page has loaded (or rotate the device) and php already finished it's job. Therefore, probably you are looking for a Javascript solution. This means, run the php function and set up some attributes or CSS classes and then have Javascript change things using those CSS classes.
I will assume that you use jQuery and have a menu in which you want certain elements to be hidden when the screen width is smaller than a threshold, say 800px.
Maybe this will help:
$(function() { // DOM is loaded
function resize() {
var bodyWidth = $('body').width();
if (bodyWidth < 800) {
$('.hide-on-mobile').hide();
} else {
$('.hide-on-mobile').show();
}
}
$(window).resize(function() {
resize();
});
resize(true); // initial call
});

How to make pdf exact 10CM width with DOMPDF and Laravel?

I am creating a PDF with DOMPDF and laravel.
The pdf is being printed with a special printer that only accepts files with 10CM width and 20CM height.
I have tried this:
$customPaper = array(0,0,720,1440);
$pdf = PDF::loadView('pdf.retourlabel',compact('retour','barcode'))->setPaper($customPaper);
Since 11X17 is
"11x17" => array(0, 0, 792.00, 1224.00),
I figured 10X20 was 0,0720,1440
But it's not working.
Any help is appreciated.
Thanks in advance
How i fixed this:
change the custom paper.
Download the PDF open in Acrobat Reader move your mouse to the left corner now you can see the width and height of the document, and i changed the custom paper accordingly.
The end result was:
10CM X 20CM =
$customPaper = array(0,0,567.00,283.80);
$pdf = PDF::loadView('pdf.retourlabel', compact('retour','barcode'))->setPaper($customPaper, 'landscape');
Very circuitous work but i did get the Job done..
Thanks
Setting the paper size in PHP requires knowing the point (pt) measurement, points being the native PDF unit. The PDF resolution (with Dompdf) is 72pt/inch. So 10cm x 20cm is roughly equivalent to 283 X 566 (as you noted in your answer).
1 inch = 72 point
1 inch = 2.54 cm
10 cm = 10/2.54*72 = 283.464566929
20 cm = 10/2.54*72 = 566.929133858
landscape is mean opposite. So, we can set it like : array(0, 0, 566.929133858, 283.464566929 ) which same as the answer but in more precise value
You can, however, allow Dompdf to calculate the appropriate point size by specifying your page dimensions in CSS. This is available starting with Dompdf 0.6.2.
<html>
<head>
<style>
#page { size: 10cm 20cm landscape; }
</style>
</head>
<body>
...
</body>
</html>
Trivia: the PDF spec does not provide for a method of indicating paper orientation (though there is a method of indicating a rotation). Dompdf just flips the width/height when landscape orientation is specified.
I think the issue is with orientation, since the setPaper uses 'A4' orientation as default so this might be the reason that your code is not working.
/**
* Set the paper size (default A4)
*
* #param string $paper
* #param string $orientation
* #return $this
*/
public function setPaper($paper, $orientation = 'portrait'){
$this->paper = $paper;
$this->orientation = $orientation;
$this->dompdf->setPaper($paper, $orientation);
return $this;
}
Try using :
$customPaper = array(0,0,720,1440);
$pdf = PDF::loadView('pdf.retourlabel',compact('retour','barcode'))->setPaper($customPaper,'portrait');
Hope that helps or try other options too instead of portrait.

Determine if YouTube video is widescreen?

I would like to know with certainty if a YouTube video is widescreen or not using the v3 API. There are many old videos that have a 4:3 ratio, so I need to detect this.
This was possible with API v2, but it is officially retired now. Here are the API v3 docs.
An API call looks something like this:
https://www.googleapis.com/youtube/v3/videos?id=[VIDEOID]&part=snippet&key=[DEVELOPERKEY]
Also, the thumbnail data always returns dimensions of 4:3, so that doesn't help. Here is an example:
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/nnnnnnnnn/default.jpg
[width] => 120
[height] => 90
)
...
)
Any ideas?
(I'm currently hacking this by analyzing pixels in the thumbnails where tell-tale black bars on 4:3 videos will be.)
Here is a sample video in 4:3 ratio:
https://www.youtube.com/watch?v=zMJ-Dl4eJu8 (old martial arts video)
and one in 16:9:
https://www.youtube.com/watch?v=7O2Jqi-LhEI (a new workout video)
Update: One promising suggestion was to explore fileDetails.videoStreams[].aspectRatio but it seems that this is only available to the video owner. Otherwise requesting fileDetails results in
The request cannot access user rating information. This error may occur because the request is not properly authorized
If you're open to using a different method other than V3 of the API, then I believe it is possible via the oEmbed API.
http://www.youtube.com/oembed?url={VIDEO_URL}&format=json
Like so:
http://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=zMJ-Dl4eJu8&format=json
Would produce:
{
"provider_url":"https:\/\/www.youtube.com\/",
"thumbnail_url":"https:\/\/i.ytimg.com\/vi\/zMJ-Dl4eJu8\/hqdefault.jpg",
"thumbnail_height":360,
"height":344,
"type":"video",
"version":"1.0",
"html":"\u003ciframe width=\"459\" height=\"344\" src=\"https:\/\/www.youtube.com\/embed\/zMJ-Dl4eJu8?feature=oembed\" frameborder=\"0\" allowfullscreen\u003e\u003c\/iframe\u003e",
"author_name":"hadronica2",
"width":459,
"provider_name":"YouTube",
"author_url":"https:\/\/www.youtube.com\/user\/hadronica2",
"title":"Aikido - Kazuo Chiba sensei - 1\u00ba part",
"thumbnail_width":480
}
In the examples you've given, the output was as follows:
http://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=zMJ-Dl4eJu8&format=json
Width: 459
Height: 344
Ratio: w/h = 1.3343 = 4:3 (ish)
http://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=zMJ-Dl4eJu8&format=json
Width: 480
Height: 270
Ratio: w/h = 1.7777 = 16/9
This appears to work in the examples you've provided.
Here is the abridged version that I have been using since v2 of the API retired.
It tests a few points on the top and bottom of the default.jpg thumbnail image of a given video where black bars might be. A vertically opposite point from a top point is tested to see if those pixels are similar to each other to within some delta. This is repeated for a few more points.
function isWidescreen($video = null) {
// LOGIC:
// 4:3 videos will have default.jpg with no top black bars
// 16:9 videos will have black top and bottom borders on default.jpg
// Get the default thumbnail (may have black bars on top and bottom)
$response = self::accessCurlObj()->get("https://i.ytimg.com/vi/{$video}/default.jpg");
$defaultImgRes = imagecreatefromstring($response);
$samplePoints = array(array(20,2), array(40,4), array(60,6), array(80,8));
// Scan a few points for equality between top and bottom
$height = imagesy($defaultImgRes);
foreach($samplePoints as $point) {
// Top
$rgbTop = imagecolorat($defaultImgRes, $point[0], $point[1]);
$colorsTop = imagecolorsforindex($defaultImgRes, $rgbTop);
// Bottom
$rgbBottom = imagecolorat($defaultImgRes, $point[0], $height - $point[1]);
$colorsBottom = imagecolorsforindex($defaultImgRes, $rgbBottom);
// If these arrays are not close, then let's call this 4:3 aspect
if(!$this->areArraysClose($colorsTop, $colorsBottom, 20)) {
return false;
}
}
// Default to widescreen
return true;
}
// Determine if the numeric values in the RGBA array are within some delta from each other
function areArraysClose(&$a, &$b, $delta = 10) {
foreach($a as $key => $val) {
if(abs($val - $b[$key]) > $delta) {
return false;
}
}
return true;
}
This seems to be working sufficiently enough. An obvious improvement is to check if the pixels are close to black, or apply some image processing to remove black bars automatically then check the dimensions of the remaining image.
However, my hope was that a domain-knowledgeable SO member would have a better solution before going deeper down this rabbit hole... and someone came through.

Laravel 4 Intervention package gets pixelated images when use fit() method

I'm trying to resize an image using the method fit, and although I get the correct proportions, the result has an awful pixelated effect. How I can prevent this?
if ($imgHeight >= $imgWidth) {
$img->fit((int) ($value[0] * $ratio), $value[0]);
} else {
$img->fit($value[0], (int) ($value[0] / $ratio));
}
The original image is:
https://www.dropbox.com/s/gs7z4s4jg1x6k6o/gallery-portraits-of-strangers-18.jpg?dl=0
And I take this:
https://www.dropbox.com/s/q9l17sf3r5x02fn/63_big.jpg?dl=0
Sorry, I don't have enough reputation to post pics.

Set Base Image Programmatically

I've got this problem that I can't solve. Partly because I can't explain it with the right terms. I'm new to this so sorry for this clumsy question.
Below you can see an overview of my goal.
I am trying to sell Similar product in my magento for this i wrote some code that every thing is working fine..
But i have only one problem with images..
that is I am getting all images of current product but the base image is not selected, How can i set the very first image as base image Programmatically.
Any ideas ?
Hello You can do as follows :
$image =$imagePath."image.png";
$product->setMediaGallery(array('images'=>array (), 'values'=>array ()));
if(is_file($image))
{
$product->addImageToMediaGallery($image, array ('image', 'small_image', 'thumbnail'), false, false);
}
i.e you need to first set the media gallery, P.S This is a necessary step.
then add all images to gallery using addImageToMediaGallery where 'image' ref to 'base_image'
i.e in above example we are setting image.png to base_image, small_image and thumbnail image in a single call.
hope this helps you.
I've achieved the same result by using:
$product->setSmallImage($path)
->setThumbnail($path)
->setImage($path)
->save();
Works better for the case where your media gallery has one or more pictures in it.
I'm doing
$product->load();
$gallery = $product->getMediaGalleryImages();
$paths = array();
foreach ($gallery as $image) {
$paths[] = $image->getFile();
}
sort($paths);
$path = array_shift($paths);
try {
$product->setSmallImage($path)
->setThumbnail($path)
->setImage($path)
->save();
} catch (Exception $e) {
echo $e->getMessage();
}
Which gets all the product images, sort's them by file name and sets the first to the product primary image. As I ran a import and added all my pictures, but didn't set the primary image.
To grab a set or "broken" products I used:
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('small_image', array('eq' => ''));

Categories