Announcement

Collapse
No announcement yet.

Generation of Autosized PHP Images

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

    Generation of Autosized PHP Images

    Posted this on the thread about the auto-sizing of product images (by Norman and in the AUG) but likely to get buried so starting new...

    What would force the new generation of the resized images? The thumbs were generated on the first trial upload and work as expected. Just done another upload and all the t_images.jpg are being sent .... the Thumbs folder shows the time and date when the upload procedure was started the second time.

    No work has been done on the product layout, the ProductImage layout, resize script, width variable etc so wondered what would force the generation of all the thumbs again (2983 is no fun).

    Is there any way for the script to check if it already exists and if so ignore?

    Edit ... found "file_exists" .. looks promising .. any pointers welcomed.


    Bikster
    SellerDeck Designs and Responsive Themes

    #2
    Looking at the original code snippet

    HTML Code:
    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 = $nScaledWidth;
    
    $t_height = round($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 "<img src=\"$sThumbImageName\" width=\"$t_width\" height=\"$t_height\" border=\"0\"";
    
    echo " alt=\"<actinic:variable name="ProductName" encoding="strip"/>\" />";
    
    }
    
    }
    would wrapping that into another if else:

    HTML Code:
    if (file_exists($sThumbImageName)) {
    
    // file exists
    
    } else {
    
    .. does that sound feasible before I kill the site on another 45 min site check?


    Bikster
    SellerDeck Designs and Responsive Themes

    Comment


      #3
      What I do in my other patches is:

      If destination file doesn't exist, create it. Done.

      Otherwise:

      Generate the required file to a temp filename.

      Calculate MD5 checksum of temp and existing file.

      If MD5's different, copy temp file to existing.
      Norman - www.drillpine.biz
      Edinburgh, U K / Bitez, Turkey

      Comment


        #4
        Think I have it working - not as elegant as Norman's but bouncing around the sections and looking into the Thumbnails subfolder the time stamps do not change (not tried on a full upload generation as hosts are playing up)

        Code I have used for the Product Image Layout to replace the existing stuff from the AUG / Knowledge Base http://community.actinic.com/showthread.php?t=45409 if of use to others or refining.....

        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 thumbnail image t_ProductImageFileName
        
        $sOriginalImageName = str_replace('\\','/', '<actinic:variable name="ProductImageFileName" encoding="perl" selectable="false" />');
        
        $nScaledWidth = <actinic:variable name="ProductImageScaledWidth" encoding="perl" selectable="false" />;
        
        $sThumbImageName = 'Thumbnails/t_' . basename($sOriginalImageName);
        
        if (file_exists($sThumbImageName)) {
        
        
        echo "<img src=\"$sThumbImageName\"  border=\"0\"";
        
        echo " alt=\"<actinic:variable name="ProductName" encoding="strip"/>\" />";
        
        
        } else {
        
        $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 = $nScaledWidth;
        
        $t_height = round($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 "<img src=\"$sThumbImageName\" width=\"$t_width\" height=\"$t_height\" border=\"0\"";
        
        echo " alt=\"<actinic:variable name="ProductName" encoding="strip"/>\" />";
        
        }
        
        }
        
        }
        
        // 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>
        HTH
        Last edited by KB2; 23-Mar-2010, 01:09 PM. Reason: Updating kb links


        Bikster
        SellerDeck Designs and Responsive Themes

        Comment


          #5
          Confirming the above does indeed work during an Actinic upload.... saved me over 30 mins last night on both generation and uploading of the thumbnails


          Bikster
          SellerDeck Designs and Responsive Themes

          Comment


            #6
            Is there a way to specifically set the default scaled image size to say 130px rather than upscaling the image in layout....
            I think it will be in the php code, but I'm not too hot on that so if somebody could point that out, that would be superb...
            Thanks
            Martin

            Comment


              #7
              The above PHP script essentially works bay taking the default product image (which can be any dimension) and creating a smaller thumbnail version saved in a separate folder ... you can then use either the original or the thumbnail.

              The thumbnail width is defined on line

              Code:
              $nScaledWidth = <actinic:variable name="ProductImageScaledWidth" encoding="perl" selectable="false" />;
              via the variable you have set in the library (ProductImageScaledWidth) to the size you want.


              Bikster
              SellerDeck Designs and Responsive Themes

              Comment

              Working...
              X