Announcement

Collapse
No announcement yet.

Automatic thumbnail generation.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Automatic thumbnail generation.

    This is quick test to see what could be done to automatically generate a thumbnail image from the product one. It's not complete (it only works with JPG product images) and requires your product images to be in the Site folder but should give some idea of what could be done.

    It's a block of code that replaces the Standard Product Image layout and contains an extra block of PHP that creates a new image t_<ProductImageFileName> scaled down to a width of 50px.
    Code:
    <actinic:block if="%3cactinic%3avariable%20name%3d%22IsPopUpDisplayedByImage%22%20%2f%3e">
       <actinic:block if="%3cactinic%3avariable%20name%3d%22ExtendedInformationType%22%20%2f%3e%20%3d%3d%20%22Opens%20in%20a%20Pop%2dUp%20Window%22">
          <a href="javascript:ShowPopUp('<actinic:variable name=ExtendedInfoPageEncoded />',<actinic:variable name="ExtInfoWindowWidth" />,<actinic:variable name="ExtInfoWindowHeight" />);">
       </actinic:block>
       <actinic:block if="%3cactinic%3avariable%20name%3d%22ExtendedInformationType%22%20%2f%3e%20%3d%3d%20%22Opens%20in%20the%20Same%20Window%22" >
          <a href="<actinic:variable name="ExtendedInfoPageName" />">
       </actinic:block>
    </actinic:block>
    
    <actinic:block if="%3cactinic%3avariable%20name%3d%22IsProductImageDisplayed%22%20%2f%3e">
       <img src="<actinic:variable name="ProductImageFileName" />"
          border="0"
          width="<actinic:variable name="ProductImageWidth" />"
          height="<actinic:variable name="ProductImageHeight" />"
          alt="<actinic:variable name="ProductName" />" />
          
    <actinic:block php="true" >
       // START Create a thumbnail image t_ProductImageFileName
     	$sOriginalImageName = '<actinic:variable  encoding="perl" name="ProductImageFileName"  selectable="false" />';
    	$sThumbImageName = 't_' . $sOriginalImageName;
    	$image = @imagecreatefromjpeg($sOriginalImageName); /* Attempt to open */
        	if (!$image) 
    			{ /* See if it failed */
    			echo "<br><font color=red>Thumbnail creation error opening: $sOriginalImageName </font>";
    			}
    		else
    			{
    			// Get new dimensions
    			$width = imagesx($image);
    			$height = imagesy($image);
    			$t_width = 50;
    			$t_height = $height * ($t_width / $width);
    
    			// Resample
    			$thumbimage = imagecreatetruecolor($t_width, $t_height);
    			imagecopyresampled($thumbimage, $image, 0, 0, 0, 0, $t_width, $t_height, $width, $height);
    			if ( ! imagejpeg($thumbimage, $sThumbImageName) ) 
    				{
    				echo "<font color=red>Thumbnail image creation failed: $sThumbImageName </font><br>";
    				}
    			else
    				{
    				echo "<br><img src=\"$sThumbImageName\" width=\"$t_width\" height=\"$t_height\" border=0 />";
    				}
    			imagedestroy($thumbimage);	
    			}
    	imagedestroy($image);
     // END Create a thumbnail image t_ProductImageFileName
     </actinic:block>  
            
          
    </actinic:block>
    
    <actinic:block if="%3cactinic%3avariable%20name%3d%22IsProductImageDisplayed%22%20%2f%3e%20%3d%3d%20False">
       <img src="<actinic:variable name="DefaultProductImage" />"
          border="0"
          alt="<actinic:variable name="ProductName" />" />
    </actinic:block>
    
    <actinic:block if="%3cactinic%3avariable%20name%3d%22IsPopUpDisplayedByImage%22%20%2f%3e">
       </a>
    </actinic:block>
    At the moment this image is displayed (necessary so that Actinic will upload it), but all you need to do is add some CSS to the <img...> tag to make this hidden and it will still be uploaded.

    Now, wherever you are able to display a Product Image you can also display

    <img src="t_<actinic:variable name="ProductImageFileName" />" />

    Code like this could also be used to allow you to use arbitrary sized product images, which are then automatically scaled down to fit the site requirements.
    Last edited by NormanRouxel; 12-Dec-2006, 05:39 PM. Reason: Added border=0 to generated <img...> tag.
    Norman - www.drillpine.biz
    Edinburgh, U K / Bitez, Turkey

    #2
    Nice one Norman

    v8 + PHP + Norman = unstopable


    Bikster
    SellerDeck Designs and Responsive Themes

    Comment


      #3
      Code like this could also be used to allow you to use arbitrary sized product images, which are then automatically scaled down to fit the site requirements.
      Nice one norm. One thing I find people have problems with is getting the images optomised for Actinic ie Mr Shopinthebox buys Actinic and then finds they need more software to process the images and it takes more time and effort to do them than building the content.

      Do you think it would be possible for an addon to allow them to place any sized image into Site1 and then add it to the product. Ideally they could specify the image size eg 200px product but that could be a fixed value. What I am suggesting is a way to make the product building process easier/faster. Obviously many will like more fleaxability but I am not suggesting a replacement for Photoshop just someting simple.

      Comment


        #4
        Hi, Malcolm

        The code above would do that with only minor editing.

        Create a new Numeric variable called StandardImageWidth of type Number and set it to 100 (or whatever) for both Initial Value and Top Level Value. Set Place Of Setting to Site, Section, Brochure, Product and Fragment.

        Then replace the Standard Product Image layout with
        Code:
        <actinic:block if="%3cactinic%3avariable%20name%3d%22IsPopUpDisplayedByImage%22%20%2f%3e">
           <actinic:block if="%3cactinic%3avariable%20name%3d%22ExtendedInformationType%22%20%2f%3e%20%3d%3d%20%22Opens%20in%20a%20Pop%2dUp%20Window%22">
              <a href="javascript:ShowPopUp('<actinic:variable name=ExtendedInfoPageEncoded />',<actinic:variable name="ExtInfoWindowWidth" />,<actinic:variable name="ExtInfoWindowHeight" />);">
           </actinic:block>
           <actinic:block if="%3cactinic%3avariable%20name%3d%22ExtendedInformationType%22%20%2f%3e%20%3d%3d%20%22Opens%20in%20the%20Same%20Window%22" >
              <a href="<actinic:variable name="ExtendedInfoPageName" />">
           </actinic:block>
        </actinic:block>
        <actinic:block if="%3cactinic%3avariable%20name%3d%22IsProductImageDisplayed%22%20%2f%3e">
        <actinic:block php="true" >
        	// START Create a fixed width image r_ProductImageFileName
        	$sOriginalImageName = '<actinic:variable encoding="perl" name="ProductImageFileName"  selectable="false" />';
        	$sResizedImageName = 'r_' . $sOriginalImageName;
        	$image = @imagecreatefromjpeg($sOriginalImageName); /* Attempt to open */
        	if (!$image) 
        		{ /* See if it failed */
        		echo "<br /><font color=red>Resized image creation error opening: $sOriginalImageName </font>";
        		}
        	else
        		{
        		// Get new dimensions
        		$width = imagesx($image);
        		$height = imagesy($image);
        		$t_width = '<actinic:variable encoding="perl" name="StandardImageWidth" selectable="false" />';
        		if ( $t_width == '' ) $t_width = 100;
        		$t_height = $height * ($t_width / $width);
        
        		// Resample
        		$Resizedimage = imagecreatetruecolor($t_width, $t_height);
        		imagecopyresampled($Resizedimage, $image, 0, 0, 0, 0, $t_width, $t_height, $width, $height);
        		if ( ! imagejpeg($Resizedimage, $sResizedImageName) ) 
        			{
        			echo "<br /><font color=red>Resized image creation failed: $sResizedImageName </font><br>";
        			}
        		else
        			{
        			echo '<img src="' . $sResizedImageName . '"';
        			echo ' border="0"';
        			echo ' width="' . $t_width . '"';
        			echo ' height="' . $t_height . '"';
        			echo ' alt="<actinic:variable name="ProductName" />" />';
        			}
        		imagedestroy($Resizedimage);	
        		}
         // END Create a fixed width image r_ProductImageFileName
         </actinic:block>  
                
              
        </actinic:block>
        
        <actinic:block if="%3cactinic%3avariable%20name%3d%22IsProductImageDisplayed%22%20%2f%3e%20%3d%3d%20False">
           <img src="<actinic:variable name="DefaultProductImage" />"
              border="0"
              alt="<actinic:variable name="ProductName" />" />
        </actinic:block>
        
        <actinic:block if="%3cactinic%3avariable%20name%3d%22IsPopUpDisplayedByImage%22%20%2f%3e">
           </a>
        </actinic:block>
        Now use any image you like and Actinic will rescale it to 100 pixels wide and save the new image as r_imagename instead of the original image.

        If you use this, you'll have to find any other places that product images are displayed (like the Search Results, Marketing, etc) and add in the r_ prefix to the filename.

        Note that the imagejpeg function can take an additional argument that defines the JPEG quality. We're using the default here which should be OK for web use. See the PHP documentation for more info on this.
        Norman - www.drillpine.biz
        Edinburgh, U K / Bitez, Turkey

        Comment


          #5
          Brilliant - I will give it a go when I get back to the office in August.

          Comment


            #6
            Nice one Norman. Just let me know if you need more ideas what can be done by using v8. ;-)
            Zoltan
            Actinic Software
            www.actinic.co.uk

            Comment


              #7
              Hi, Zoltan

              Some guidance as to what

              encoding="perl"
              selectable="false"

              do and what other options are available within Variables would be useful.
              Norman - www.drillpine.biz
              Edinburgh, U K / Bitez, Turkey

              Comment


                #8
                Originally posted by NormanRouxel
                this image is displayed (necessary so that Actinic will upload it), but all you need to do is add some CSS to the <img...> tag to make this hidden and it will still be uploaded.
                Hi Norman,

                whenever I add a class="n" to the <img /> tag or even <div> span the <img /> tag I receive "Parse error : parse error, expecting '"," or '";" in main on line 27" ..... this is even if the .class has not been declared in the actinic.css file.

                The main image remains but the thumbnail is replaced with the parse error.


                Bikster
                SellerDeck Designs and Responsive Themes

                Comment


                  #9
                  Hi, Jont

                  If this is my first Thumbnail example, we're evaluating a quoted PHP string at that point to you probably need to escape any double quotes.

                  e.g.

                  class=\"myclass\"

                  in the second example I changed things to multiple echo statments using single quoted strings to make it easier to follow.
                  Norman - www.drillpine.biz
                  Edinburgh, U K / Bitez, Turkey

                  Comment


                    #10
                    Originally posted by NormanRouxel
                    evaluating a quoted PHP string at that point to you probably need to escape any double quotes.
                    Thanks Norman.

                    Have pretty much avoided PHP in the main so far .. with v8 now allowing easy integration it into the variables etc this is now a must learn language.


                    Bikster
                    SellerDeck Designs and Responsive Themes

                    Comment


                      #11
                      Norman, sorry for the belated answer, but I'm not monitoring actively the community.

                      encoding="perl"
                      The variable references in the markup got this attribute. You can set if the substituted variable needs to be encoded on some way. Perl encoding means: make the string safe for insertion into a perl string in single quotes by escaping single quotes, <CR>s, <NL>s , and backslashes with backslashes. Therefore if you are inserting an Actinic text variable into your PHP code, then use single quotes and set this attribute on the vartiable otherwise you will get PHP syntax errors.

                      selectable="false"
                      Preview generation encloses the substituted variable values by span tags to support point and click editing. These span tags don't make sense in the PHP code and may mess the syntax up in design mode. Therefore it should be switced off in PHP fragments.
                      Zoltan
                      Actinic Software
                      www.actinic.co.uk

                      Comment


                        #12
                        do and what other options are available within Variables would be useful.
                        Sorry, I have missed this bit last night. If you right click on the variable in the code view and select "Edit appearance" you will see the rest of oftions in the grid.
                        Zoltan
                        Actinic Software
                        www.actinic.co.uk

                        Comment


                          #13
                          Hi Norman,

                          I am having trouble with this as my product images are in a sub folder and therefore the new image name is "r_/graphics/products/foo.jpg", which is invalid.

                          I tested out moving changing the format of the new item to: $sResizedImageName = $sOriginalImageName .'.php.jpg';

                          this works and generates the file, but of course its not a very nice looking filename. As the php expert, I wondered if its possible to strip the .jpg (or whatever) of the filename and then add something and put it back so the filename would end up as: foo_php.jpg instead of foo.jpg_php.jpg as I have it now.

                          Thanks,
                          Paul.

                          P.S. Thanks for this php script, its exactly what I need!

                          Comment


                            #14
                            Try replacing (NB unchecked untested)
                            Code:
                            	$sOriginalImageName = '<actinic:variable encoding="perl" name="ProductImageFileName"  selectable="false" />';
                            	$sResizedImageName = 'r_' . $sOriginalImageName;
                            with
                            Code:
                            	$sOriginalImageName = '<actinic:variable encoding="perl" name="ProductImageFileName"  selectable="false" />';
                            	if ( strpos($sOriginalImageName, "/") === false )					// if Site1
                            		{
                            		$sResizedImageName = 'r_' . $sOriginalImageName;				// simple name change
                            		}
                            	else
                            		{
                            		$sResizedImageName = preg_replace('%(.*/)%', '$1r_', $sOriginalImageName);	// change filename only
                            		}
                            Norman - www.drillpine.biz
                            Edinburgh, U K / Bitez, Turkey

                            Comment

                            Working...
                            X