How to render a Youtube thumbnail in a template in Drupal 7 - php

I am using the following modules:
media
media_youtube
Styles
and would like to render a thumbnail of a Youtube video in a template (.tpl). Which theme function should I use and with what params?
My best quess would be something like:
$my_media['style_name'] = 'unlinked_thumbnail';
print theme('file_styles',$my_media);
where $my_media is an array containing fid,uri,filename,filemime etc.
Since i'm very new to Drupal, all my attempts to make sense of the modules source code have failed. I feel like I have tried all possible combinations of style names defined in the youtube and styles module without getting any output.
Rendering the video itself however works just fine using
print theme('media_youtube_video',$my_media);
How do you guys do it?

Digging around in the media_youtube module code there's a function that will build this up for you in includes/media_youtube.formatters.inc called media_youtube_file_formatter_image_view(). You can use code like the following to render the thumbnail image:
// Make sure the correct include file is loaded
module_load_include('inc', 'media_youtube', '/includes/media_youtube.formatters.inc');
// Load the file
$file = file_load($my_media['fid']);
// Set up the settings array with your image style
$display['settings'] = array('image_style' => 'unlinked_thumbnail');
// Get the render array for the thumbnail image
$image_render_array = media_youtube_file_formatter_image_view($file, $display, LANGUAGE_NONE);
// Render it
print render($image_render_array);

One more example, which makes possible to render video thumbnail (vimeo, youtube, qbrick, etc.) with your custom image style.
$file - File object that you can easily get from media field.
$wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
$image_uri = $wrapper->getLocalThumbnailPath();
$image_rendered = theme('image_style', array(
'style_name' => 'YOUR_IMAGE_STYLE_HERE',
'path' => $image_uri
));
Please note, that if you want to render also image, than you need to check $file->type if it's video or image, because it will use different wrapper objects with different methods.

And if you want to print both thumb+video you could do this :
<?php print render($content['field_your_field']); ?>
If you in a tpl and want to use $variables - it will be something like:
<?php print render($variables['content']['field_url']['#object']->content['field_video']);?>
Hopu u can use it...

Related

Export rendered html files as zip from modx

First of all my target is to write an Extras for modx to export website contents as epub file.
I searched already if something like that exists but I did not find anything. Does anyone know extras like that? Or can anyone suggest me the best way to do it in modx?
My thought is to gather all rendered html files and resources and then use an php based epub library to generate epub file.
But I did not find a way to get the rendered html files from modx.
I can get the template and I can also get the html snippet code but I need the whole html file.
MODX info:
MODX-Version: MODX Revolution 2.7.3-pl (traditional)
Versions-Codename: Revolution
There is multiple ways to go about this.
You could write a standalone PHP application that calls all the URLs on your site from an XML sitemap or an array of links and then downloads the rendered HTML using the file_get_contents() function (see doc here).
Something along the lines of this (following code is untested):
// [filename] => [URL]
$pages = array(
'index.html' => 'https://example.com/',
'contact.html' => 'https://example.com/contact.html',
);
foreach($pages as $filename => $link){
$filePath = $_SERVER['DOCUMENT_ROOT'].'/'.$filename;
$html= file_get_contents($link);
$handle = fopen($filePath,"w");
fwrite($handle,$html);
fclose($handle);
}
Alternatively you could write a snippet within MODX and get the resource URLs directly using xPDO. The following code will get the links and file names for all resources and exclude weblinks, symlinks and static resources in the process. If you're planning on implementing the following code within your package, you will need to adjust it slightly.
$resources = $modx->getIterator('modResource', array(
'class_key' => 'modDocument',
));
$pages = array();
foreach($resources as $resource){
$pages[$resource->get('alias').'.html'] = $modx->makeUrl($resource->get('id'), '', '', 'full');
}
Output of the $pages array in the above code would be as follows:
Array
(
[index.html] => https://example.com/
[test.html] => https://example.com/test.html
)

How display img without the file extension

I want to show an img like this:
http://picviewer.umov.me/Pic/GetImage?id=92013681&token=ee103380297bbb2df0d8855949d791df
How should i use php to show the img with dynamic parameters?
You need to set the proper content type headers and then stream the binary data.
$imagepath = '/path/to/file';
header("Content-type: image/jpeg");
echo file_get_contents( $imagepath );
First i'll explain what does that URL do: it's configured to point to a script, or class->function, that manages the searching in the database, for the real path, the path you want to hide for the user, then returns the image to the request.
That said, in two steps, what you have to do is the following.
Check how to customize your url, this could be a start:
How to create friendly URL in php?
create your custom url pointing to a script or class/function like this:
Return a PHP page as an image
Thats, all... Assuming that you already have the images/database covered of course. if not, well you'll have to make the necessary database and tables...

get image style height in pre process function in Drupal 8

I use Drupal 8 with the awesome inline responsive images module. I want to make changes to the img field (the fallback image) before the <picture> element is rendered, more specifically: I need to add the width and height parameters to the <img> field. I therefore use the preprocess_image hook.
This hook provides me with a bunch of variables, most notably $variables[attributes].
$variables[width], $variables[height] and $variables[uri] are all empty strings for some reason. Fortunately $variables[attributes] contains:
$variables[attributes][data-entity-uuid] and $variables[attributes][srcset] so at least I have path to the styled image and the uuid to the original image.
I figured there are two ways to get to where I want to go (that is load the styled image and get height and width):
convert the path to an uri (or is it path)?
Get the file id from the uuid and then somehow get the uri from the styled image (which seems like a detour to get what I want)
I can't get option 1 to work. The path in srcset is like this:
/sites/default/files/styles/image_lightbox/public/inline-images/erf-2.jpg?itok=4_EU9Ttx and I think I need to convert that to public://styles/image_lightbox/public/inline-images/erf-2.jpg but got stuck at something like:
$parsed_url = parse_url($variables['attributes']['srcset']);
$path = file_build_uri($parsed_url['path']);
but that still left the /sites/default/files part in there
I can't get option 2 to work. I'm stuck at:
$file_array = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uuid' => $img_uuid]);
$file_id = reset(array_keys($file_array));
$file = File::load($file_id);
$image_uri = ImageStyle::load('image-lightbox')->buildUrl($file->getFileUri());
$image = \Drupal::service('image.factory')->get($image_uri);
This fails at $file = File::load($file_id) for some reason
After wasting 8 hours of my life in getting this solved I would be very grateful with any help
The answer was much simpler than I thought.
Option 1 turned out to be very easy:
Instead of converting /sites/default/files/styles/image_lightbox/public/inline-images/erf-2.jpg?itok=4_EU9Ttx (from $variables[attributes][srcset]) to public://styles/image_lightbox/public/inline-images/erf-2.jpg. I had to convert it to http://hostname/path-to-drupal-install/sites/default/files/styles/image_lightbox/public/inline-images/erf-2.jpg?itok=4_EU9Ttx which I did like this:
global $base_url;
$image_uri = $base_url.$variables['attributes']['srcset'];

Zend Framework: PHP script in img src tag causing broken image

I'm using the JpGraph library within a Zend Framework project of version 1.11. I'm trying to display the graph of some data using an img tag by including a php script in the src field, as so:
echo "<img src='vgraph.php?param1=val1&param2=val2...
When I do this, the only thing displayed as an image is the my browser's default broken image. Since typical URLs in Zend are treated as controller-action pairs, the path to the file I am including is also treated as a controller-action pair. To investigate why I wasn't getting my image, I viewed the source of the page in the browser. When I clicked on the link for the above img tag, I was directed to a page containing the error:
Invalid controller specified (vgraph.php)
I don't by any means intend to use the script as a controller. It is only supposed to display the image of a graph. I am unsure about how to make sure that Zend only uses the file's output as an image, and not to use its path as a URL.
Here is the file I am using for the image, vgraph.php:
<?php
/* graph the vital data */
require_once( 'path/to/Zend/project/web/library/jpgraph/jpgraph.php' );
require_once( 'path/to/Zend/project/web/library/jpgraph/jpgraph_line.php' );
/* get the needed parameters */
$id = $_GET['param1'];
$type = $_GET['param2'];
$from = $_GET['param3'];
$to = $_GET['param4'];
$graph = new Graph( $width, $height );
/* create the data and plot */
$obj = new CcFunc_VitalSigns();
$vitals = $obj->getVitalData( $id, $type, $from, $to );
/* configure everything */
$graph->setScale( 'intint' );
$graph->title->Set( "Graph of " . $type );
$graph->xaxis->title->Set( "X-Axis" );
$graph->yaxis->title->Set( "Numerical Value" );
$lp = new LinePlot( $vitals );
$lp->setColor( 'blue' );
$lp->setWeight( 5 );
/* add the plot to the graph */
$graph->Add( $lp );
/* display the graph */
$graph->Stroke();
NOTE: I've tried other solutions, such as streaming the graph to a png file and letting the src field be the URL of that file, but I was met with the same problem, even after adding a RewriteRule to apache to prevent redirection of .png files. I've settled upon this method as the most comfortable (potential) solution.
EDIT: I didn't say this before, but the code where the img tag is echo'd is not a controller, and is actually located in path/to/Zend/roject/web/application/scripts/views. The script vgraph.php is located in path/to/Zend/project/web/library.
You need to move the vgraph.php file to your public folder for this to work, the browser isn't able to view files outside of that.
Move it to path/to/Zend/project/web/public/vgraph.php, change your img tag to <img src="/vgraph.php?param1=val1&param2=val2... and everything should work as you expect.
When linking try in the view to specify the full path as:
<?php echo $this->baseURL();?>
Zend View Helpers
This will return localhos/[APP]/
Hope this helps
Try to put everything in a Graph controller within a generateAction method and you can link to it:
Generate

Need to create dynamic thumbnail grid.

I am new in web development, i need to create a website for portfolio purposes in which i need to show thumbnail and a little description of my project underneath it and all content should be dynamically displayed. I have basic knowledege of PHP, wordpress, javascript, jquery, python, and HTML/CSS so this i am doing for learning purpose.
I want you guys to please tell me the way how can i do it, just guide me rest of it i will handle.
Some similar examples are
http://themes.themepunch.com/?theme=megafoliopro_jq
http://codecanyon.net/item/dzs-scroller-gallery-cool-jquery-media-gallery/full_screen_preview/457913?ref=ibrandstudio
I am new to this forum and expect someone will answer my question
Thanks a lot mates.
Download CMS Made Simple and get either the album or gallery module. This is one out of many ways to get the job done.
You could glob() a directory to generate your thumbnails using PHP. I use this method on my photography site:
<?php
$counter = 0; // Set counter to 0
foreach (glob("images/photo-strip/thumb/*.jpg") as $pathToThumb) { // Grab files from the thumbnail path and save them to variable
$th_filename = basename($pathToThumb); // Strip the thumbnail filename from the path
$filename = str_replace('th_', '', $th_filename); // Strip th_ from the filename and save it to a different variable
$pathToFull = 'images/photo-strip/' . $filename; // Rebuild the path to the full-size image
echo ("<section class=\"photo-box\"><img src=\"$pathToThumb\" /></section>"); // Echo the photobox
$counter++; // Increment counter by 1 until no file exists
}
?>
You could expand on this code some in order to generate your "captions" perhaps even style your captions off a title="" property inside the <img> tag. How you match those captions up to the file is up to you.

Categories