【发布时间】:2014-09-13 10:57:41
【问题描述】:
我有一个 450x450 像素的源图像。我想使用 PHP 将它裁剪成 9 个 150x150 的片段。
我正在使用以下代码。
我得到 9 张图像,但裁剪不合适。
这是屏幕截图:
最后一张是我正在使用的源图像。
这是我的代码。
<?php
$targ_w = $targ_h = 150;
$jpeg_quality = 25;
$src = "puzzleimages/puzzle2.jpg";
$y = 0;
$y2 = 150;
$x = 0;
$x2 = 150;
$i = 3;
$name = 0;
while( $i-- ){
$j = 3;
while( $j-- ){
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
imagecopyresampled($dst_r,$img_r,0,0,$x,$y,$targ_w,$targ_h,$x2,$y2);
//header('Content-type: image/jpeg');
imagejpeg($dst_r,'puzzleimages/'.++$name.'.jpg', $jpeg_quality);
$x += 150;
$x2 += 150;
}
$y += 150;
$y2 += 150;
$x = 0;
$x2 = 150;
}
?>
谁能指出出了什么问题?
【问题讨论】:
-
exec(sprintf("convert -crop 150x150 %s", escapeshellarg($fn)))将是生成图块的更简单替代方法。 -
我在 imagemagick 中做了一些非常相似的事情,你有那个库吗?
-
我从未使用过任何外部库,所以我避免使用 imagemagick。