问题
coders. Im very new to php, so my code is prob as wrong as it gets. Im loading plain text from a txt file and useing that to load pictures from a website. (Thanks to this forum for helping me with that). Now im having problems with resizing those photos. First of all, i dont think i got the if statement right on the last 2. and i dont really know how to use "while", and if i even have to use it.
Can you look over the code and see if you see any mistakes? Thank you.
In the first (if) section i made the text in the txt file to images. Now i want to resize all those images. In the second (if) im trying to call all those images to be resized. In the third (if) im trying to call all those resized images so i can echo them in the last bit of code.
<?php
$file = 'serverlist.txt';
$servers = '';
if ($handle = fopen($file, 'r')) {
while (!feof($handle)) {
$content = trim(fgets($handle));
$names = explode(' ', $content);
foreach ($names as $name) {
$servers .= '<img src="http://minecraft.net/skin/' . $name . '.png" alt="">';
}
}
fclose($handle);
} else {
}
if $file == 'serverlist.txt' {
$fullimages = explode($servers);
foreach ($fullimages as $_fullimages) {
$face = imagecreatetruecolor($width, $height);
$imgwidth = 200;
$imgheight = 200;
imagecopyresized($face, $_fullimages, 0, 0, 8, 8, $width, $height, 8, 8);
imagecopyresized($face, $_fullimages, 0, 0, 40, 8, $width, $height, 8, 8);
}
} else {
}
if $file = 'serverlist.txt' {
$displayimages = explode($_fullimages);
foreach ($displayimages as $_displayimages) {
}
} else {
}
echo $_displayimages;
?>
回答1:
You are asigned the values in every if
statement you should use ==
instead of single =
<?php
$file = 'serverlist.txt';
$servers = '';
if ($handle == fopen($file, 'r')) {
while (!feof($handle)) {
$content = trim(fgets($handle));
$names = explode(' ', $content);
foreach ($names as $name) {
$servers .= '<img src="http://minecraft.net/skin/' . $name . '.png" alt="">';
}
}
fclose($handle);
} else {
}
if ($file == 'serverlist.txt') {
$fullimages = explode($servers);
foreach ($fullimages as $_fullimages) {
$face = imagecreatetruecolor($width, $height);
$imgwidth = 200;
$imgheight = 200;
imagecopyresized($face, $_fullimages, 0, 0, 8, 8, $width, $height, 8, 8);
imagecopyresized($face, $_fullimages, 0, 0, 40, 8, $width, $height, 8, 8);
}
} else {
}
if ($file == 'serverlist.txt') {
$displayimages = explode($_fullimages);
foreach ($displayimages as $_displayimages) {
}
} else {
}
echo $_displayimages;
?>
来源:https://stackoverflow.com/questions/15447913/php-if-while-and-or-img-resizing-error