Uncategorized

Displaying base64 image data from DB using PHP

This is based on a question from stackoverflow that I answered.

Imagine we store within a database image data as the following:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD...

After retrieving the data from MySQL how do we display it?

Convert base64 data to image:

In this case you need to use the header function to tell the browser you will send an image.
Following, you take the $data variable that you set from a MySQL query, and use base64_decode to display.

header("Content-type: image/gif");
$data = "/9j/4AAQSkZJRgABAQEAYABgAAD........";
echo base64_decode($data);

Clients request .php to display image:

In this case you want to encode the image data directly into a PHP generated page you would do the following:

echo '<img src="data:image/gif;base64,' . $data . '" />';

There is a downside to this in that the browser does not cache an image that is used on multiple separate pages (since each page contains the binary data within the HTML document).The second case is bad because the browser does not perform caching if the same image is shown on multiple pages.

References:

 

Retrieve images of chemical structures using Excel VBA

The below is from an answer I posted on stackoverflow.

You can retrieve the chemical structure of an image using the following:

Sub Run()
getImage ("iron")
End Sub

Public Function getImage(ByVal name As String) As String
  Dim imgURL As String
  Dim XMLhttp: Set XMLhttp = CreateObject("MSXML2.ServerXMLHTTP")
  XMLhttp.setTimeouts 1000, 1000, 1000, 1000
  imgURL = "http://cactus.nci.nih.gov/chemical/structure/" + name + "/image"

  XMLhttp.Open "GET", imgURL, False
  XMLhttp.send

  If XMLhttp.Status = 200 Then
   'It exists so get the image
    Sheets(1).Shapes.AddPicture imgURL, msoFalse, msoTrue, 100, 100, 250, 250
  Else
    '
  End If
End Function

This can further simplified to simply only use

Sheets(1).Shapes.AddPicture imgURL, msoFalse, msoTrue, 100, 100, 300, 300

Instead of downloading the image the twice, and simply using an error handler to catch when image not found.

Reference:

 

WordPress Permalink Migration Warning (SEO semi-disaster)

If you follow those guides about migrating your blog to another webhost using import/export functionally…
You must rebuild your permalinks in the exact same format as the original webhost!

Go to Settings > Permalinks, and click save changes.
Make sure you choose the correct permalink structure.

If you do not, all your indexed pages on search engines (google, etc) will disappear and your site will be re-indexed losing you your traffic.

Accessing MYSQL server behind firewall using SSH

So, you want to access a MYSQL server but you only have an SSH terminal…
Not to worry, you can use SSH tunnelling to do this!

Basic Example

Example: ssh -L 3306:localhost:3306 user@IP -p SSHPORT

Now how do you control this dark ssh sorcery? Let’s break it down.

1) 3306:localhost:3306

  •  Open a port 3306 on my local machine to redirect
  • localhost:3306 means we are tunnelling to 3306 on SSH machine

2) user@IP

This is your username and the IP address of the server

3) -p SSHPORT

This is the ssh port if it is not on 22.

Another Example

ssh -L 9000:192.168.212.1:5000 user@192.168.2.1 -p 1234

You would use this if you wanted to:

  • use your computer port 9000
  • to access the port 5000 on IP 192.168.212.1
  • through the SSH server 192.168.2.1
  • with SSH server port on 1234

Happy SSHing!

 

Connecting to JMX from visualVM using SSH tunnel

I used the following source:

https://bowerstudios.com/node/731

I did this in windows using git bash that has ssh command. You can also do using cygwin or pure minggw.

1) Run ssh tunnel command in command prompt (I do this in git bash/MINGGW32).

ssh -D 9010 -p 22 root@IP -v

2) Run your application on server with JMX options

java -Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.port=9010 
-Dcom.sun.management.jmxremote.local.only=false 
-Dcom.sun.management.jmxremote.authenticate=false 
-Dcom.sun.management.jmxremote.ssl=false -jar 
application.jar

3) Run visualVM through socks proxy to connect:

visualvm -J-Dnetbeans.system_socks_proxy=localhost:9010 
-J-Djava.net.useSystemProxies=true

4) Actually add your JMX remote connection in visualVM

MysqlDump directly on mounted remote directory (CENTOS, Linux, Unix)

If you don’t have space to write a dump file, or want to directly send your SQL to another server (without intermediate transfers), using a remotely mounted folder is a way to go.

There are various mounting options such as:

  • SSHFS
  • NFS
  • Windows Shares (SAMBA)

More

Repeating a unix command every X seconds

The watch command can be used to repeat a command every X seconds.

Examples:

Monitor Disk Usage (updated per second)

watch -n1 df -h

DiskPrint Hello World (every second)

watch -n1 echo “Hello World”

Reference: 

http://unix.stackexchange.com/questions/10646/repeat-a-unix-command-every-x-seconds-forever

Java application not sending e-mail with gmail

Suddenly my java application for sending student attendance reports threw the following exception:

sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

It turned out that my avast firewall was interfering so I would propose to all those having this problem to disable any anti-viruses or firewalls temporarily and try again.

X11a Netbook Drivers

I’ve noticed many posts on forums from users asking for X11a drivers.

Below are the drivers I have been able to find using the hardware IDs displayed in the device manager.

IDs

 

Graphics card:

Mobile Intel(R) 945 Express Chipset Family
https://downloadcenter.intel.com/Detail_Desc.aspx?agr=Y&DwnldID=16835&lang=eng

 Audio Driver:

http://www.realtek.com.tw/downloads/downloadsview.aspx?Langid=1&PNid=14&PFid=24&Level=4&Conn=3&DownTypeID=3&GetDown=false

Dealing with: pci system error on bus/device/funtion 000h

Dealing with

pci system error on bus/device/funtion 000h

Possible Solution

1.to reset the bios 
2.remove the battery 
3.add the battery again. 

It is likely there is a hardware issue or shortcircuit on the motherboard.

Note:

I relieved this error on a net book both on Windows XP, and with Lubuntu USB disks. Unofrtunately, the netbook died after a few months.