Pawan
Pawan

Reputation: 3864

Magento2 adding dynamic background to product images

I am building a site on Magento2 platform. I need to add images for painting done by Artists for sale.

what is required is that in addition to image being uploaded, I want to add 2-3 pre-defined background images to the painting. for example - Dining Room, Living Room.

I am able to change background image on per page load(randomly) using this code.

<head>
    <style type="text/css">
<!--
body{
background: url(images/<?php echo $selectedBg; ?>) no-repeat;
}
-->
</style>
</head>

<?php
  $bg = array('SET_A_01_Living.jpg', 'SET_A_02_Bed.jpg', 'SET_A_03_Study.jpg', 'SET_A_04_Dining.jpg' ); // array of filenames

  $i = rand(0, count($bg)-1); // generate random number size of the array
  $selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>

but how I add the background to the image and in Magento Gallery, I am not able to make out.

some guidance/hint on this will help me achieve, what I am trying to do.

Upvotes: 0

Views: 762

Answers (1)

Jinesh
Jinesh

Reputation: 1580

you have to write php above the head tag like below :-

<?php
  $bg = array('SET_A_01_Living.jpg', 'SET_A_02_Bed.jpg', 'SET_A_03_Study.jpg', 'SET_A_04_Dining.jpg' ); // array of filenames

  $i = rand(0, count($bg)-1); // generate random number size of the array
  $selectedBg = $bg[$i]; // set variable equal to which random filename was chosen
?>

<head>
    <style type="text/css">
<!--
body{
background: url(images/<?php echo $selectedBg; ?>) no-repeat;
}
-->
</style>
</head>

Upvotes: 0

Related Questions