Drupal 8 images with image style - image

In drupal 7, i use function image_style_url('style', uri) to generate new image with style and return image's path. so what will be instead of it in drupal 8? thanks

Per the change records:
use Drupal\image\Entity\ImageStyle;
$path = 'public://images/image.jpg';
$url = ImageStyle::load('style_name')->buildUrl($path);

You should try to use the new Drupal functions wherever possible.
Instead, use:
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
$fid = 123;
$file = File::load($fid);
$image_uri = ImageStyle::load('your_style-name')->buildUrl($file->getFileUri());
Edited as per https://www.drupal.org/node/2050669:
$original_image = 'public://images/image.jpg';
// Load the image style configuration entity
use Drupal\image\Entity\ImageStyle;
$style = ImageStyle::load('thumbnail');
$uri = $style->buildUri($original_image);
$url = $style->buildUrl($original_image);

In your Controllers and other OOP part of Drupal you can use :
use Drupal\image\Entity\ImageStyle;
$path = 'public://images/image.jpg';
$url = ImageStyle::load('style_name')->buildUrl($path);
And in YOUR_THEME.theme file while Error: Class 'ImageStyle' not found in YOURTHEMENAME_preprocess_node you can do it with the follwing
$path = 'public://images/image.jpg';
$style = \Drupal::entityTypeManager()->getStorage('image_style')->load('thumbnail');
$url = $style->buildUrl($path);
Another method is provide a renderable array and let the drupal Render engine render it.
$render = [
'#theme' => 'image_style',
'#style_name' => 'thumbnail',
'#uri' => $path,
// optional parameters
];

I have found that I often want to preprocess the image to apply an image style to an image on a node or a paragraph type. In many cases I have created a paragraph that allows the user to choose the width of the image as a percentage. In the preprocess I would check the value of the width and apply the correct image style.
use Drupal\image\Entity\ImageStyle;
function THEME_preprocess_paragraph__basic_content(&$vars) {
//get the paragraph
$paragraph = $vars['paragraph'];
//get the image
$images = $paragraph->get('field_para_image');
//get the images value, in my case I only have one required image, but if you have unlimited image, you could loop thru $images
$uri = $images[0]->entity->uri->value;
//This is my field that determines the width the user wants for the image and is used to determine the image style
$preset = $paragraph->get('field_column_width')->value;
$properties = array();
$properties['title'] = $images[0]->getValue()['title'];
$properties['alt'] = $images[0]->getValue()['alt'];
//this is where the Image style is applied
switch($preset) {
case 'image-20':
$properties['uri'] = ImageStyle::load('width_20_percent')->buildUrl($uri);
break;
case 'image-45':
$properties['uri'] = ImageStyle::load('width_45_percent')->buildUrl($uri);
break;
case 'image-55':
$properties['uri'] = ImageStyle::load('width_55_percent')->buildUrl($uri);
break;
case 'image-100':
$properties['uri'] = ImageStyle::load('width_100_percent')->buildUrl($uri);
break;
}
//assign to a variable that the twig template can use
$vars['basic_image_display'] = $properties;
}
In this example, I am preprocessing a specific paragraph type named "basic_content" but you can do the same thing with a node preprocess. Continuing my example, I would have a twig template named paragraph--basic_content.html.twig to handle the display of that paragraph type.
Displaying the image would be something like this in the twig file.
<img class="img-responsive" src="{{basic_image_display['uri']}}" alt="{{ basic_image_display['alt'] }}" title="{{ basic_image_display['title'] }}"/>

$view_mode = $variables['content']['field_media_image']['0']['#view_mode'];
$display_content = \Drupal::service('entity_display.repository')
->getViewDisplay('media', 'image', $view_mode)->build($media_entity);
$style = ImageStyle::load($display_content['image'][0]['#image_style']); $original_image = $media_entity->get('image')->entity->getFileUri();
$destination = $style->buildUri($original_image);
This is how you get image style from a media image entity.

Works for me from a classic Drupal database Query in .module file :
$query = \Drupal::database()->select('file_managed', 'f' );
$query->addField('f', 'uri');
$pictures = $query->execute()->fetchAll();
foreach ($pictures as $key => $picture) {
$largePictureUri = entity_load('image_style', 'large')->buildUrl($picture->uri);
}

I used in Drupal 8 this code. It's working fine.
$fid = 374; //get your file id, this value just for example
$fname = db_select('file_managed', 'f')->fields('f', array('filename'))->condition('f.fid', $fid)->execute()->fetchField();
$url = entity_load('image_style', 'YOUR_STYLE_NAME')->buildUrl($fname);

Related

How to show image GD Resource in twig template

I have generated a GD Image Resource from the Zend Barcode Object. How can I render this resource in a twig template?
hello this code do the trick :
require 'vendor/autoload.php';
use Zend\Barcode\Barcode;
// Only the text to draw is required
$barcodeOptions = array('text' => 'ZEND-FRAMEWORK');
// No required options
$rendererOptions = array();
$image = Barcode::draw(
'code39',
'image',
$barcodeOptions,
$rendererOptions
);
// By default, imagepng will print on the output file your image.
// Here we play around with ob_* to catch this output and store it on $contents variable.
ob_start();
imagepng($image);
$contents = ob_get_contents();
ob_end_clean();
// Save as test.png your current barcode.
file_put_contents('test.png', $contents);
// Display img tag with base64 encoded barcode.
echo '<img src="data:image/png;base64,'.base64_encode($contents).'">';
Explication :
imagejpeg() or imagepng() receive as parameter gd image ressource and print it. Here we play around with ob_*function to capture this output in variable instead of print it. Then you can do what ever you want with this data. On my code i have done both possibility :
Save it in static file.
Directly print it as base64 image inside my html.

PHP Sessions: Generate a variable and save it for session

I am totally new to php sessions. I am not able to get a simple task done.
This is what I am trying to do:
A visitor of my website gets shown a random image
well this part works so far. But everytime the user goes on to another page (I have this random image shown on all pages) the script generates a new random image to be shown.
What I want to do now is:
Save the random image variable to a session so he will see the same image on each page he visits while he has the session saved.
Here is my working code to get a random image without saving it into sessions. If anybody could help me with how the code should look like so it works with sessions would be awesome. Remember: I am a total newby when it comes to sessions.
As you can see I need the variable $img to be stored into a session after it got generated. And the script only to strt again on a new site visit if a user hasnt stored the $img variable in his session.
<?php
function getImagesFromDir($path) {
$images = array();
if ( $img_dir = #opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}
function getRandomFromArray($ar) {
mt_srand( (double)microtime() * 1000000 ); // php 4.2+ not needed
$num = array_rand($ar);
return $ar[$num];
}
$root = '';
// use if specifying path from root
//$root = $_SERVER['DOCUMENT_ROOT'];
$path = 'images/';
// Obtain list of images from directory
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
?>
<img src="/<?php echo $path . $img ?>" alt="image" />
You'll need to add this at the top of every page to get your image:
session_start();
if(isset($_SESSION['UserImg'])){
$img = $_SESSION['UserImg'];
}
else {
$img = getRandomFromArray($imgList);
$_SESSION['UserImg'] = $img;
}
This should work out!
on top of each page call:
session_start();
To safe variable in session for example:
$_SESSION['imageid'] = $ID
To get the variable back:
$imageid = $_SESSION['imageid']

Removing the placeholder images for Product Tags using WooCommerce Predictive Search

I am using the predictive search to look for products and using PRO it searches for product tags as well, which is great. However, it still shows the placeholder (like a broken image link box) for the tags. I am simply wanting to remove the placeholder for tags only.
Anyone have an idea of the 'IF' statement I would use and where I would apply it? Again, it would only be for the product tags, I still would like the product itself to show the thumbnail images.
In the Wordpress plugins directory open the predictive search folder then open the classes folder. In the file class-wc-predictive-search-filter.php on line 331 you will see a foreach loop
foreach ( $search_tags as $item ) {
$link_detail = get_term_link($item->slug, 'product_tag');
$avatar = WC_Predictive_Search::woops_get_product_cat_thumbnail($item->term_id,64,64);
$item_html = '<div class="ajax_search_content"><div class="result_row"><span class="rs_avatar">'.$avatar.'</span><div class="rs_content_popup"><span class="rs_name">'.stripslashes( $item->name).'</span><span class="rs_description">'.WC_Predictive_Search::woops_limit_words(strip_tags( str_replace("\n", "", $item->description) ),$text_lenght,'...').'</span></div></div></div>';
$rs_items['p_tag'] .= $item_html.'[|]'.$link_detail.'[|]'.stripslashes( $item->name)."\n";
$end_row--;
if ($end_row < 1) break;
}
}
}
you will need to remove
<span class="rs_avatar">'.$avatar.'</span>
so the foreach loop reads
foreach ( $search_tags as $item ) {
$link_detail = get_term_link($item->slug, 'product_tag');
$avatar = WC_Predictive_Search::woops_get_product_cat_thumbnail($item->term_id,64,64);
$item_html = '<div class="ajax_search_content"><div class="result_row"><div class="rs_content_popup"><span class="rs_name">'.stripslashes( $item->name).'</span><span class="rs_description">'.WC_Predictive_Search::woops_limit_words(strip_tags( str_replace("\n", "", $item->description) ),$text_lenght,'...').'</span></div></div></div>';
$rs_items['p_tag'] .= $item_html.'[|]'.$link_detail.'[|]'.stripslashes( $item->name)."\n";
$end_row--;
if ($end_row < 1) break;
}
}
}

How to render banners alongside featured items in home page

I'm making a custom template form joomla 2.5, and one of the goals for the development is to include a banner after each featured article iteration.
After a long research I can render a banner into /template_name/html/com_content/featured/default_item.php with the following code:
$document = JFactory::getDocument();
$renderer = $document->loadRenderer('modules');
$position = "nota";
$options = array('style' => 'raw');
echo $renderer->render($position, $options, null);
But the issue is that each iteration resets the banner list so I have the same banner repeated with each featured article.
I tried to include the banner module using the same code in /template_name/html/com_content/featured/default.php without success. Lately I'd try with <jdoc:include type="modules" name="nota" style="raw" /> and it didn't works too, so I will appreciate any help to solve this point.
Thanks in advance.
You can solve this in two ways.
The right way
Copy and rename the banner module (fx. to mybanners), change the getList() method in the helper file to retrieve different banners on each call. This could fx. be:
class modMybannersHelper
{
static function &getList(&$params)
{
static $index = 0;
JModelLegacy::addIncludePath(JPATH_ROOT.'/components/com_banners/models', 'BannersModel');
$document = JFactory::getDocument();
$app = JFactory::getApplication();
$keywords = explode(',', $document->getMetaData('keywords'));
$model = JModelLegacy::getInstance('Banners', 'BannersModel', array('ignore_request'=>true));
$model->setState('filter.client_id', (int) $params->get('cid'));
$model->setState('filter.category_id', $params->get('catid', array()));
$model->setState('list.limit', 1);
$model->setState('list.start', $index++);
$model->setState('filter.ordering', $params->get('ordering'));
$model->setState('filter.tag_search', $params->get('tag_search'));
$model->setState('filter.keywords', $keywords);
$model->setState('filter.language', $app->getLanguageFilter());
$banners = $model->getItems();
$model->impress();
return $banners;
}
}
This is just a sketch; you still need to handle the case of $index being greater than the number of records.
The hacky way
The retrieval code has only one port open to inject conditions - the documents's keywords.
So you could (in your template file) store the original keywords an replace them with a keyword to identify a banner. The banner must have the same keyword in that case.
$document = JFactory::getDocument();
$keywords = $document->getMetaData('keywords');
$renderer = $document->loadRenderer('modules');
$position = "nota";
$options = array('style' => 'raw');
$document->setMetaData('keywords', 'banner_key');
echo $renderer->render($position, $options, null);
$document->setMetaData('keywords', $keywords);
Either way, caching may prevent that from work, so you might have to turn it off (I didn't check that).

WordPress: snag first image from a gallery

On my homepage, I'm trying to output the first image of each post, and I'm able to successfully do that using this code in my functions.php:
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
and then I call it in my loop like this:
<img src=”<?php catch_that_image(); ?>” />
The problem with this method is that it won't work if I place a gallery in that same post. I'm confused because the output of the post still renders the img markup, and my assumption is that catch_that_image() should snag that markup? Is my thinking incorrect? Is there a better way to handle this?
The gallery is placed inside your post using a WordPress short-tag. The shorttag is transformed into HTML-image tags when the filter for transforming the shorttag is applied.
The following might work, but I am not sure:
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$transformed_content = apply_filters('the_content',$post->post_content);
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $transformed_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
Please let me know if this was useful or if it was pointing you into the right direction!
Codex Links:
http://codex.wordpress.org/Gallery_Shortcode
http://codex.wordpress.org/Function_Reference/apply_filters
http://codex.wordpress.org/Function_Reference/do_shortcode

Resources