PHP会创建损坏的缩略图

时间:2012-03-30 22:26:34

标签: php

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

    // 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);
    }

1 个答案:

答案 0 :(得分:0)

我认为您的问题是您正在尝试打开不存在或不是图像的图像...在每个图像中,您都是通过$_FILES["photo"]['tmp_name']找到文件类型但是如果条件为真,则直接位于下方,您正在使用imagecreatefromXXX($filename)。您不应该打开上传的图像文件而不是您要保存图像的路径吗?打开路径$filename(假设它存在)将始终复制相同的正方形并保存它。这似乎不是你要做的事情。