HELP!! CURL..Images!!
Posted: Tue Apr 06, 2004 9:42 am
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)
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);
}
?>