Scripts to write image information to JSON file

These are scripts used to save image information into the images_list.json file required for the Image Browser. You may elect to use or not use database tables to store the image information. The only feature lost if no table are used is the ability to separate images in to categories in the Image Browser popup. The table names, if you elect to use them are wt_images and wt_image_groups. Only DBF tables are provided in our example so you would have to recreate them in SQL if needed. Please see comments in each example script for additional customization.

Electing to use tables also has the nice effect of creating an "image library" where all of your images are listed in your application.

You would need to add a button in your application to fire the appropriate script. Although not provided in this CKEditor project example, you might add an Image Upload option to your application and fire this script automatically after a new image has been uploaded.

The xbasic function is named post_images in all cases below.

Xbasic function if NO tables are used to store image information.

Xbasic function if DBF tables are used to store image information.

Xbasic funcition if SQL tables are used to store image information.


Xbasic function if NO tables are used to store image information


'==Use this = "images"

dim valid_img as c         = ".jpg,.png,.gif"

dim app_url asfunction if you are NOT using the two tables starting with wt_image. 

'==This script does not include 'categories' in the Image Browser. 

'==list the images to 'omit' from the Image Browser in the variable below

function post_images as c (e as p)

dim img_fldr as c         c        = Request.ApplicationRootUrl

dim app_fldr as c         = Request.ApplicationRoot

dim thumb_prefix as c = "thumb_"

dim omit_lst as c         = <<%txt%

colorful_skin.gif

gray_skin.gif

%txt%


if a5_is_path_valid(img_fldr) = .f.

       post_images = "alert('Error - Invalid image path defined in postImagesToDatabase function');"

       END

end if


lst = filefind.get(img_fldr + chr(92) + "*.*",FILE_FIND_NOT_DIRECTORY,"N")


dim lst2 as c = ""

for each foo in lst

       if inlist2(file.filename_parse(foo,"E"),valid_img)

               if at(thumb_prefix,foo) <> 1

                       lst2 = lst2 + foo + crlf()

               end if

       end if

next


'==write json

for each foo in lst2

       if is_one_of(foo,crlf_to_comma(omit_lst))

               continue

       end if

       thmb = .f.

       if file.exists(app_fldr + img_fldr + chr(92) + thumb_prefix + foo)

               thmb = .t.

       end if

       dim lst3 as c

       lst3 = lst3 + chr(9) + "{" + crlf()

       lst3 = lst3 + chr(9) + chr(9) + "\"image\": \"" + app_url + img_fldr + "/" + alltrim(foo)+"\"" + if(thmb = .t.,",","") + crlf()

       if thmb = .t.

               lst3 = lst3 + chr(9) + chr(9) + "\"thumb\": \"" + app_url + img_fldr + "/" + thumb_prefix + alltrim(foo)+"\"" + crlf()

       end if

       lst3 = lst3 + chr(9) + "}," + crlf()

next


lst3 = strip_trailing_char(lst3,",")

lst3 = "[" + crlf() + chr(9) + lst3 + crlf() + "]" + crlf()

save_to_file(lst3, img_fldr + chr(92) + "images_list.json")


post_images = "alert('Complete.');"


end function 




Xbasic function if DBF tables are used to store image information


'==Use this function if you ARE using the two tables starting with wt_image AND using DBF tables. 

'==This script includes 'categories' in the Image Browser. 

'==You can designate images to 'omit' in the wt_images table

function post_images as c (e as p)


dim img_fldr as c        = "images"

dim valid_img as c         = ".jpg,.png,.gif"

dim app_url as c        = Request.ApplicationRootUrl

dim app_fldr as c         = Request.ApplicationRoot

dim thumb_prefix as c = "thumb_"

if a5_is_path_valid(img_fldr) = .f.

       post_images = "alert('Error - Invalid image path defined in postImagesToDatabase function');"

       END

end if


lst = filefind.get(img_fldr + chr(92) + "*.*",FILE_FIND_NOT_DIRECTORY,"N")


dim lst2 as c = ""

for each foo in lst

       if inlist2(file.filename_parse(foo,"E"),valid_img)

               if at(thumb_prefix,foo) <> 1

                       lst2 = lst2 + foo + crlf()

               end if

       end if

next 


dim tbl as p

tbl = table.open("[PathAlias.ADB_Path]\wt_image")

tbl.index_primary_put("Filename")


'==remove record if no image file

tbl.fetch_first()

while .not. tbl.fetch_eof()

       if file.exists(img_fldr + chr(92) + tbl.filename) = .f.

               tbl.change_begin()

               tbl.delete()

               tbl.change_end(.t.)

       end if

       tbl.fetch_next()

end while        


'==add image names


for each foo in lst2

       recnum = tbl.fetch_find(alltrim(foo))

       if file.exists(img_fldr + chr(92) + thumb_prefix + alltrim(foo))

               tbl.change_begin()

               tbl.thumb = .t.

               tbl.change_end()

       end if

       if recnum > 0 ' do nothing

               else

               tbl.enter_begin(.t.)

               tbl.filename = foo

               if file.exists(img_fldr + chr(92) + thumb_prefix + alltrim(foo))

                       tbl.thumb = .t.

               end if

               tbl.enter_end()

       end if 

next

'==write json

tbl.fetch_first()

dim lst3 as c

while .not. tbl.fetch_eof()

       if tbl.omit <> .t.

               thmb = .f.

               if file.exists(app_fldr + img_fldr + chr(92) + thumb_prefix + tbl.filename)

                       thmb = .t.

               end if

               lst3 = lst3 + chr(9) + "{" + crlf()

               lst3 = lst3 + chr(9) + chr(9) + "\"image\": \"" + app_url + img_fldr + "/" + alltrim(tbl.filename)+"\"," + crlf()

               if thmb = .t.

                       lst3 = lst3 + chr(9) + chr(9) + "\"thumb\": \"" + app_url + img_fldr + "/" + thumb_prefix + alltrim(tbl.filename)+"\"," + crlf()

               end if

               if tbl.image_group <> ""

                       lst3 = lst3 + chr(9) + chr(9) + "\"folder\": \""+alltrim(tbl.image_group)+"\"" + crlf()

               end if                        

               lst3 = strip_trailing_char(lst3,",")

               lst3 = lst3 + chr(9) + "}," + crlf()

       end if

       tbl.fetch_next()

end while


tbl.close()


lst3 = strip_trailing_char(lst3,",")

lst3 = "[" + crlf() + chr(9) + lst3 + crlf() + "]" + crlf()

save_to_file(lst3, img_fldr + chr(92) + "images_list.json")


post_images = "alert('Complete.');"


end function 



Xbasic funcition if SQL tables are used to store image information