Jump to content

Recommended Posts

Purpose:

The following is some very rough code in the attempt to grab images off google search and shove them in a mysql database. This is Just a test run for a learning algorithm. Final will not keep images, just scan them

 

State of code:

So far code works to the point that it will grab images and put the URL in the database then display the images(not extracted from database).

 

Need:

I would like to put the images themselves in the database but cannot find a successful way to do so. Any help is greatly appreciated.

 

 <?php
               
                        // get list of images from google images
                         // Create MySQL login values and
                // set them to your login information.
                $username = "*******";
                $password = "**********";
                $host = "*******";
                $database = "binary";
               
                // Make the connect to MySQL or die
                // and display an error.
                $link = mysql_connect($host, $username, $password);
                if (!$link) {
                        die('Could not connect: ' . mysql_error());
                }
               
                // Select your database
                mysql_select_db ($database);  
                       
                       
                $k='*****some query****';
                $url = "http://www.google.com/images?hl=en&source=imghp&biw=1920&bih=859&q=##query##&gbv=2&aq=f&aqi=g10&aql=&oq=&gs_rfai=";
                $web_page = file_get_contents( str_replace("##query##",urlencode($k), $url ));

                $tieni = stristr($web_page,"dyn.setResults(");
                $tieni = str_replace( "dyn.setResults(","", str_replace(stristr($tieni,");"),"",$tieni) );
                $tieni = str_replace("[]","",$tieni);
                $m = preg_split("/[\[\]]/",$tieni);
                $x = array();
                for($i=0;$i<count($m);$i++) {
                        $m[$i] = str_replace("/imgres?imgurl\\x3d","",$m[$i]);
                        $m[$i] = str_replace(stristr($m[$i],"\\x26imgrefurl"),"",$m[$i]);
                        $m[$i] = preg_replace("/^\"/i","",$m[$i]);
                        $m[$i] = preg_replace("/^,/i","",$m[$i]);
                        if ($m[$i]!="")
                        {
                                $tmpName = $m[$i];
                               
                                // Create the query and insert
                                // into our database.
                                $query = "INSERT INTO tbl_images ";
                                $query .= "(image) VALUES ('$tmpName')";
                                $results = mysql_query($query, $link);
                                //
                                echo "<img src='".$m[$i]."' />";
                                array_push($x,$m[$i]);
                        }
                       
                }
            $ar = $x;
        ?>

Link to comment
https://forums.phpfreaks.com/topic/214152-web-image-to-mysql-via-php/
Share on other sites

    * your MySQL server version --5.1.50

 

    * the raw MySQL statement in question [in a CODE block, and without any PHP variables]

     

  INSERT INTO tbl_images ##image## VALUES ##URL## ;

                    -currently the URL is being put in the database but I would like to put the whole image file in it.

 

    * the table structure & column indexes of the relevant tables [via SHOW CREATE TABLE is preferred]

CREATE TABLE `tbl_images` (
    `id` TINYINT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `image` BLOB NOT NULL,
    PRIMARY KEY (`id`)
)

 

  • 4 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.