【发布时间】:2014-12-14 17:04:24
【问题描述】:
我对 PHP 中的 GD 库有一点问题 - 我调整了图像大小,然后我想将其裁剪为 320 像素(宽度)/240 像素(高度)。让我说调整大小的图像是 320px/300px。当我裁剪它时,图像底部会出现一个 1 像素的黑色条带 - 我不知道为什么。
我正在使用imagecrop、imagecreatefromjpeg 和imagecopyresampled
示例如下:
感谢您的宝贵时间。
代码
$filename = '../store/projects/project-123.jpg';
$mime = mime_content_type($filename);
list($w, $h) = getimagesize($filename);
$prop = $w / $h;
$new_w = 0;
$new_h = 0;
if ($prop <= 4/3) {
$new_w = 320;
$new_h = (int)floor($h*($new_w/$w));
} else {
$new_h = 240;
$new_w = (int)floor($w*($new_h/$h));
}
$thumb = imagecreatetruecolor($new_w, $new_h);
if (strcmp($mime,'image/png') == 0) {
header('Content-Type: image/png');
$source = imagecreatefrompng($filename);
} else {
header('Content-Type: image/jpeg');
$source = imagecreatefromjpeg($filename);
}
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
$filename = '../store/projects-thumbs/project-123.jpg';
$crop_data = array('x' => 0 , 'y' => 0, 'width' => 320, 'height'=> 240);
$thumb = imagecrop($thumb, $crop_data);
imagejpeg($thumb, $filename, 100);
imagedestroy($thumb);
imagedestroy($source);
【问题讨论】:
-
请出示您的代码
-
我编辑了我的帖子——代码在上面:)
标签: php image-processing gd