Can not send Captcha image through nusoap webservice

I'm creating a Captcha session by createCaptcha function that i wrote. and in the webservice I call this function to create a Captcha session and image then I want to send Captcha image Or display created Captcha image by webservice that user can send it's value through webservice but I'm facing error. actually instead of a link to that image I get a encoded result when I'm testing it by firefox add-ons soap_client.

any idea where I'm wrong ?

captcha function code :

<?php
session_start();

function createcaptcha($imgname)
{
    /* Attempt to open */
    $im = @imagecreatefromjpeg($imgname);

    /* See if it failed */
    if(!$im)
    {
        /* Create a black image */
        $code=rand(1000,9999);
        $_SESSION["code"]=$code;
        $im = imagecreatetruecolor(50, 24);
        $bg = imagecolorallocate($im, 22, 86, 165);
        $fg = imagecolorallocate($im, 255, 255, 255);
        imagefill($im, 0, 0, $bg);
        /* Output an captcha code */
        imagestring($im, 5, 5, 5,  $code, $fg);
    }

    return $im;
}  

GetCaptcha soap Class :

session_start();
include_once('captcha.php');

class getCaptcha extends DBM
{
    public function getcaptcha()
    {
        //creating and generating captcha image and code
        $img = createcaptcha('captcha.png');


        //encoding to base64
        //$base64 = base64_encode($img);

        $path = $img;
        $type = pathinfo($path, PATHINFO_EXTENSION);
        $data = file_get_contents($path);
        $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

        $save = "/captchafile/captcha.png";
        imagepng($base64, $save);


        $captcha=$save;

        $this->strResult = "";


            $this->strResult.="<captcha>";
            $this->strResult.="<captcha>$captcha</captcha>";
            $this->strResult.="</captcha>";


    }


    function __toString()
    {
        if($this->strResult!=""){
            return $this->strResult;
        }
        return "";
    }


}

Answers


You need to give your imagepng() function another parameter to save the picture then with Get_file_content() function get the content of your image file then encode it to base64 to send via webservice in xml format.

include_once('captcha.php');

class getCaptcha extends DBM
{
    public function getcaptcha($dump)
    {
        //creating and generating captcha image and code
        $img = createcaptcha("Captcha.png");

        imagepng($img,"Captcha.png");
        imagedestroy($img);

        $captchafile = file_get_contents('./Captcha.png');

        //encoding to base64
        $base64 = base64_encode($captchafile);


        $captcha=$base64;

        $this->strResult = "";


            $this->strResult.="<captcha>";
            $this->strResult.="<captcha>$captcha</captcha>";
            $this->strResult.="</captcha>";


    }


    function __toString()
    {
        if($this->strResult!=""){
            return $this->strResult;
        }
        return "";
    }


}

Need Your Help

navigator.notification.confirm() - firing multiple times

javascript json cordova

Hi I have a loop in JSON to retry connection 3x before firing an error however sometimes I have 3-4 JSON requests and all of them can trough an errors so I have 3 alerts in my phonegap app.

Is the application code visible to others when it is run?

python flask

I don't want other people to see my application code. When I host my application, will others be able to see the code that is running?