javascript: the beginning and end of an image along its center

Javascript snippet that detects the beginning and end of an image along its center, alpha supported.

Parameters: image id (without ‘#’)

Returns an object with:
– start (first non trasparent/not white pixel from center top)
– end (first not trasparent/not white pixel from center bottom)
– image width
– image height

 

function getRealImageYStartEnd($imageId){
    var img = document.getElementById($imageId);
    var canvas = document.createElement('canvas');
    canvas.width = img.width;
    canvas.height = img.height;
    canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height);
    var context = canvas.getContext('2d');
    var middle = Math.floor(img.width/2);
    var startImage = 0;
    var endImage = img.height;
    for (i = 0; i < img.height; i++){
        var data = context.getImageData(Math.floor(img.width/2), i, 1, 1).data;
        if (data[0] !== 0 && data[1] !== 0 && data[1] !== 0 && data[3] !== 0){
            startImage = i;
            break;
        }
    }
    for (i = img.height; i >= 0; i--){
        var data = context.getImageData(Math.floor(img.width/2), i, 1, 1).data;
        if (data[0] !== 0 && data[1] !== 0 && data[1] !== 0 && data[3] !== 0){
            endImage = i;
            break;
        }
    }
    return {start: startImage, end: endImage, width: img.width, height: img.height};
}

WordPress 4.8.3 + ACF + get_posts with meta_query

Here in Papion headquarters we love using ACF.
Advanced Custom Fields is one of the best WordPress plugins for custom field management.

Recently, with the new WordPress 4.8.3, a change was made in the prepare query to the database.

Instead of characters like % now we have placeholders like {1872368126381726387126381276381726318723} (for more information see code).

It does not involve any major issues, except when we use ACF in combination with get_posts and metaquery to search for post based on repeater fields that have meta_key in fieldgrup_%_field format like “fieldgrup_0_field”, “fieldgrup_1_field”, etc.

One of the solutions ACF proposes is (source)

function my_posts_where( $where ) {
  $where = str_replace("meta_key = 'locations_%", "meta_key LIKE 'locations_%", $where);
  
  return $where;
}
add_filter('posts_where', 'my_posts_where');

However, with the update, instead of the “%” character, we find ourselves with a numeric placeholder, so this code should be changed so that the comparison is done with the query without placeholders:

function my_posts_where( $where ) {
  $where = str_replace("meta_key = 'locations_%", "meta_key LIKE 'locations_%", $where);
  
  return $where;
}
add_filter('posts_where', 'my_posts_where');

Iubenda, check consent given in javascript

Logo Iubenda

Simple script to check if user have given consent to Iubenda privacy/cookie policy. Function will return true if user has given consent, false otherwise.

Semplice script per verificare se l’utente ha dato il consenso alla privacy / cookie policy di Iubenda. La funzione restituirà true se l’utente ha dato il consenso, altrimenti falso.

Event style use:

function iubendaConsentGiven(){
  var pairs = document.cookie.split(";");
  var cookies = {};
  for (var i=0; i<pairs.length; i++){
    var pair = pairs[i].split("=");
	if(pair[0].indexOf('_iub_cs-s') !== -1 || pair[0].indexOf('_iub_cs') !== -1 ) {
    	return true;
    }
  }
  return false;
}
var checkCookie = true;
var checkCookieInterval = setInterval(function(){
	if(iubendaConsentGiven()){
		console.log('ok');
		clearInterval(checkCookieInterval);
    }
}, 1000);