上传脚本会显示损坏的图像

时间:2013-12-09 16:09:28

标签: php thumbnails image-uploading

这是我的图片上传脚本。它确实上传了正常的图像,就像缩略图一样。普通图像上传到uploads文件夹,缩略图上传到uploads / thumbs文件夹。但奇怪的是:缩略图已上传,但是当转到服务器上的图像时,它会给出一个像这样的破碎图像块[x]

让我的导游通过我的代码:

首先,我开始为每个图像创建变量:

//#################### Image upload ########################//
for($i=0; $i<count($_FILES['image']['name']); $i++) {
$imgtemp = $_FILES["image"]["tmp_name"][$i];
$imgtype = $_FILES["image"]["type"][$i];
$imgsize = $_FILES["image"]["size"][$i];
$ext = end(explode(".", $_FILES["image"]["name"][$i]));
if( $imgtype == 'image/jpeg' ){ $filetype= '.jpg'; }else{ $filetype= str_replace ( 'image/', '', $imgtype ); }
$image_name = md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) ) . '.jpg';
$path= '../../uploads/' . $image_name;
$thumb_name = "thumb_" . md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) ) . '.jpg';
$thumb_path= '../../uploads/thumbs/' . $thumb_name;
$imgsize2= getimagesize( $imgtemp );
$width= $imgsize2[0];
$height= $imgsize2[1];

根据变量$image_thumbheight$image_thumbwidth

,下面的脚本会根据变量进行检查
//
        if( $width == $height ){ $case=1; }
        if( $width > $height ){ $case=2; }
        if( $width < $height ){ $case=3; }
        if(isEmpty($image_thumbheight) && !isEmpty($image_thumbwidth)){


        switch( $case ){

            case 1:

            $newwidth= $image_thumbwidth;
            $newheight= $image_thumbwidth;

            break;

            case 2:

            $newwidth= $image_thumbwidth;
            $ratio= $newwidth / $width;
            $newheight= $height * $ratio;

            break;

            case 3:

            $newwidth= $image_thumbwidth;
            $ratio= $newwidth / $width;
            $newheight= $height * $ratio;

            break;

            }
        } 
        if(isEmpty($image_thumbheight) && isEmpty($image_thumbwidth)){


        switch( $case ){

            case 1:

            $newwidth= 100;
            $newheight= 100;

            break;

            case 2:

            $newheight= 100;
            $ratio= $newheight / $height;
            $newwidth= round( $width * $ratio );

            break;

            case 3:

            $newwidth= 100;
            $ratio= $newwidth / $width;
            $newheight= $height * $ratio;

            break;

            }
        } 
        if(!isEmpty($image_thumbheight) && !isEmpty($image_thumbwidth)){


        switch( $case ){

            case 1:

            $newwidth= $image_thumbwidth;
            $newheight= $image_thumbheight;

            break;

            case 2:

            $newheight= $image_thumbheight;
            $ratio= $newheight / $height;
            $newwidth= round( $width * $ratio );

            break;

            case 3:

            $newwidth= $image_thumbwidth;
            $ratio= $newwidth / $width;
            $newheight= $height * $ratio;

            break;

            }
        } 
        if(!isEmpty($image_thumbheight) && isEmpty($image_thumbwidth)){


        switch( $case ){

            case 1:

            $newwidth= $image_thumbheight;
            $newheight= $image_thumbheight;

            break;

            case 2:

            $newheight= $image_thumbheight;
            $ratio= $newheight / $height;
            $newwidth= round( $width * $ratio );

            break;

            case 3:

            $newheight= $image_thumbheight;
            $ratio= $newheight / $height;
            $newwidth= round( $width * $ratio );

            break;

            }
        } 

我认为问题在于下面的代码,因为这是上传脚本的最后一部分。它将上传的文件移动到给定的路径:

            switch( $imgtype ){

                case 'image/jpeg';

                $img= imagecreatefromjpeg( $imgtemp );
                $thumb= imagecreatetruecolor( $newwidth, $newheight );
                imagecopyresampled( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
                imagejpeg( $thumb, $thumb_path, 70 );  

                break;

                case 'image/png';

                $img= imagecreatefrompng( $imgtemp );
                $thumb= imagecreatetruecolor( $newwidth, $newheight );
                imagecopyresampled( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
                imagepng( $thumb, $thumb_path, 70 ); 

                break;

                case 'image/gif';

                $img= imagecreatefromgif( $imgtemp );
                $thumb= imagecreatetruecolor( $newwidth, $newheight );
                imagecopyresampled( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
                imagegif( $thumb, $thumb_path, 70 ); 

                break;


            }
            move_uploaded_file( $imgtemp, $path );
}

有人可以解决这个问题吗?

0 个答案:

没有答案