PHP if/while and or img resizing error

烂漫一生 提交于 2019-12-13 02:54:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!