>我一直遇到一些问题,每当我尝试获取将项目图像下载到服务器的请求时,它都不起作用。它似乎让物品与物品一起显示企鹅,但这只是因为图像在那里。图像将下载到名为 paper/images/%size%/%itemid%
的路径。我不确定权限是否正确,或者它只是显示一个空白页面,如下所示。http://api.frosty.us/avatar/?id=1。正如你在这里看到的,项目在这里 http://api.frosty.us/avatar/paper/image/88/9.png 它得到用户的颜色,头部,颈部,身体,旗帜,照片,面部等。然后它像洋娃娃一样建立它。
这是我的代码
<?php
function downloadPapers($itemId, $paperSizes = array(60, 88, 120, 600)){
foreach($paperSizes as $paperSize){
$avatarPaperUri = sprintf('paper/image/%d/%d.png', $paperSize, $itemId);
$avatarPaperUrl = 'http://mobcdn.clubpenguin.com/game/items/images/' . $avatarPaperUri;
$paperCurl = curl_init($avatarPaperUrl);
curl_setopt($paperCurl, CURLOPT_FRESH_CONNECT, true);
curl_setopt($paperCurl, CURLOPT_RETURNTRANSFER, true);
$imageData = curl_exec($paperCurl);
$statusCode = curl_getinfo($paperCurl, CURLINFO_HTTP_CODE);
curl_close($paperCurl);
if($statusCode == 200){
file_put_contents($avatarPaperUri, $imageData);
} else {
return false;
}
}
}
function cachePaper($itemId, $paperSize){
$downloadStatus = downloadPapers($itemId);
if($downloadStatus !== false){
$paperImage = imagecreatefrompng(sprintf('paper/image/%d/%d.png', $paperSize, $itemId));
return $paperImage;
} else {
return false;
}
}
function returnPaperResource($itemId, $paperSize = 120){
$avatarUnbiasUri = sprintf('paper/image/%d/%d.png', $paperSize, $itemId);
$paperImage = file_exists($avatarUnbiasUri) ? imagecreatefrompng($avatarUnbiasUri) : cachePaper($itemId, $paperSize);
return $paperImage;
}
$validPaperSizes = array(60, 88, 120, 600);
$defaultPaperSize = 120;
if(isset($_GET['id']) === false){
die();
}
$playerSwid = $_GET['id'];
if(isset($_GET['size']) === false){
$avatarPaperSize = 120;
} else {
$avatarPaperSize = $_GET['size'];
}
if(is_dir("paper/image/60/") === false){
mkdir("paper/image/60/", 0755, true);
}
if(is_dir("paper/image/88/") === false){
mkdir("paper/image/88/", 0755, true);
}
if(is_dir("paper/image/120/") === false){
mkdir("paper/image/120/", 0755, true);
}
if(is_dir("paper/image/600/") === false){
mkdir("paper/image/600/", 0755, true);
}
if(!in_array($avatarPaperSize, $validPaperSizes)){
$avatarPaperSize = $defaultPaperSize;
}
require 'configlinkhere';
try {
$database = new PDO('mysql:host=' . $config['db_host'] . ';dbname=' . $config['db_name'], $config['db_user'], $config['db_pass']);
} catch(PDOException $pdoException){
echo $pdoException->getMessage(), die();
}
$playerQuery = 'SELECT ID FROM `users` WHERE ID = :ID';
$playerStatement = $database->prepare($playerQuery);
$playerStatement->bindValue(':ID', $playerSwid);
$playerStatement->execute();
$rowCount = $playerStatement->rowCount();
$playerStatement->closeCursor();
if($rowCount < 1){
echo 'Player doesn''t exist', die();
}
$clothingQuery = 'SELECT head, face, neck, body, hand, feet, photo, flag, colour FROM `users` WHERE ID = :ID';
$clothingStatement = $database->prepare($clothingQuery);
$clothingStatement->bindValue(':ID', $playerSwid);
$clothingStatement->execute();
$playerClothing = $clothingStatement->fetch(PDO::FETCH_ASSOC);
$clothingStatement->closeCursor();
header('Content-type: image/png');
$colorResource = returnPaperResource($playerClothing['colour'], $avatarPaperSize);
unset($playerClothing['colour']);
if($playerClothing['photo'] != 0){
$imageResource = returnPaperResource($playerClothing['photo'], $avatarPaperSize);
if($imageResource === false) {
$imageResource = $colorResource;
} else {
imagecopyresampled($imageResource, $colorResource, 0, 0, 0, 0, imagesx($imageResource), imagesy($imageResource), imagesx($colorResource), imagesy($colorResource));
}
} else {
$imageResource = $colorResource;
}
unset($playerClothing['photo']);
foreach($playerClothing as $clothingPart => $itemId){
if($itemId != 0){
$clothingResource = returnPaperResource($itemId, $avatarPaperSize);
if($clothingResource !== false) {
imagecopyresampled($imageResource, $clothingResource, 0, 0, 0, 0, imagesx($imageResource), imagesy($imageResource), imagesx($clothingResource), imagesy($clothingResource));
}
}
}
imagealphablending($imageResource, false);
imagesavealpha($imageResource, true);
imagepng($imageResource);
imagedestroy($imageResource);
unset($database);
?>
我发现了它使用权限的问题root root
它应该使用权限www-data www-data