Development News

TomThorp.me Blog: How to build beautiful CERN websites

Main Drupal Feed - Thu, 11/07/2019 - 12:02
How to build beautiful CERN websites using Drupal 8

TomThorp.me Blog: Nasty PHP7 bug exploit in the wild

Main Drupal Feed - Thu, 11/07/2019 - 11:26
New PHP7 bug CVE-2019-11043 can allow even non-technical attackers to take over servers.

TomThorp.me Blog: Nasty PHP7 bug exploit in the wild

Main Drupal Feed - Thu, 11/07/2019 - 11:26
New PHP7 bug CVE-2019-11043 can allow even non-technical attackers to take over servers.

TomThorp.me Blog: Nasty PHP7 bug exploit in the wild

Main Drupal Feed - Thu, 11/07/2019 - 11:26
New PHP7 bug CVE-2019-11043 can allow even non-technical attackers to take over servers.

Lullabot: Custom Layout Options in Drupal 8

Main Drupal Feed - Wed, 11/06/2019 - 17:52

Like most CMS products that provide structured content modeling tools, Drupal excels at building template-driven pages with consistent layouts. A blog post might use one template and a staff bio another, but as long as the basic shape of the page is consistent, Drupal's traditional content modeling and page composition tools are usually enough. When it comes time to build special content, though—departmental portal pages, landing pages for ephemeral marketing campaigns, editorial curated topic hubs, and so on—Drupal's combination of templates and structured content can fall short.

Lullabot: Custom Layout Options in Drupal 8

Main Drupal Feed - Wed, 11/06/2019 - 17:52

Like most CMS products that provide structured content modeling tools, Drupal excels at building template-driven pages with consistent layouts. A blog post might use one template and a staff bio another, but as long as the basic shape of the page is consistent, Drupal's traditional content modeling and page composition tools are usually enough. When it comes time to build special content, though—departmental portal pages, landing pages for ephemeral marketing campaigns, editorial curated topic hubs, and so on—Drupal's combination of templates and structured content can fall short.

Lullabot: Custom Layout Options in Drupal 8

Main Drupal Feed - Wed, 11/06/2019 - 17:52

Like most CMS products that provide structured content modeling tools, Drupal excels at building template-driven pages with consistent layouts. A blog post might use one template and a staff bio another, but as long as the basic shape of the page is consistent, Drupal's traditional content modeling and page composition tools are usually enough. When it comes time to build special content, though—departmental portal pages, landing pages for ephemeral marketing campaigns, editorial curated topic hubs, and so on—Drupal's combination of templates and structured content can fall short.

Lullabot: Custom Layout Options in Drupal 8

Main Drupal Feed - Wed, 11/06/2019 - 17:52

Like most CMS products that provide structured content modeling tools, Drupal excels at building template-driven pages with consistent layouts. A blog post might use one template and a staff bio another, but as long as the basic shape of the page is consistent, Drupal's traditional content modeling and page composition tools are usually enough. When it comes time to build special content, though—departmental portal pages, landing pages for ephemeral marketing campaigns, editorial curated topic hubs, and so on—Drupal's combination of templates and structured content can fall short.

Lullabot: Custom Layout Options in Drupal 8

Main Drupal Feed - Wed, 11/06/2019 - 17:52

Like most CMS products that provide structured content modeling tools, Drupal excels at building template-driven pages with consistent layouts. A blog post might use one template and a staff bio another, but as long as the basic shape of the page is consistent, Drupal's traditional content modeling and page composition tools are usually enough. When it comes time to build special content, though—departmental portal pages, landing pages for ephemeral marketing campaigns, editorial curated topic hubs, and so on—Drupal's combination of templates and structured content can fall short.

InternetDevels: Shopping cart usability & Commerce Cart Flyout Drupal module

Main Drupal Feed - Wed, 11/06/2019 - 16:22

Shopping cart is a vital place in your online store where the “moment of truth” often happens — the customer either proceeds to checkout or abandons the purchase.

The situation can be changed in favor of option 1 if you increase shopping cart usability. Among nice Drupal 8 shopping cart modules, we would like to discuss one today. Its name is the Commerce Cart Flyout module in Drupal 8, so let’s see what it does in the usability arena.

Read more

InternetDevels: Shopping cart usability & Commerce Cart Flyout Drupal module

Main Drupal Feed - Wed, 11/06/2019 - 16:22

Shopping cart is a vital place in your online store where the “moment of truth” often happens — the customer either proceeds to checkout or abandons the purchase.

The situation can be changed in favor of option 1 if you increase shopping cart usability. Among nice Drupal 8 shopping cart modules, we would like to discuss one today. Its name is the Commerce Cart Flyout module in Drupal 8, so let’s see what it does in the usability arena.

Read more

InternetDevels: Shopping cart usability & Commerce Cart Flyout Drupal module

Main Drupal Feed - Wed, 11/06/2019 - 16:22

Shopping cart is a vital place in your online store where the “moment of truth” often happens — the customer either proceeds to checkout or abandons the purchase.

The situation can be changed in favor of option 1 if you increase shopping cart usability. Among nice Drupal 8 shopping cart modules, we would like to discuss one today. Its name is the Commerce Cart Flyout module in Drupal 8, so let’s see what it does in the usability arena.

Read more

InternetDevels: Shopping cart usability & Commerce Cart Flyout Drupal module

Main Drupal Feed - Wed, 11/06/2019 - 16:22

Shopping cart is a vital place in your online store where the “moment of truth” often happens — the customer either proceeds to checkout or abandons the purchase.

The situation can be changed in favor of option 1 if you increase shopping cart usability. Among nice Drupal 8 shopping cart modules, we would like to discuss one today. Its name is the Commerce Cart Flyout module in Drupal 8, so let’s see what it does in the usability arena.

Read more

InternetDevels: Shopping cart usability & Commerce Cart Flyout Drupal module

Main Drupal Feed - Wed, 11/06/2019 - 16:22

Shopping cart is a vital place in your online store where the “moment of truth” often happens — the customer either proceeds to checkout or abandons the purchase.

The situation can be changed in favor of option 1 if you increase shopping cart usability. Among nice Drupal 8 shopping cart modules, we would like to discuss one today. Its name is the Commerce Cart Flyout module in Drupal 8, so let’s see what it does in the usability arena.

Read more

Centarro: Commerce 2.15 adds invoicing, VAT number handling, and Drupal 9 readiness

Main Drupal Feed - Wed, 11/06/2019 - 15:42

We had a great time at DrupalCon Amsterdam last week introducing the European Drupal communtiy to our new brand, our near term product roadmap, and our recently launched support offering. It's always encouraging to hear from the many active individual and agency contributors in Europe, and the broad interest we received in Centarro Support validated our approach for providing fast, professional support in a dedicated channel.

Joachim's blog: Debugging and Logging AJAX requests tests in Docksal

Main Drupal Feed - Wed, 11/06/2019 - 14:30

The hardest thing I find with tests is understanding errors. Every time I think I've got debugging output sorted, I find a new layer where it doesn't work, I've got nothing, and I'm in the dark with something that's crashing and I don't know why.

The first layer is simple: errors in your test code itself. For example, make a typo in your tests/src/Functional/MyTest.php and PHPUnit crashes and you see the error in the terminal.

But when it's site code that's crashing, you're dealing with a system that is being driven by code, and therefore, you can't see it. And that's a major obstacle to figuring out a problem.

The HTML output that Drupal's Functional and Functional Javascript tests produce is a huge help: every time your test code makes a request to the test site, an HTML file is written to the test files directory. If your site crashes when your test makes a request, you'll see the error and the backtrace there.

However, there's no such output when in a Functional Javascript test you cause an AJAX request. And while you can create a screenshot of what the page looks like after the request, our another HTML file of the page (see https://www.drupal.org/project/drupal/issues/3090498 for instructions how; the issue is about how to make that automatic but I have no idea how that might be possible), you can't see the actual error, because AJAX requests that fail just sit there doing nothing. There's nothing useful to see in the browser.

So we need to see logs. When a real site has an AJAX crash, with a human being-controlled web browser making the request, you can go and look in the logs. With a test site, the log table is zapped when the test is completed.

Fortunately, Drupal 8's pluggable logging means there are other ways of getting hold of them, more permanent ways.

I first tried log_stdout module. This outputs log errors to STDOUT. If you're running on Docksal, as I am, you have an extra layer to get though to see that. You can monitor the cli container with fin logs -f cli, and with that module add a | ag WATCHDOG to filter.

However, I wasn't seeing backtrace in this output, and I gave up figuring out why.

So I tried filelog module instead, which as the name implies, writes log to a simple text file. This needs a little bit more work, as by default it writes to 'public://logs'. This means that each run of the test gets its own log file, which is perhaps what you want, but for my own uses I wanted a single log file I could tail -f in a terminal window and have continual monitoring.

A quick bit of config setting in the test's setUp() does the trick:

$this->config('filelog.settings') ->set('location', '/var/www/docroot/sites/simpletest/logs') ->save();

And I think that's me at last sorted.

Tags: debuggingtestsdrupal planet

ComputerMinds.co.uk: Making product images consistent with image styles

Main Drupal Feed - Wed, 11/06/2019 - 14:22

We’ve recently given the dimplex.co.uk site a new face lift and stumbled across an interesting problem during development when it came to implementing the new product carousel on their product pages, more specifically, the handling of the images themselves.

The design brief stipulated that the transparent studio style product images required a light grey background behind it, giving the impression of a product floating nicely in the middle of a light grey surrounding.

We had 2 problems here:

1. A lot of the studio style product images didn’t have sufficient transparent space around themselves and we ended up with unsightly results; images would be touching the edges of its container and weren’t sitting in the middle as intended. We needed to cater for these types of images.

2. We have a mix of studio and lifestyle shots. We couldn't just apply the same image style to both types of image; we would have to come up with something magic/clever to distinguish a studio shot from a lifestyle shot and then apply an image style accordingly.

Given that we are ComputerMinds (we massively care about our clients and we love a challenge) and knowing that the client would have to manually go back, edit and re-upload thousands of images, we decided if would be cool if we could come up with a code solution for them. Below is a brief outline as to how we achieved a code solution to a design/content problem. Enjoy!

Our concept was pretty clear; for images that had at least 1 - and no more than 3 - edges that contained transparent pixels, apply a Drupal image style with a custom effect that would “pad” out the image. The idea was, if an image has 4 transparent edges, the image had sufficient space around it. If it had no transparent edges we knew this was a lifestyle product shot, i.e a radiator in a lounge.

I started looking at the possibilities of detecting transparent pixels with PHP and came across a handy function from the PHP GD image library called imagecolorat(). Using this function and some hexadecimal converter madness (please don't ask me to explain!), we can detect what type of pixel we are looking at given a set of coordinates.

// The result of $transparency will be an integer between 0 and 127 // 127 is transparent, 0 is opaque/solid. $rgba = imagecolorat($image_resource, $x, $y); $transparency = ($rgba >> 24) & 0x7F;

Now we needed to run this function for every pixel along on all four edges. So, first things first, grab the image width and height and subtract 1 from the result. Subtracting 1 ensures you won’t hit a PHP notice about being “out of bounds” - we’re not playing a round of golf here :). Next, we need to sort out our coordinate ranges for us to loop over:

// Top side X = 0 Y = $image_width // Bottom side X = $image_height Y = $image_width // Left side X = $image_height Y = 0 // Right side X = $image_width Y = $image_height

For each permutation of coordinates (x=0, y=750, x=1, y=750, x=2, y=750 etc) , we simply check each pixel result from imagecolorat() and save the result to an array for us to check later on. Once we have detected a transparent and a non-transparent pixel (remembering to check our magic number 127), we then break out because we have all the information we need from this particular edge.

After you’ve completed this process for all four sides, we then do a simple check to see we have a mix of transparent and non transparent pixel edges. If we do, then we pass the image along to our custom image style for processing.

Our custom Drupal image style uses the “define canvas” image processor from the contrib module imagecache actions. We define our own image style effect but use imagecache_actions’ “define canvas” processor to transform our image. This is where we add X amount of pixels to increase the “padding” around the image.

In order to create a custom Drupal image style, we would need to implement hook_image_effect_info() and place any effect code into the "effect callback". See below for an example.

/** * Implements hook_image_effect_info(). */ function MY_MODULE_image_effect_info() { $effects = array(); // Remember to replace MY_MODULE with the name of your module (in lower case). $effects['MY_MODULE_transparent_padding'] = array( 'label' => t('Transparent padding'), 'help' => t('Applies padding if an image has a mix of transparent and solid pixels around the edges of an image.'), 'effect callback' => 'MY_MODULE_transparent_padding_effect_callback', 'form callback' => 'MY_MODULE_transparent_padding_form', 'summary theme' => 'transparent_padding_summary', ); return $effects; } /** * Callback for MY_MODULE_transparent_padding image effect. */ function MY_MODULE_transparent_padding_effect_callback(stdClass $image, array $data) { if ($image->info['mime_type'] == 'image/png') { $image_height = ($image->info['height'] - 1); $image_width = ($image->info['width'] - 1); $image_resource = $image->resource; // Do the coordinate magic and determine transparent and non transparent pixels // Build up $data array (this will contain background colour and amount of pixels - // to pad out the image. // Use imagecache_actions custom effect "define canvas" and pass in your $data array. $success = image_toolkit_invoke('definecanvas', $image, array($data)); return $success }

Below is the result of our custom image style effect settings form as defined above in hook_image_effect_info via "form callback". (The form was taken directly from imagecache_actions' definecanvas effect where I made a couple of edits - so credit goes to them)

And the result is shown below: a before and after. Much better! Furthermore, we’ve saved the client from having to manually edit thousands of images and then manually re-upload them. Win win!

 

 

ComputerMinds.co.uk: Making product images consistent with image styles

Main Drupal Feed - Wed, 11/06/2019 - 14:22

We’ve recently given the dimplex.co.uk site a new face lift and stumbled across an interesting problem during development when it came to implementing the new product carousel on their product pages, more specifically, the handling of the images themselves.

The design brief stipulated that the transparent studio style product images required a light grey background behind it, giving the impression of a product floating nicely in the middle of a light grey surrounding.

We had 2 problems here:

1. A lot of the studio style product images didn’t have sufficient transparent space around themselves and we ended up with unsightly results; images would be touching the edges of its container and weren’t sitting in the middle as intended. We needed to cater for these types of images.

2. We have a mix of studio and lifestyle shots. We couldn't just apply the same image style to both types of image; we would have to come up with something magic/clever to distinguish a studio shot from a lifestyle shot and then apply an image style accordingly.

Given that we are ComputerMinds (we massively care about our clients and we love a challenge) and knowing that the client would have to manually go back, edit and re-upload thousands of images, we decided if would be cool if we could come up with a code solution for them. Below is a brief outline as to how we achieved a code solution to a design/content problem. Enjoy!

Our concept was pretty clear; for images that had at least 1 - and no more than 3 - edges that contained transparent pixels, apply a Drupal image style with a custom effect that would “pad” out the image. The idea was, if an image has 4 transparent edges, the image had sufficient space around it. If it had no transparent edges we knew this was a lifestyle product shot, i.e a radiator in a lounge.

I started looking at the possibilities of detecting transparent pixels with PHP and came across a handy function from the PHP GD image library called imagecolorat(). Using this function and some hexadecimal converter madness (please don't ask me to explain!), we can detect what type of pixel we are looking at given a set of coordinates.

// The result of $transparency will be an integer between 0 and 127 // 127 is transparent, 0 is opaque/solid. $rgba = imagecolorat($image_resource, $x, $y); $transparency = ($rgba >> 24) & 0x7F;

Now we needed to run this function for every pixel along on all four edges. So, first things first, grab the image width and height and subtract 1 from the result. Subtracting 1 ensures you won’t hit a PHP notice about being “out of bounds” - we’re not playing a round of golf here :). Next, we need to sort out our coordinate ranges for us to loop over:

// Top side X = 0 Y = $image_width // Bottom side X = $image_height Y = $image_width // Left side X = $image_height Y = 0 // Right side X = $image_width Y = $image_height

For each permutation of coordinates (x=0, y=750, x=1, y=750, x=2, y=750 etc) , we simply check each pixel result from imagecolorat() and save the result to an array for us to check later on. Once we have detected a transparent and a non-transparent pixel (remembering to check our magic number 127), we then break out because we have all the information we need from this particular edge.

After you’ve completed this process for all four sides, we then do a simple check to see we have a mix of transparent and non transparent pixel edges. If we do, then we pass the image along to our custom image style for processing.

Our custom Drupal image style uses the “define canvas” image processor from the contrib module imagecache actions. We define our own image style effect but use imagecache_actions’ “define canvas” processor to transform our image. This is where we add X amount of pixels to increase the “padding” around the image.

In order to create a custom Drupal image style, we would need to implement hook_image_effect_info() and place any effect code into the "effect callback". See below for an example.

/** * Implements hook_image_effect_info(). */ function MY_MODULE_image_effect_info() { $effects = array(); // Remember to replace MY_MODULE with the name of your module (in lower case). $effects['MY_MODULE_transparent_padding'] = array( 'label' => t('Transparent padding'), 'help' => t('Applies padding if an image has a mix of transparent and solid pixels around the edges of an image.'), 'effect callback' => 'MY_MODULE_transparent_padding_effect_callback', 'form callback' => 'MY_MODULE_transparent_padding_form', 'summary theme' => 'transparent_padding_summary', ); return $effects; } /** * Callback for MY_MODULE_transparent_padding image effect. */ function MY_MODULE_transparent_padding_effect_callback(stdClass $image, array $data) { if ($image->info['mime_type'] == 'image/png') { $image_height = ($image->info['height'] - 1); $image_width = ($image->info['width'] - 1); $image_resource = $image->resource; // Do the coordinate magic and determine transparent and non transparent pixels // Build up $data array (this will contain background colour and amount of pixels - // to pad out the image. // Use imagecache_actions custom effect "define canvas" and pass in your $data array. $success = image_toolkit_invoke('definecanvas', $image, array($data)); return $success }

Below is the result of our custom image style effect settings form as defined above in hook_image_effect_info via "form callback". (The form was taken directly from imagecache_actions' definecanvas effect where I made a couple of edits - so credit goes to them)

And the result is shown below: a before and after. Much better! Furthermore, we’ve saved the client from having to manually edit thousands of images and then manually re-upload them. Win win!

 

 

ComputerMinds.co.uk: Making product images consistent with image styles

Main Drupal Feed - Wed, 11/06/2019 - 14:22

We’ve recently given the dimplex.co.uk site a new face lift and stumbled across an interesting problem during development when it came to implementing the new product carousel on their product pages, more specifically, the handling of the images themselves.

The design brief stipulated that the transparent studio style product images required a light grey background behind it, giving the impression of a product floating nicely in the middle of a light grey surrounding.

We had 2 problems here:

1. A lot of the studio style product images didn’t have sufficient transparent space around themselves and we ended up with unsightly results; images would be touching the edges of its container and weren’t sitting in the middle as intended. We needed to cater for these types of images.

2. We have a mix of studio and lifestyle shots. We couldn't just apply the same image style to both types of image; we would have to come up with something magic/clever to distinguish a studio shot from a lifestyle shot and then apply an image style accordingly.

Given that we are ComputerMinds (we massively care about our clients and we love a challenge) and knowing that the client would have to manually go back, edit and re-upload thousands of images, we decided if would be cool if we could come up with a code solution for them. Below is a brief outline as to how we achieved a code solution to a design/content problem. Enjoy!

Our concept was pretty clear; for images that had at least 1 - and no more than 3 - edges that contained transparent pixels, apply a Drupal image style with a custom effect that would “pad” out the image. The idea was, if an image has 4 transparent edges, the image had sufficient space around it. If it had no transparent edges we knew this was a lifestyle product shot, i.e a radiator in a lounge.

I started looking at the possibilities of detecting transparent pixels with PHP and came across a handy function from the PHP GD image library called imagecolorat(). Using this function and some hexadecimal converter madness (please don't ask me to explain!), we can detect what type of pixel we are looking at given a set of coordinates.

// The result of $transparency will be an integer between 0 and 127 // 127 is transparent, 0 is opaque/solid. $rgba = imagecolorat($image_resource, $x, $y); $transparency = ($rgba >> 24) & 0x7F;

Now we needed to run this function for every pixel along on all four edges. So, first things first, grab the image width and height and subtract 1 from the result. Subtracting 1 ensures you won’t hit a PHP notice about being “out of bounds” - we’re not playing a round of golf here :). Next, we need to sort out our coordinate ranges for us to loop over:

// Top side X = 0 Y = $image_width // Bottom side X = $image_height Y = $image_width // Left side X = $image_height Y = 0 // Right side X = $image_width Y = $image_height

For each permutation of coordinates (x=0, y=750, x=1, y=750, x=2, y=750 etc) , we simply check each pixel result from imagecolorat() and save the result to an array for us to check later on. Once we have detected a transparent and a non-transparent pixel (remembering to check our magic number 127), we then break out because we have all the information we need from this particular edge.

After you’ve completed this process for all four sides, we then do a simple check to see we have a mix of transparent and non transparent pixel edges. If we do, then we pass the image along to our custom image style for processing.

Our custom Drupal image style uses the “define canvas” image processor from the contrib module imagecache actions. We define our own image style effect but use imagecache_actions’ “define canvas” processor to transform our image. This is where we add X amount of pixels to increase the “padding” around the image.

In order to create a custom Drupal image style, we would need to implement hook_image_effect_info() and place any effect code into the "effect callback". See below for an example.

/** * Implements hook_image_effect_info(). */ function MY_MODULE_image_effect_info() { $effects = array(); // Remember to replace MY_MODULE with the name of your module (in lower case). $effects['MY_MODULE_transparent_padding'] = array( 'label' => t('Transparent padding'), 'help' => t('Applies padding if an image has a mix of transparent and solid pixels around the edges of an image.'), 'effect callback' => 'MY_MODULE_transparent_padding_effect_callback', 'form callback' => 'MY_MODULE_transparent_padding_form', 'summary theme' => 'transparent_padding_summary', ); return $effects; } /** * Callback for MY_MODULE_transparent_padding image effect. */ function MY_MODULE_transparent_padding_effect_callback(stdClass $image, array $data) { if ($image->info['mime_type'] == 'image/png') { $image_height = ($image->info['height'] - 1); $image_width = ($image->info['width'] - 1); $image_resource = $image->resource; // Do the coordinate magic and determine transparent and non transparent pixels // Build up $data array (this will contain background colour and amount of pixels - // to pad out the image. // Use imagecache_actions custom effect "define canvas" and pass in your $data array. $success = image_toolkit_invoke('definecanvas', $image, array($data)); return $success }

Below is the result of our custom image style effect settings form as defined above in hook_image_effect_info via "form callback". (The form was taken directly from imagecache_actions' definecanvas effect where I made a couple of edits - so credit goes to them)

And the result is shown below: a before and after. Much better! Furthermore, we’ve saved the client from having to manually edit thousands of images and then manually re-upload them. Win win!

 

 

ComputerMinds.co.uk: Making product images consistent with image styles

Main Drupal Feed - Wed, 11/06/2019 - 14:22

We’ve recently given the dimplex.co.uk site a new face lift and stumbled across an interesting problem during development when it came to implementing the new product carousel on their product pages, more specifically, the handling of the images themselves.

The design brief stipulated that the transparent studio style product images required a light grey background behind it, giving the impression of a product floating nicely in the middle of a light grey surrounding.

We had 2 problems here:

1. A lot of the studio style product images didn’t have sufficient transparent space around themselves and we ended up with unsightly results; images would be touching the edges of its container and weren’t sitting in the middle as intended. We needed to cater for these types of images.

2. We have a mix of studio and lifestyle shots. We couldn't just apply the same image style to both types of image; we would have to come up with something magic/clever to distinguish a studio shot from a lifestyle shot and then apply an image style accordingly.

Given that we are ComputerMinds (we massively care about our clients and we love a challenge) and knowing that the client would have to manually go back, edit and re-upload thousands of images, we decided if would be cool if we could come up with a code solution for them. Below is a brief outline as to how we achieved a code solution to a design/content problem. Enjoy!

Our concept was pretty clear; for images that had at least 1 - and no more than 3 - edges that contained transparent pixels, apply a Drupal image style with a custom effect that would “pad” out the image. The idea was, if an image has 4 transparent edges, the image had sufficient space around it. If it had no transparent edges we knew this was a lifestyle product shot, i.e a radiator in a lounge.

I started looking at the possibilities of detecting transparent pixels with PHP and came across a handy function from the PHP GD image library called imagecolorat(). Using this function and some hexadecimal converter madness (please don't ask me to explain!), we can detect what type of pixel we are looking at given a set of coordinates.

// The result of $transparency will be an integer between 0 and 127 // 127 is transparent, 0 is opaque/solid. $rgba = imagecolorat($image_resource, $x, $y); $transparency = ($rgba >> 24) & 0x7F;

Now we needed to run this function for every pixel along on all four edges. So, first things first, grab the image width and height and subtract 1 from the result. Subtracting 1 ensures you won’t hit a PHP notice about being “out of bounds” - we’re not playing a round of golf here :). Next, we need to sort out our coordinate ranges for us to loop over:

// Top side X = 0 Y = $image_width // Bottom side X = $image_height Y = $image_width // Left side X = $image_height Y = 0 // Right side X = $image_width Y = $image_height

For each permutation of coordinates (x=0, y=750, x=1, y=750, x=2, y=750 etc) , we simply check each pixel result from imagecolorat() and save the result to an array for us to check later on. Once we have detected a transparent and a non-transparent pixel (remembering to check our magic number 127), we then break out because we have all the information we need from this particular edge.

After you’ve completed this process for all four sides, we then do a simple check to see we have a mix of transparent and non transparent pixel edges. If we do, then we pass the image along to our custom image style for processing.

Our custom Drupal image style uses the “define canvas” image processor from the contrib module imagecache actions. We define our own image style effect but use imagecache_actions’ “define canvas” processor to transform our image. This is where we add X amount of pixels to increase the “padding” around the image.

In order to create a custom Drupal image style, we would need to implement hook_image_effect_info() and place any effect code into the "effect callback". See below for an example.

/** * Implements hook_image_effect_info(). */ function MY_MODULE_image_effect_info() { $effects = array(); // Remember to replace MY_MODULE with the name of your module (in lower case). $effects['MY_MODULE_transparent_padding'] = array( 'label' => t('Transparent padding'), 'help' => t('Applies padding if an image has a mix of transparent and solid pixels around the edges of an image.'), 'effect callback' => 'MY_MODULE_transparent_padding_effect_callback', 'form callback' => 'MY_MODULE_transparent_padding_form', 'summary theme' => 'transparent_padding_summary', ); return $effects; } /** * Callback for MY_MODULE_transparent_padding image effect. */ function MY_MODULE_transparent_padding_effect_callback(stdClass $image, array $data) { if ($image->info['mime_type'] == 'image/png') { $image_height = ($image->info['height'] - 1); $image_width = ($image->info['width'] - 1); $image_resource = $image->resource; // Do the coordinate magic and determine transparent and non transparent pixels // Build up $data array (this will contain background colour and amount of pixels - // to pad out the image. // Use imagecache_actions custom effect "define canvas" and pass in your $data array. $success = image_toolkit_invoke('definecanvas', $image, array($data)); return $success }

Below is the result of our custom image style effect settings form as defined above in hook_image_effect_info via "form callback". (The form was taken directly from imagecache_actions' definecanvas effect where I made a couple of edits - so credit goes to them)

And the result is shown below: a before and after. Much better! Furthermore, we’ve saved the client from having to manually edit thousands of images and then manually re-upload them. Win win!

 

 

Pages