【问题标题】:PHP if/while and or img resizing errorPHP if/while 和/或 img 调整大小错误
【发布时间】:2013-03-16 10:00:48
【问题描述】:

编码员。我对 php 很陌生,所以我的代码很可能是错误的。 我从 txt 文件加载纯文本并使用它从网站加载图片。 (感谢这个论坛帮助我)。现在我在调整这些照片的大小时遇到​​了问题。首先,我不认为我在最后 2 条上得到了正确的 if 语句。我真的不知道如何使用“while”,如果我什至必须使用它。

你能看一下代码,看看有没有错误吗?谢谢。

在第一个(如果)部分中,我将 txt 文件中的文本转换为图像。现在我想调整所有这些图像的大小。在第二个(如果)我试图调用所有这些图像来调整大小。在第三个(如果)中,我试图调用所有这些调整大小的图像,以便我可以在最后一段代码中回显它们。

<?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;
?>

【问题讨论】:

    标签: php html while-loop if-statement


    【解决方案1】:

    在每个if 语句中为您分配了值,您应该使用== 而不是单个=

    <?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;
        ?>
    

    【讨论】:

    • 现在页面加载。谢谢你。但我没有得到任何内容。如果我在第一个 else{} 之后删除所有内容,则会加载图像。
    • 你能解释一下你想做什么吗?
    • 在第一个 if 部分中,我将 txt 文件中的文本转换为图像。现在我想调整所有这些图像的大小。在第二个中,如果我试图调用所有这些图像来调整大小。在第三个中,如果我试图调用所有这些调整大小的图像,以便我可以在最后一段代码中回显它们。
    • 那么你可以像这样给出 minecraft.net/skin' . $name . '.png" alt="" style="width:100px;height:100px;"> 这样你可以省略这两个步骤
    • 我会这样做,但我还需要裁剪图像。
    猜你喜欢
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 2020-10-01
    相关资源
    最近更新 更多