Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrew-serrano/bfe17f08a8f2f1fe30019d502558de45 to your computer and use it in GitHub Desktop.
Save andrew-serrano/bfe17f08a8f2f1fe30019d502558de45 to your computer and use it in GitHub Desktop.
Script is used to resize a single image to a specific dimension. Used for a google product feed.
<mvt:comment> <mvt:assign name="g.Product_Code" value="'34072'" /> </mvt:comment>
<mvt:comment>
***
Image Resizer - Start
***
Image resizer is being used for google product feed form.
</mvt:comment>
<mvt:comment> Load Single Product </mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.success" value="Product_Load_Code(g.Product_Code, l.settings:product)" />
<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">
No Image found
<mvt:exit/>
</mvt:if>
<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>
SUCCESS!
<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