Skip to content

Instantly share code, notes, and snippets.

@andrew-serrano
Created May 22, 2019 22:28
Show Gist options
  • Save andrew-serrano/697bf279dfbd21dba19effca8698a3a1 to your computer and use it in GitHub Desktop.
Save andrew-serrano/697bf279dfbd21dba19effca8698a3a1 to your computer and use it in GitHub Desktop.
Script is used to resize images to a specific dimension. Used for a google product feed.
<mvt:comment> Debug Helper Functions </mvt:comment>
<mvt:assign name="l.tab" value="asciichar( 9 )" />
<mvt:assign name="l.line_feed" value="asciichar( 10 )" />
<mvt:do file="g.Module_Library_Utilities" name="l.timestamp" value="Timestamp_ISO8601(s.time_t)" />
<mvt:comment> Redirect URL </mvt:comment>
<mvt:assign name="g.url" value="glosub( g.domain:base_surl, 'mm5/', '' ) $ 'generate-product-images'" />
<mvt:comment> Debug Log Vars </mvt:comment>
<mvt:assign name="l.debug_log_path" value="'/google_product_feed_image_generator_log.txt'" />
<mvt:comment> Collect debug log data </mvt:comment>
<mvt:assign name="l.header" value="
'PRODUCT_CODE' $ l.tab $
'FILE_PATH' $ l.tab $
'SAVED/WRITE' $ l.tab $
'UPLOADED_DB' $ l.tab $
'IMAGE_FOUND' $ l.tab $
'UPLOADED_PROD_DB' $ l.tab $
'DATE' $ l.line_feed
" />
<mvt:comment>
***
Image Resizer - Start
***
Image resizer is being used for google product feed form.
</mvt:comment>
<mvt:if expr="ISNULL g.offset">
<mvt:comment> Delete log if it exists </mvt:comment>
<mvt:if expr="sexists( l.debug_log_path ) EQ 1">
<mvt:assign name="g.null" value="sdelete( l.debug_log_path )" />
</mvt:if>
<mvt:comment>Create log with header row</mvt:comment>
<mvt:assign name="g.null" value="file_create( l.debug_log_path , 'script', l.header)" />
<mvt:comment> Set offset to 0 </mvt:comment>
<mvt:assign name="g.offset" value="0" />
<mvt:else>
<mvt:comment> Else continue </mvt:comment>
<mvt:assign name="g.offset" value="g.offset" />
</mvt:if>
<mvt:comment> Load all products </mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.success" value="ProductList_Load_Offset(g.offset ,'', '', 200 , g.nextOffset , l.settings:products )" />
<mvt:comment> Iterate over products </mvt:comment>
<mvt:foreach iterator="product" array="products">
<mvt:comment> NULL Vars </mvt:comment>
<mvt:assign name="l.image_type" value="NULL" />
<mvt:assign name="l.image_data" value="NULL" />
<mvt:assign name="l.image_file_relative_path" value="''" />
<mvt:assign name="l.main_image_file_path" value="''" />
<mvt:assign name="l.isJPG" value="''" />
<mvt:assign name="l.isGIF" value="''" />
<mvt:assign name="l.isPNG" value="''" />
<mvt:assign name="l.image_file_type" value="''" />
<mvt:assign name="l.image_file_new_name" value="''" />
<mvt:assign name="l.image" value="''" />
<mvt:assign name="l.productimage" value="''" />
<mvt:assign name="l.body" value="''" />
<mvt:assign name="l.adjusted_height" value="0" />
<mvt:assign name="l.adjusted_width" value="0" />
<mvt:comment> Width/Height helper variables </mvt:comment>
<mvt:comment> Default Height/Width </mvt:comment>
<mvt:assign name="l.height" value="0" />
<mvt:assign name="l.width" value="0" />
<mvt:comment> Multiply By </mvt:comment>
<mvt:assign name="l.h_multiplier" value="1" />
<mvt:assign name="l.w_multiplier" value="1" />
<mvt:comment> Read customfield for short desc </mvt:comment>
<mvt:item name="customfields" param="Read_Product_ID(l.settings:product:id , 'short_desc' , g.short_desc)" />
<mvt:comment> Load image data by code </mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.thumbnail_image_exists" value="ImageType_Load_Code('thmb', l.image_type)" />
<mvt:comment> Load image type </mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.success" value="ProductImage_Load_Type(l.settings:product:id, l.image_type:id , l.image_data)" />
<mvt:comment> If we don't have a thumbnail image exit </mvt:comment>
<mvt:if expr="NOT ISNULL l.image_data">
<mvt:comment> Load image </mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.success" value="Image_Load_ID(l.image_data:image_id, l.product_image)" />
<mvt:else>
<mvt:comment> Create file path referencing the product page </mvt:comment>
<mvt:assign name="l.main_image_file_path" value="'graphics/00000001/' $ g.short_desc $ '.jpg'" />
<mvt:comment> Load in image by file path </mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.success" value="Image_Load_File(l.main_image_file_path, l.product_image)" />
</mvt:if>
<mvt:if expr="ISNULL l.product_image">
<mvt:comment> Don't do anything </mvt:comment>
<mvt:else>
<mvt:comment> Image Relative Path Set </mvt:comment>
<mvt:assign name="l.image_file_relative_path" value="'/mm5/' $ l.product_image:image" />
<mvt:comment> Image file type </mvt:comment>
<mvt:assign name="l.isJPG" value="indexof('jpg', l.product_image:image , 1 )" />
<mvt:assign name="l.isGIF" value="indexof('gif', l.product_image:image , 1 )" />
<mvt:assign name="l.isPNG" value="indexof('png', l.product_image:image , 1 )" />
<mvt:comment> Determine which file image type it is </mvt:comment>
<mvt:if expr="l.isJPG NE 0">
<mvt:assign name="l.image_file_type" value="'jpg'" />
<mvt:elseif expr="l.isGIF NE 0">
<mvt:assign name="l.image_file_type" value="'gif'" />
<mvt:elseif expr="l.isPNG NE 0">
<mvt:assign name="l.image_file_type" value="'png'" />
</mvt:if>
<mvt:comment> Height </mvt:comment>
<mvt:if expr="l.product_image:height LT 125">
<mvt:comment> Height has been adjusted </mvt:comment>
<mvt:assign name="l.adjusted_height" value="1" />
<mvt:comment> While image height less than or equal to 125 continue...</mvt:comment>
<mvt:while expr="l.height LE 125">
<mvt:comment> Increment by 1 </mvt:comment>
<mvt:assign name="l.h_multiplier" value="l.h_multiplier + 1" />
<mvt:comment> Adjusted height </mvt:comment>
<mvt:assign name="l.height" value="l.product_image:height * l.h_multiplier" />
</mvt:while>
</mvt:if>
<mvt:comment> Width </mvt:comment>
<mvt:if expr="l.product_image:width LT 125">
<mvt:comment> width has been adjusted </mvt:comment>
<mvt:assign name="l.adjusted_width" value="1" />
<mvt:comment> While image width less than or equal to 125 continue...</mvt:comment>
<mvt:while expr="l.width LE 125">
<mvt:comment> Increment by 1 </mvt:comment>
<mvt:assign name="l.w_multiplier" value="l.w_multiplier + 1" />
<mvt:comment> Adjusted width </mvt:comment>
<mvt:assign name="l.width" value="l.product_image:width * l.w_multiplier" />
</mvt:while>
</mvt:if>
<mvt:comment> Height didn't get adjusted </mvt:comment>
<mvt:if expr="l.adjusted_height EQ 0">
<mvt:assign name="l.height" value="l.product_image:height * l.w_multiplier" />
</mvt:if>
<mvt:comment> Width didn't get adjusted </mvt:comment>
<mvt:if expr="l.adjusted_width EQ 0">
<mvt:assign name="l.width" value="l.product_image:width * l.h_multiplier" />
</mvt:if>
<mvt:comment> Create a file </mvt:comment>
<mvt:do file="g.Module_Library_Utilities" name="l.gd_image" value="Image_gdCreateFromFile_Type( l.image_file_type , l.image_file_relative_path , 'script')" />
<mvt:comment> Resize the file </mvt:comment>
<mvt:do file="g.Module_Library_Utilities" name="l.successfully_resized" value="Image_ResizeIntoBoundingBox_gdImage( l.gd_image , l.width , l.height , l.gd_resized , l.generated_width , l.generated_height )" />
<mvt:comment> If we have an succesully resized image continue </mvt:comment>
<mvt:if expr="l.successfully_resized">
<mvt:comment> Create new name and add width x height prefix EX: /mm5/graphics/00000003/hflvykipmc5g22mc3m0m_600x600.jpg </mvt:comment>
<mvt:if expr="NOT ISNULL l.generated_width AND NOT ISNULL l.generated_height">
<mvt:comment>
Add short description as photo name
else add product name as photo
</mvt:comment>
<mvt:if expr="NOT ISNULL g.short_desc">
<mvt:assign name="l.image_file_new_name" value="'/mm5/graphics/00000001/' $ glosub( g.short_desc, '/', '') $ '_' $ l.generated_width $ 'x' $ l.generated_height $ '.' $ l.image_file_type" />
<mvt:else>
<mvt:assign name="l.image_file_new_name" value="'/mm5/graphics/00000001/' $ l.settings:product:name $ '_' $ l.generated_width $ 'x' $ l.generated_height $ '.' $ l.image_file_type" />
</mvt:if>
<mvt:comment> l.image width and height </mvt:comment>
<mvt:assign name="l.image:width" value="l.generated_width" />
<mvt:assign name="l.image:height" value="l.generated_height" />
<mvt:else>
<mvt:comment>
Add short description as photo name
else add product name as photo
</mvt:comment>
<mvt:if expr="NOT ISNULL g.short_desc">
<mvt:assign name="l.image_file_new_name" value="'/mm5/graphics/00000001/' $ glosub( g.short_desc, '/', '') $ '_' $ l.width $ 'x' $ l.height $ '.' $ l.image_file_type" />
<mvt:else>
<mvt:assign name="l.image_file_new_name" value="'/mm5/graphics/00000001/' $ l.settings:product:name $ '_' $ l.generated_width $ 'x' $ l.generated_height $ '.' $ l.image_file_type" />
</mvt:if>
<mvt:comment> l.image width and height </mvt:comment>
<mvt:assign name="l.image:width" value="l.width" />
<mvt:assign name="l.image:height" value="l.height" />
</mvt:if>
<mvt:comment> Create image variable to insert into DB </mvt:comment>
<mvt:assign name="l.image:image" value="glosub(l.image_file_new_name, '/mm5/', '')" />
<mvt:comment> Read Image from file path </mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.does_generated_image_exists" value="Image_Load_File(l.image:image, l.null)" />
<mvt:comment> If we can't find the file then insert into DB </mvt:comment>
<mvt:if expr="l.does_generated_image_exists EQ 0">
<mvt:comment> Save/Write to Filepath </mvt:comment>
<mvt:do file="g.Module_Library_Utilities" name="l.generated_image_is_saved" value="Image_gdSaveToFile_Type(l.gd_resized , l.image_file_type , l.image_file_new_name , 'script')" />
<mvt:comment> Image was successfully saved at the file path ( graphics folder ) </mvt:comment>
<mvt:if expr="l.generated_image_is_saved">
<mvt:comment> Insert image into DB </mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.image_successfully_uploaded" value="Image_Insert_LowLevel(l.image)" />
<mvt:comment> Read Image from file path </mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.generated_image_exists" value="Image_Load_File(l.image:image, l.generated_image_loaded_from_filepath)" />
<mvt:comment> If image has been successfully inserted into the DB and found continue... </mvt:comment>
<mvt:comment> Load google product feed image type to insert id </mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.success" value="ImageType_Load_Code('google_product_feed', l.google_image_type)" />
<mvt:comment> Create image variable to insert into product level images </mvt:comment>
<mvt:assign name="l.productimage:product_id" value="l.settings:product:id" />
<mvt:assign name="l.productimage:image_id" value="l.generated_image_loaded_from_filepath:id" />
<mvt:assign name="l.productimage:type_id" value="l.google_image_type:id" />
<mvt:comment> If we have the correct data insert the image at the product level </mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.generated_image_productlevel_saved" value="ProductImage_Insert_LowLevel(l.productimage)" />
</mvt:if>
</mvt:if>
</mvt:if>
<mvt:comment> Collect debug log data </mvt:comment>
<mvt:assign name="l.body" value="
l.settings:product:code $ l.tab $
l.image:image $ l.tab $
l.generated_image_is_saved $ l.tab $
l.image_successfully_uploaded $ l.tab $
l.generated_image_exists $ l.tab $
l.generated_image_productlevel_saved $ l.tab $
l.timestamp $ l.line_feed
" />
<mvt:comment> Append data </mvt:comment>
<mvt:assign name="g.null" value="file_append( l.debug_log_path , 'script' , l.body )" />
</mvt:if>
</mvt:foreach>
<mvt:if expr="g.nextOffset EQ 0">
Finished!
<mvt:exit/>
<mvt:else>
<mvt:comment>Refresh Page</mvt:comment>
<mvt:assign name="g.redirect_url" value="g.url $ '?continue=' $ g.continue $ '&offset=' $ g.nextOffset" />
<mvt:if expr="g.continue EQ 'refresh'">
<meta http-equiv="refresh" content="0;url=&mvt:global:redirect_url;"/>
<mvt:elseif expr="g.continue EQ 'cron'">
<mvt:eval expr="miva_output_header( 'Status', '301 Moved Permanently')" />
<mvt:eval expr="miva_output_header( 'Location', g.redirect_url )" />
</mvt:if>
</mvt:if>
<mvt:comment>
***
Image Resizer - End
***
</mvt:comment>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment