我正在做一个有趣的网站,但我似乎无法找出为什么这段代码不会工作:PHP调整图像,然后把它切成小块,并显示它,不工作
// 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>';
}
}
}
}
我是什么试图做的是:
- 在图像中加载并将其调整到合适的大小。
- 把它切成小块
- 表现出来一块一块用的(我这样做的.css文件因此div标签)
这些是我得到的错误,他们之间的空间一点点:
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
我认为,这些功能可能要到图像的路径,所以我也试图使这是调整的形象和将切割在这个地图保存那些\图像\。 ons \ Ons-foto \但是也没有工作。它没有保存图像,也没有将它们加载回来以在屏幕上显示它们。
什么它也可能是imagecreatefromjpeg()不输出,我认为它输出和$img
但fopen()
节省了没工作,要么给了同样的错误,所以我不能弄明白
莫非有人看看这个,并告诉我为什么它不起作用? 给那些谁呢,谢谢
为什么你想分开它?因为您可以使用CSS background-position在不同的div中显示相同照片的部分。您的解决方案会带来更多流量和带宽 – Codebeat
我不知道有一种方法可以用CSS来做到这一点。 – bergkid