PHP调整大小的缩略图是黑色的?

时间:2012-01-04 17:55:52

标签: php jquery

我正在使用jquery file upload中的脚本并修改create_scaled_image函数以与我的数据库一起使用。看起来图像正在重新调整尺寸,但是图像是完全黑色的。知道会导致这种情况的原因吗?

以下是我对脚本的修改:

private function create_scaled_image($file_name, $options) {

    list($img_width, $img_height) = @getimagesize($file_name);

    error_log("Width: $img_width");
    error_log("Height: $img_height");

    if (!$img_width || !$img_height) {
        return false;
    }
    $scale = min(
        $options['max_width'] / $img_width,
        $options['max_height'] / $img_height
    );

    error_log("SCALE: $scale");
    if ($scale > 1) {
        $scale = 1;
    }
    $new_width = $img_width * $scale;
    $new_height = $img_height * $scale;
    $new_img = @imagecreatetruecolor($new_width, $new_height);
    switch (strtolower(substr(strrchr($file_name, '.'), 1))) {
        case 'jpg':
        case 'jpeg':
            $src_img = @imagecreatefromjpeg($file_name);
            break;
        case 'gif':
            @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
            $src_img = @imagecreatefromgif($file_name);
            break;
        case 'png':
            @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
            @imagealphablending($new_img, false);
            @imagesavealpha($new_img, true);
            $src_img = @imagecreatefrompng($file_name);
            break;
        default:
            $src_img = $image_method = null;
    }

    @imagecopyresampled(
        $new_img,
        $src_img,
        0, 0, 0, 0,
        $new_width,
        $new_height,
        $img_width,
        $img_height
    ); 

    // Free up memory (imagedestroy does not delete files):
    @imagedestroy($src_img);

    return $new_img;
}

我对该函数的调用如下所示:

        $options['max_width'] = 80;
        $options['max_height'] = 80;

        $thumb_source = $this->create_scaled_image($uploaded_file, $options);

        ob_start(); // Start capturing stdout.
        imagejpeg($thumb_source); // As though output to browser.
        $thumb_bin = mysql_real_escape_string(ob_get_contents()); // the raw jpeg image data.
        ob_end_clean(); // Dump the stdout so it does not screw other output.

        // Code to insert data into my DB here

1 个答案:

答案 0 :(得分:2)

删除代码中的@并查找错误(如果有)。 @抑制使用后面的函数时发生的任何错误。