从显示黑色背景的上传图像创建缩略图

时间:2014-06-06 06:36:31

标签: php

我正在获取图片缩略图,但背景是黑色的。我无法理解为什么会发生这种情况。虽然我在stackoverflow中找到了这个脚本。 是gd library.i'm使用xampp

的问题
makeThumbnails($upload_dir.$new_nm,227,182,$upload_dir_thumb.$new_nm);
function makeThumbnails($src,$w,$h,$dir)
{
    $arr_image_details = getimagesize($src); 
    $original_width = $arr_image_details[0];
    $original_height = $arr_image_details[1];

   if ($original_width > $original_height) {
        $new_width = $w;
        $new_height = intval($original_height * $new_width / $original_width);
    } else {
        $new_height = $h;
        $new_width = intval($original_width * $new_height / $original_height);
    }
    $dest_x = intval(($w - $new_width) / 2);
    $dest_y = intval(($h - $new_height) / 2);

    if ($arr_image_details[2] == 1) {
        $imgt = "ImageGIF";
        $imgcreatefrom = "ImageCreateFromGIF";
    }
    if ($arr_image_details[2] == 2) {
        $imgt = "ImageJPEG";
        $imgcreatefrom = "ImageCreateFromJPEG";
    }
    if ($arr_image_details[2] == 3) {
        $imgt = "ImagePNG";
        $imgcreatefrom = "ImageCreateFromPNG";
    }
    if ($imgt) {
        $old_image = $imgcreatefrom($src);
        $new_image = imagecreatetruecolor($w, $h);
        imagecopyresized($new_image, $old_image, $dest_x, $dest_y, 0, 0, $new_width, $new_height, $original_width, $original_height);
        $imgt($new_image, $dir);
    }

2 个答案:

答案 0 :(得分:0)

我认为你在$old_image = $imgcreatefrom($src)

做错了
What will return `$imgcreatefrom($src)` check it.

enter image description here

答案 1 :(得分:0)

function makethumb($raw,$tw,$th,$DestFolder)
{
    $arr_image_details = getimagesize($raw); 
    $CurWidth = $arr_image_details[0];
    $CurHeight = $arr_image_details[1];
    //print_r($arr_image_details);exit;
    switch(strtolower($arr_image_details['mime']))
    {

               case 'image/png':
                    $CreatedImage = imagecreatefrompng($raw);
                    break;
                case 'image/gif':
                    $CreatedImage = imagecreatefromgif($raw);
                    break;
                case 'image/jpeg':
                case 'image/pjpeg':
                    $CreatedImage = imagecreatefromjpeg($raw);
                    break;
                default:
                    $processImage = false; //image format is not supported!
    }
    if($tw >=$th )
    {
        $MaxSize=$tw;
    }else{
        $MaxSize=$th;
    }
    $ImageScale         = min($MaxSize/$CurWidth, $MaxSize/$CurHeight); 
    $NewWidth           = ceil($ImageScale*$CurWidth);
    $NewHeight          = ceil($ImageScale*$CurHeight);
    if($CurWidth < $NewWidth || $CurHeight < $NewHeight)
    {
        $NewWidth = $CurWidth;
        $NewHeight = $CurHeight;
    }
    $NewCanves  = imagecreatetruecolor($NewWidth, $NewHeight);

    if(imagecopyresampled($NewCanves, $CreatedImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight))
    {
        switch(strtolower($arr_image_details['mime']))
        {
            case 'image/png':
                imagepng($NewCanves,$DestFolder);
                break;
            case 'image/gif':
                imagegif($NewCanves,$DestFolder);
                break;          
            case 'image/jpeg':
            case 'image/pjpeg':
                imagejpeg($NewCanves,$DestFolder,90);
                break;
            default:
                return false;
        }
    if(is_resource($NewCanves)) { 
      imagedestroy($NewCanves); 
    } 
    return true;
    }




}