EDIT: Replace base64 string that is in the textarea with a URL. The textarea is a WYSIWYG editor (CKEditor). I need to upload an image to the server file system. I'm trying to use this code to convert the string to an actual image and then in the textarea replacing the base64 string with the location of the image on the server (URL).
<cfset image = imageReadBase64(#LocalOccurrence#)>
<cfimage source="#image#"
destination="#save_image_to_this_location#
& #name_of_image#
& #extension_of_image#"
action="write">
Original Question: Using ColdFusion, am trying to find all base64 image strings inside HTML then save each as its own file on the server, create URL, and insert into database. I need help crafting a loop at this point.
I got as far finding a single occurrence of the base64 string with this code:
<cfset textarea_to_search = #form.overview_text#>
<cfset string_base64_header = "base64,">
<cfset string_base64_ending = '"'>
<cfoutput>
<cfset mystart = find(#string_base64_header#, #textarea_to_search#)>
<cfset myend = find(#string_base64_ending#,#textarea_to_search#,#mystart#)>
<cfset my64 = mid(#textarea_to_search#, (#mystart#+7), ((#myend#-7)-#mystart#))>
<span style=font-size:8px;"> #mystart#, #myend#, #my64#</span>
</cfoutput>
Re-wrote the original loop to look like this but it only returns the first occurrence of the base64 string:
<cfset counter = 1>
<cfset my_array =[]>
<cfoutput>
<cfloop condition = "counter LTE 5">
<cfset mystart = find(#string_base64_header#, #textarea_to_search#)>
<cfset myend = find(#string_base64_ending#,#textarea_to_search#,#mystart#)>
<cfset my64 = mid(#textarea_to_search#, (#mystart#+7), ((#myend#-7)-#mystart#))>
<span style=font-size:8px;"> #mystart#, #myend#, #my64#</span>
<cfset ArrayAppend(my_array, #my64#)>
<cfset counter = counter+1>
</cfloop>
<cfdump var = "#my_array#">
</cfoutput>
There are a number of ways you could go about this. Possibly with a regex would be best, though I couldn't get an easy example of this working. Alternatively, you could replace the occurrences in the string as you find them and keep looking until there are none left.
You'd have to do more work than this for error checking / validation, etc, but here's a basic example. Here's the full example.