php调整图像大小然后切成碎片并显示它,不起作用

时间:2012-10-21 23:43:44

标签: php image

我正在建立一个有趣的网站,但我似乎无法弄清楚为什么这段代码不会起作用:

// error control
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
// loading in the image.
$img = imagecreatefromjpeg(".\image\ons\Ons-foto\mayor.jpg");

if($img != null){
    $width = imagesx($img);
    $heigth = imagesy($img);
    $calw = 134*7;
    $calh = 122*7;
    $newimg = null;
    if($width != null && $heigth != null){

        // a check to see if the image is the right size

        if($width > $calw || $width < $calh || $heigth < $calh || $heigth > $calh)
        {
        // here i save the resized image into the variable $newimg

        imagecopyresized($newimg, $img, 0,0,0,0, $calw, $calh, $width, $heigth);
        } else {
        $newimg = $img;
        }
        $tempimg = null;
        // a nested loop to automatically cut the image into pieces

        for($w = 0; $w < $calw; $w+134){
            for($h = 0; $h < $calh; $h+122){
                // taking a piece of the image and save it into $tempimg

                imagecopy($tempimg, $newimg, 0, 0, $w, $h, 134, 122);

                // showing the image on screen
                echo '<div class="blokken" style="margin: 1px;">';
                imagejpeg($tempimg);             
                echo '</div>';
            }
        }
    }
}

我要做的是:

  1. 加载图片并将其调整为合适的大小。
  2. 切成碎片
  3. 逐个显示它们之间的一点点空间(我在.css文件中这样做,因此div标签)
  4. 这些是我得到的错误:

    Warning: imagecopyresized(): supplied argument is not a valid Image resource in root\ons.php on line 52
    
    Warning: imagecopy(): supplied argument is not a valid Image resource in root\ons.php on line 63
    
    Warning: imagejpeg(): supplied argument is not a valid Image resource in root\ons.php on line 67
    
    Warning: imagecopy(): supplied argument is not a valid Image resource in root\ons.php on line 63
    

    我认为这些函数可能需要一个图像路径,因此我还尝试制作已调整大小的图像以及在此地图中保存的图像。\ image \ ons \ Ons-foto \但是也没用。它没有保存图像,也没有加载它们以在屏幕上显示它们。

    它也可能是imagecreatefromjpeg()没有输出我认为它输出并保存在$imgfopen()也不起作用并且给出了同样的错误所以我无法计算它出来了

    有人可以看看这个并告诉我为什么它不起作用? 对那些做的人,谢谢

1 个答案:

答案 0 :(得分:1)

您需要先创建$newimg$tempimg,然后才能将其复制到其中。所有其他错误都是从这个问题中解决的。