PHP调整图像然后将其切成零件并显示出来,不起作用



我正在创建一个有趣的网站,但我似乎无法弄清楚为什么这件代码无法正常工作:

// error control
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
// loading in the image.
$img = imagecreatefromjpeg(".imageonsOns-fotomayor.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标签)

这些是我遇到的错误:

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

我认为这些功能可能需要通往图像的路径,因此我还尝试制作要调整大小的图像,并切割出来保存在此地图中的图像。也没有工作。它没有保存图像,也没有将它们加载到屏幕上。

也可能是,ImageCreatefromjpeg()没有输出我认为它输出并保存在$img中的内容,但是fopen()也无法正常工作,并且给出了相同的错误,所以我无法弄清楚它

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

您需要创建$newimg$tempimg,然后才能复制到它们中。所有其他错误都是从这个问题中级联。

最新更新