2012-03-30 76 views
0

这是我正在thumnbail脚本,它最终创建一个损坏的33字节图像。我认为问题出在exif_imagetype if语句中,但我不确定。我将不胜感激任何帮助。PHP创建腐败thumnbail

// Original image 
    $filename = 'images/T' . $neutralName; 

    // Get dimensions of the original image 
    list($current_width, $current_height) = getimagesize($filename); 

    // The x and y coordinates on the original image where we 
    // will begin cropping the image 
    $left = 10; 
    $top = 5; 

    // This will be the final size of the image (e.g. how many pixels 
    // left and down we will be going) 
    $crop_width = 140; 
    $crop_height = 100; 

    // Resample the image 
    $canvas = imagecreatetruecolor($crop_width, $crop_height); 
    if ((exif_imagetype($_FILES['photo']['tmp_name'])) == IMAGETYPE_JPEG) { 
     $current_image = imagecreatefromjpeg($filename); 
     imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height); 
     imagejpeg($canvas, $filename, 100); 
    } else if ((exif_imagetype($_FILES["photo"]['tmp_name'])) == IMAGETYPE_GIF) { 
     $current_image = imagecreatefromgif($filename); 
     imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height); 
     imagegif($canvas, $filename, 100); 
    } else { 
     $current_image = imagecreatefrompng($filename); 
     imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height); 
     imagepng($canvas, $filename, 100); 
    } 
+0

有更多的代码?那个声明之后你在用$ canvas做什么? – mikevoermans 2012-03-30 22:29:49

+2

那个图像里面有什么?也许这是33个符号的错误信息? – 2012-03-30 22:29:51

+1

所有的代码都在那里,canvas被定义在if语句的正上方。是的,我相信有33个符号的错误。它始终认为任何图片都是PNG图片,这意味着即使它可能是JPG,它仍然无法通过第一次和第二次测试。 – Ramin 2012-03-30 22:34:29

回答

0

我觉得你的问题是你试图打开一个不存在或不是一个图像的图像......在每个各的,你通过$_FILES["photo"]['tmp_name']但找到的文件类型直接在下面,如果条件成立,则使用imagecreatefromXXX($filename)。您不应该打开上传的图像文件,而不是要保存图像的路径?打开路径$filename(假设它存在)将始终复制相同的确切平方并保存。看起来你不会做可行的事情。

+0

您是完全正确的,我现在修复它。而不是检查原始图片,看看它是JPG还是PNG或其他东西,我要测试$ filename来查看它是什么类型的图片。我已经测试过它,它工作。万分感谢!这只是我不知情的事情。 – Ramin 2012-03-30 22:47:32