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'];
Related
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...
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.
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...
I wrote a simple image randomizer on PHP that picks a random image from a list using the rand() function. The code works perfectly, and a random image is generated when I include it on my html as a picture.
The problem comes when I try to include it twice in the same html. A random image WILL be generated and displayed for both times I included it, but it will be the same image. In other words, I get a repeated random image on my page.
An easy way to solve this is to simply copy the randomizer.php, give it a new name, and include both images in HTML. The reason I don't want to do this is because my final HTML will have about 25 pictures, and I simply feel like there should be a better way to do this. Keep in mind that I CANNOT add any PHP functions into my HTML, given that my files are hosted in different servers, and my HTML server does not support PHP.
If anyone know of a better fix other than creating 25 copies of my randomizer.php file (or creating 25 different files that include it), please let me know. I will most definitely appreciate your input!!
Thank you very, very much!!
Here's a snippet of the code:
if (count($fileList) > 0) {
do { //do-while loop will get a new random image until that image has not been used yet in this session
$imageNumber = rand( 0 , ( count($fileList) - 1) ); //get random image from fileList
$iterations++;
} while( !(empty($_SESSION['img' . $imageNumber])) && iterations < 200);
$_SESSION['img' . $imageNumber] = True; //this image number has been displayed
$_SESSION['shown']++; //increments the number of shown pictures in this signature
$img = $folder.$fileList[$imageNumber];
}
It may be that the browser thinks it is the same image and is caching, try setting the name of the image (emit a header with content-disposition/filename IIRC) and/or adding a unique tag to the end of the image name with a random string, ( e.g. image.jpg?e0.6613725793930488 )
My guess is that rand() either didn't reseed, or is seeded with the same value.
Have you considered calling srand() - or "the better random number generator" combination of mt_srand() and mt_rand()?
I need some help with screen scraping a site (http://website.com).
Lets say I'm trying to get an image inside <div id="imageHolder">
But when I pull it down, it's path is relative ie "image_large/imageName.jpg" (I'm going to pull down this image daily as it changes daily. It always begins with "images_large/.
How can I go in and prepend the url website.com to that image inside <div id="imageHolder">?
$url = 'http://www.website.com/';
// screen scraping here
// say you have your image you scraped stored in the variable below
$img = 'images_large/imageName.jpg';
// your new full path to the image
$fullpath = $url . $img;
// test it (just to see for yourself)
echo $fullpath;
This is just making basic usage of PHP's concatenation operator, ., to append the relative image path onto your scraped URL.
after scrapping the data , u can use str_replace to replace "image_large/" with "http://website.com/image_large/" ... thats the simplest i think
this will replace all similar image paths in the html.. or if u just want one image out of it , i think another user has given u a solution for that. doesnt get any simpler that that i think.