输出缩略图底部的黑色方块

时间:2011-02-02 21:35:21

标签: php image image-processing upload resize

我的输出imagecopyresized()缩略图图片上有黑色区域。

我的代码:

function thumbImage($src){
        /* thumb */
    list($height, $width) = getimagesize($src);

    $rel_difference_thumb = array('width'=>0, 'height'=>0);
    if($width > 79) { $rel_difference_thumb['width'] = ($width-79)/79; }
    if($height > 105) { $rel_difference_thumb['height'] = ($height-105)/105; }

    asort($rel_difference_thumb);
    $newwidth_thumb = $width/(1+end($rel_difference_thumb));
    $newheight_thumb = $height/(1+end($rel_difference_thumb)); 
    $newwidth_thumb = round($newwidth_thumb);
    $newheight_thumb = round($newheight_thumb);
    $jpeg_quality_thumb = 90;
    $thumbloc = 'images/users/privAlbum/thumb/'.$USER . md5(uniqid()) . '.jpg';
    switch(exif_imagetype($src)) {
    case IMAGETYPE_GIF:
    $img_r_thumb = imagecreatefromgif($src);
    break;
    case IMAGETYPE_JPEG:
    $img_r_thumb = imagecreatefromjpeg($src);
    break;
    case IMAGETYPE_PNG:
    $img_r_thumb = imagecreatefrompng($src);
    break;
    default:
    echo json_encode(array('error' => 'Ingen bild!'));
    exit(0);
    break;
    }
    $dst_r_thumb = ImageCreateTrueColor( $newwidth_thumb, $newheight_thumb );

    imagecopyresized($dst_r_thumb, $img_r_thumb, 0, 0, 0, 0, $newwidth_thumb , $newheight_thumb, $width, $height);

    if( imagejpeg($dst_r_thumb,$thumbloc,$jpeg_quality_thumb) ) {
    return true;
    }
    imagedestroy($img_r_thumb);

}

为什么会这样?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

list($height, $width) = getimagesize($src);应为list($width, $height) = getimagesize($src);

manual on getimagesize所述:

  

返回一个包含7个元素的数组:

     

索引0和1分别包含   图像的宽度和高度。