通过php调整图像大小后显示黑色背景

时间:2015-08-12 18:50:23

标签: php image-resizing

我正在通过PHP调整图像大小:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php

include 'extraphp/config.php';

$title = $_POST['titleStory'];
$excerpt = $_POST['excerptStory'];
$story = $_POST['storyStory'];
$catagory = $_POST['catagory'];
$tags = $_POST['tagsStory'];
$author = $_POST['authorStory'];
$date = $_POST['dateStory'];

if($catagory == ""){}else{
    $catagory2 = implode (", ", $catagory);
}
if($author == ""){}else{
    $author2 = implode(',', $author);
}

/* Image Upload */
$image = $_FILES["imageStory"];
$image2= preg_replace("/[^A-Z0-9._-]/i", "_", $image["name"]);
$target_dir = "../uploads/large/";
$target_file = $target_dir . $image2;
if(file_exists($target_file)){
    $actual_name = pathinfo($image2,PATHINFO_FILENAME);
    $original_name = $actual_name;
    $extension = pathinfo($image2, PATHINFO_EXTENSION);

    $i = 1;
    while(file_exists($target_dir .$actual_name.".".$extension)){           
        $actual_name = (string)$original_name.$i;
        $image3 = $actual_name.".".$extension;
        $i++;
        }
        $target_file2 = $target_dir . $image3;
    move_uploaded_file($_FILES['imageStory']['tmp_name'], $target_file2);

    $target_file3 = "../uploads/thumb/" .$image3;
    list( $imageWidth, $imageHeight ) = getimagesize( $target_file2 );
    $resampledImage = imagecreatetruecolor( 180, 109 );
    //Check file extension here to use the correct image create function
    //imagecreatefromjpeg(); imagecreatefrompng(); imagecreatefromgif() etc...
    $source = imagecreatefromjpeg( $target_file2 );
    imagecopyresized( $resampledImage, $source, 180, 109, $imageWidth, $imageHeight );
    ob_start();
    //Check file extension here to use the correct image output function
    //imagejpeg(); imagegif(); imagepng() etc...
    imagejpeg( $resampledImage, null, 100 );
    $imageContent = ob_get_clean();
    file_put_contents( $target_file3, $imageContent );

    $sql="INSERT INTO story (titleStory,excerptStory,storyStory,catagory,tagsStory,authorStory,dateStory,imageStory) VALUES ('". $title ."','". $excerpt ."','". $story ."','". $catagory2 ."','". $tags ."','". $author2 ."','". $date ."','". $target_file2 ."')";
    mysqli_query($con,$sql);
    header("location: story-add-new.php?status=success");
}else{
    move_uploaded_file($_FILES['imageStory']['tmp_name'], $target_file);
    $sql="INSERT INTO story (titleStory,excerptStory,storyStory,catagory,tagsStory,authorStory,dateStory,imageStory) VALUES ('". $title ."','". $excerpt ."','". $story ."','". $catagory2 ."','". $tags ."','". $author2 ."','". $date ."','". $target_file ."')";
    mysqli_query($con,$sql);
    header("location: story-add-new.php?status=success");
}
?>

我面临的问题是调整后的图像是黑色但是我想要的尺寸相同(180x109),任何人都可以告诉我为什么这个图像会变黑?

2 个答案:

答案 0 :(得分:0)

尝试在

之后添加这些行
INFO [ 2015-08-12 13:50:56.024895 ] -- [ 636058 ] ## =3========== 413895
INFO [ 2015-08-12 13:50:55.983973 ] -- [ 702266 ] ## =3========== 903408
INFO [ 2015-08-12 13:50:56.026447 ] -- [ 636058 ] ## =3========== 159765
INFO [ 2015-08-12 13:50:56.027220 ] -- [ 636058 ] ## =3========== 623839
INFO [ 2015-08-12 13:50:56.028004 ] -- [ 636058 ] ## =3========== 119600
INFO [ 2015-08-12 13:50:56.030873 ] -- [ 636058 ] ## =3========== 438547
INFO [ 2015-08-12 13:50:56.031684 ] -- [ 636058 ] ## =3========== 350262
INFO [ 2015-08-12 13:50:55.989753 ] -- [ 702266 ] ## =3========== 140647
INFO [ 2015-08-12 13:50:56.033201 ] -- [ 636058 ] ## =3========== 711811
INFO [ 2015-08-12 13:50:55.991291 ] -- [ 702266 ] ## =3========== 614276
INFO [ 2015-08-12 13:50:56.034981 ] -- [ 636058 ] ## =3========== 016519
INFO [ 2015-08-12 13:50:56.036542 ] -- [ 636058 ] ## =3========== 040190
INFO [ 2015-08-12 13:50:56.037481 ] -- [ 636058 ] ## =3========== 704540
INFO [ 2015-08-12 13:50:56.038423 ] -- [ 636058 ] ## =3========== 099302
INFO [ 2015-08-12 13:50:56.039301 ] -- [ 636058 ] ## =2========== 838688
INFO [ 2015-08-12 13:50:56.040924 ] -- [ 636058 ] ## =3========== 411958
INFO [ 2015-08-12 13:50:56.042387 ] -- [ 636058 ] ## =3========== 332725
INFO [ 2015-08-12 13:50:56.045384 ] -- [ 636058 ] ## =3========== 816498
INFO [ 2015-08-12 13:50:56.001173 ] -- [ 237925 ] ## =1========== 237925

答案 1 :(得分:0)

我使用了imagecopyresampled($resampledImage, $source, 0,0,0,0,180,109,$imageWidth,$imageHeight);
而不是 imagecopyresized( $resampledImage, $source, 180, 109, $imageWidth, $imageHeight );
现在它像魅力一样工作;)