我正在研究一个脚本,该脚本使用某个哈希标签从推文中获取图像。通常这个脚本工作得很好,因为它只在专用服务器上实现,其中open_basedir和safe_mode是关闭的问题。与open_basedir共享服务器上的CURLOPT_FOLLOWLOCATION的替代方法ON
但是,现在我试图让这个脚本适用于客户端,该客户端在共享服务器上托管他的网站,其中open_basedir处于打开状态。结果:尝试从Twitter图片网址(几个客户端,如Twitpic,Yfrog)获取重定向的URL时给我一个错误。
这是我的错误:
警告:curl_setopt()[function.curl-SETOPT]:当在safe_mode设置或open_basedir的被设置在CURLOPT_FOLLOWLOCATION不能被激活.....
基本上,脚本会在数据库中生成Tweets,并在其中存储URL。但是,当试图在服务器上创建映像时,它会给我一个结果:如下面脚本中所述的'隐藏',因为它无法从直接URL中找到MIME类型。因此,存储在数据库中的URL需要遵循图像路径来拉取图像并创建它。
我的问题是,有没有办法重写FOLLOWLOCATION部分?我搜索了一个解决方案约3小时,但似乎无法实现它吧...
希望有人能帮助我
脚本:
<?php
# Database gegevens inladen
include('config.php');
$query = mysql_query("select * from tweets where loaded=0 and hidden=0 order by id asc limit ".$maximgload);
if(mysql_num_rows($query) > 0){
while($db = mysql_fetch_object($query)){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $db->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$buffer = curl_exec($ch);
curl_close($ch);
if(!empty($buffer)){
# Afbeelding opslaan
$fp = fopen($imgdir.'/'.$db->id.'.temp', 'x');
fwrite($fp, $buffer);
fclose($fp);
if(!function_exists('mime_content_type')) {
function mime_content_type($filename) {
$mime_types = array(
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
);
$ext = strtolower(array_pop(explode('.',$filename)));
if (array_key_exists($ext, $mime_types)) {
return $mime_types[$ext];
}
elseif (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
}
else {
return 'application/octet-stream';
}
}
}
# Bestand omzetten naar juiste formaat
$mimetype = mime_content_type($imgdir.'/'.$db->id.'.temp');
# Jpg
if($mimetype == 'image/jpeg'){
rename($imgdir.'/'.$db->id.'.temp',$imgdir.'/'.$db->id.'.jpg');
}
# Png
elseif($mimetype == 'image/png'){
rename($imgdir.'/'.$db->id.'.temp',$imgdir.'/'.$db->id.'.png');
}
# Ander (onbekend) formaat? weg er mee!
else{
@unlink($imgdir.'/'.$db->id.'.temp');
@mysql_query("update tweets set hidden='1' where id='".$db->id."'");
$result = 'file '.$db->id.' onbekend';
break;
}
# Thumbnail maken
$source_image = imagecreatefromstring($buffer);
$source_width = imagesx($source_image);
$source_height = imagesy($source_image);
$source_ratio = $source_width/$source_height;
$destination_ratio = $thumbwidth/$thumbheight;
// landscape
if($source_ratio > $destination_ratio){
$temp_width = (int)($source_height * $destination_ratio);
$temp_height = $source_height;
$source_x = (int)(($source_width - $temp_width)/2);
$source_y = 0;
}
// portrait
else {
$temp_width = $source_width;
$temp_height = (int)($source_width * $destination_ratio);
$source_x = 0;
$source_y = (int)(($source_height - $temp_height)/2);
}
$destination_x = 0;
$destination_y = 0;
$source_width = $temp_width;
$source_height = $temp_height;
$new_destination_width = $thumbwidth;
$new_destination_height = $thumbheight;
$thumb = imagecreatetruecolor($thumbwidth,$thumbheight);
imagecopyresampled($thumb,$source_image,$destination_x,$destination_y,$source_x,$source_y,$new_destination_width,$new_destination_height,$source_width,$source_height);
# Thumbnail opslaan
if($mimetype == 'image/jpeg'){
imagejpeg($thumb,$imgdir.'/'.$db->id.'_thumb.jpg');
}
else{
imagepng($thumb,$imgdir.'/'.$db->id.'_thumb.png');
}
# Bijwerken in database
mysql_query("update tweets set loaded='1', mime='".$mimetype."' where id='".$db->id."'");
$result = 'afb '.$db->id.' gemaakt';
}
# Kan url niet openen? Dan uit database gooien
else{
mysql_query("update tweets set hidden='1' where id='".$db->id."'");
$result = 'afb '.$db->id.' hidden';
}
}
}
else{
$result = 'done';
}
echo '<html>
<head>
<meta http-equiv="refresh" content="2">
<title>'.$result.'</title>
</head>
</html>';
?>