HELP!! CURL..Images!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sakaveli
Forum Commoner
Posts: 60
Joined: Tue Apr 06, 2004 9:42 am

HELP!! CURL..Images!!

Post by sakaveli »

hi ppl,

ive been using fsockets to retrieve images from outside webpages, this works fine aslong as there is no proxy server, however ive now got a proxy server and NEED to use curl, ive got no idea how to use it and would be greatful if someone took a look at the code below and pointed out what i need to change.

thanks..
the main function i think im haviong trouble with is loadimage($url)

Code: Select all

<?php
/**
 *	This class is for storing image data with each row in the database.
 *  Tables that contain parts, tyres, and accessories will contain images.
 *  The table MUST have an 'imagetype' column that specifies whether the image is jpg, gif, etc.
 *  The table MUST have an 'image' column that will store the image data.
 */

//include("db.php"); assume DB already loaded

function getImage($id,$table = "item") {
	if ($id == "") {
  		die ("Image not found");
	}

	// image = byte data of the image
	// imagetype = jpg, gif, etc.
	$statement = "SELECT image,imagetype FROM $table WHERE item_id = $id";
	$result = query($statement);

	if($info = mysql_fetch_array($result)) { 
  		header("Content-Type: Image/$info[1]");
  		header("Content-Disposition: filename="image.$info[1]";");
  		echo $info[0];
	} else {
  		echo "Image not found";
	}
}

function saveImage($id,$data,$type,$table = "item") {
	//clean up the data so SQL can understand it.
	echo"saving";
	$data = str_replace("","\",$data);
  	$data = str_replace("'","\''",$data);
  	$result = query("UPDATE $table set imagetype = '$type', image = '$data' where item_id = $id");
}

function loadImage($url) {
	
  if ( !preg_match('/^(http:\/\/)?([\w\-\.]+)\:?([0-9]*)\/(.*)$/', $url, $url_ary) ) {
  print_r(array_values($url_ary));
  echo "Error - invalid image url - $filename";
  }
  $base_get = '/' . $url_ary[4];
  $port = ( !empty($url_ary[3]) ) ? $url_ary[3] : 80;
  //stream_set_timeout($fsock, 60);
if ( !($fsock = fsockopen($url_ary[2], $port, $errno, $errstr)) )
  
{
      echo $data;
	echo "here!";
	echo "<br>Error<br>$errno<br>$errstr\n";
      return false;
    }

  fputs($fsock, "GET $base_get HTTP/1.1\r\n");
  fputs($fsock, "HOST: " . $url_ary[2] . "\r\n");
  fputs($fsock, "Proxy-Connection: close\r\n\r\n");
  
  unset($data);
  while( !feof($fsock) )
    {
      $data .= fread($fsock,1024);
      echo"loadIMAGE2";
    }
  fclose($fsock);
  if (!preg_match('#Content-Length\: ([0-9]+)[^ /][\s]+#i', $data, $file_data1) || !preg_match('#Content-Type\: image/[x\-]*([a-z]+)[\s]+#i', $data, $file_data2))
    {
      echo "Error getting image<br>\n";
      return false;
    }
  $filesize = $file_data1[1];
  $filetype = $file_data2[1];
  
  $data = substr($data, strlen($data) - $filesize, $filesize);
  echo "returning data";
  return $data;
}

function storeImageURL($url,$id,$table = "item") {
	$type = getImageType($url);
	if ($type === FALSE) return FALSE;
	$data = loadImage($url);
	if ($data === FALSE) return FALSE;
	//$type = TRUE;
	//$data = TRUE;
	$rtn = saveImage($id,$data,$type,$table);
}

function getImageType($url) {
	$length = strlen($url);
	$pos = strrpos($url,".");
	if ($pos === FALSE || $length - $pos > 5) return FALSE;// filename.jpeg passes, filename.jpegx fails
	return substr($url,$pos+1,$length - $pos);
}

?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

cURL basics are as follows:
  • create the connection
  • set the connection options
  • execute request

Code: Select all

/**
 * useful function 
 * @param  resource  $cx     connection
 * @param  array       $opts  array of options: array(option_name => option_value)
 * @return  void
 */
function curl_set_options($cx, $opts) {
  foreach($opts as $opt_name => $opt_value)
    if( defined('CURLOPT_' . strtoupper($opt_name) ) )
      curl_setopt($cx, constant( 'CURLOPT_' . strtoupper($opt_name) ), $opt_name);
}

$cx = curl_init($url); // create the connection
curl_set_options($cx, array('returntransfer' => true, 'followlocation' => true)); // set connection options
$result = curl_exec($cx); // execute the request

// now $result holding the response of the remote server: image, html error-page, nothing - whatever.
// check the PHP manual, cURL chapter for more options, such as proxies, authentication etc.
User avatar
sakaveli
Forum Commoner
Posts: 60
Joined: Tue Apr 06, 2004 9:42 am

Post by sakaveli »

hey thanks alot, but which variable holds the url string that i want to retrieve the pic from?? and what exactly should i replace this function with??

what parts of my file should i change?

im new to php and ive tried reading through the manuals, and searching the forum.

could u give a bit more annotation to your code please?

thankyou very much
sak.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

sakaveli wrote: hey thanks alot, but which variable holds the url string that i want to retrieve the pic from?? and what exactly should i replace this function with??
In code I provided the url is stored in $url variable. In your code fetching of the image is enclosed in loadImage function.
sakaveli wrote: what parts of my file should i change?
loadImage function
sakaveli wrote: im new to php and ive tried reading through the manuals, and searching the forum.
If you have some programming background (such as C, Basic, Pascal) you shouldn't have any problems with reading the PHP manual. Concepts behind all that languages are (almost) the same.
Post Reply