HTML with Embedded Images from PL/SQL
Use PL/SQL to create HTML with embedded images.
oracle miscconfigurationintermediate
by OracleDba
12 views
Use PL/SQL to create HTML with embedded images.
123
<img src="https://oracle-base.com/images/site_logo.gif" />
<img src="data:<mimetype>;base64,<data>" />1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
conn sys/SysPassword1@//localhost:1521/pdb1 as sysdba
create or replace directory images as '/tmp/';
grant read, write on directory images to testuser1;
conn testuser1/testuser1@//localhost:1521/pdb1
create table images (
id number(10) not null,
name varchar2(50) not null,
image blob not null,
constraint images_pk primary key (id),
constraint images_uk unique (name)
);
create sequence images_seq;
declare
l_dir varchar2(10) := 'IMAGES';
l_file varchar2(20) := 'site_logo.gif';
l_bfile bfile;
l_blob blob;
l_dest_offset integer := 1;
l_src_offset integer := 1;
begin
insert into images (id, name, image)
values (images_seq.nextval, l_file, empty_blob())
return image into l_blob;
l_bfile := bfilename(l_dir, l_file);
dbms_lob.fileopen(l_bfile, dbms_lob.file_readonly);
-- loadfromfile deprecated.
-- dbms_lob.loadfromfile(l_blob, l_bfile, dbms_lob.getlength(l_bfile));
dbms_lob.loadblobfromfile (
dest_lob => l_blob,
src_bfile => l_bfile,
amount => dbms_lob.lobmaxsize,
dest_offset => l_dest_offset,
src_offset => l_src_offset);
dbms_lob.fileclose(l_bfile);
commit;
end;
/
create or replace procedure create_file_from_clob (p_dir in varchar2,
p_file in varchar2,
p_clob in out nocopy clob)
as
l_file utl_file.file_type;
l_step pls_integer := 12000;
begin
l_file := utl_file.fopen(p_dir, p_file, 'w', 32767);
for i in 0 .. trunc((dbms_lob.getlength(p_clob) - 1 )/l_step) loop
utl_file.put(l_file, dbms_lob.substr(p_clob, l_step, i * l_step + 1));
utl_file.fflush(l_file);
end loop;
utl_file.fclose(l_file);
end;
/
create or replace function base64encode(p_blob in blob)
return clob
is
l_clob clob;
l_step pls_integer := 12000; -- make sure you set a multiple of 3 not higher than 24573
begin
for i in 0 .. trunc((dbms_lob.getlength(p_blob) - 1 )/l_step) loop
l_clob := l_clob || utl_raw.cast_to_varchar2(utl_encode.base64_encode(dbms_lob.substr(p_blob, l_step, i * l_step + 1)));
end loop;
return l_clob;
end;
/1234567891011121314151617
create or replace procedure get_enc_img_from_fs (p_dir in varchar2,
p_file in varchar2,
p_clob in out nocopy clob)
as
l_bfile bfile;
l_step pls_integer := 12000;
begin
l_bfile := bfilename(p_dir, p_file);
dbms_lob.fileopen(l_bfile, dbms_lob.file_readonly);
for i in 0 .. trunc((dbms_lob.getlength(l_bfile) - 1 )/l_step) loop
p_clob := p_clob || utl_raw.cast_to_varchar2(utl_encode.base64_encode(dbms_lob.substr(l_bfile, l_step, i * l_step + 1)));
end loop;
dbms_lob.fileclose(l_bfile);
end;
/1234567891011121314151617181920212223242526272829
create or replace procedure get_enc_img_from_http (p_url in varchar2,
p_clob in out nocopy clob)
as
l_http_request utl_http.req;
l_http_response utl_http.resp;
l_raw raw(32767);
begin
l_http_request := utl_http.begin_request(p_url);
l_http_response := utl_http.get_response(l_http_request);
begin
loop
utl_http.read_raw(l_http_response, l_raw, 12000);
p_clob := p_clob || utl_raw.cast_to_varchar2(utl_encode.base64_encode(l_raw));
end loop;
exception
when utl_http.end_of_body then
utl_http.end_response(l_http_response);
end;
end;
/
create or replace procedure get_enc_img_from_http (p_url in varchar2,
p_clob in out nocopy clob)
as
begin
p_clob := p_clob || base64encode(httpuritype.createuri(p_url).getblob());
end;
/12345678910
create or replace procedure get_enc_img_from_tab (p_image_name in varchar2,
p_clob in out nocopy clob)
as
begin
select p_clob || base64encode(image)
into p_clob
from images
where name = p_image_name;
end;
/12345678910111213141516171819202122232425262728293031323334353637383940414243444546
declare
l_clob clob;
begin
dbms_lob.createtemporary(l_clob, FALSE);
-- Build the start of the HTML document, including the start of the IMG tag
-- and place it in a CLOB.
l_clob := '<html>
<head>
<title>Test HTML with Embedded Image</title>
</head>
<body>
<h1>Test HTML with Embedded Image</h1>
<p>And here it is:</p>
<img src="data:image/gif;base64,';
get_enc_img_from_fs (p_dir => 'IMAGES',
p_file => 'site_logo.gif',
p_clob => l_clob);
--get_enc_img_from_http (p_url => 'https://oracle-base.com/images/site_logo.gif',
-- p_clob => l_clob);
--get_enc_img_from_tab (p_image_name => 'site_logo.gif',
-- p_clob => l_clob);
-- Close off the IMG tag and complete the HTML document.
l_clob := l_clob || '" alt="Site Logo" />
<p>The end.</p>
</body>
</html>';
-- The CLOB now contains the complete HTML with the embedded image, so do something with it.
-- In this case I'm going to write it to the file system.
create_file_from_clob (p_dir => 'IMAGES',
p_file => 'EmbeddedImageTest.htm',
p_clob => l_clob);
dbms_lob.freetemporary(l_clob);
exception
when others then
dbms_lob.freetemporary(l_clob);
raise;
end;
/Please to add comments
No comments yet. Be the first to comment!