繁体   English   中英

通过PHP调整图像大小后,它显示黑色背景

[英]After resizing image by php it show black background

我正在像这样通过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),任何人都可以告诉我为什么这个图像会变黑?

尝试在之后添加这些行,

$resampledImage = imagecreatetruecolor( 180, 109 );

imagealphablending( $resampledImage, FALSE );
imagesavealpha( $resampledImage, TRUE );

我用
imagecopyresampled($resampledImage, $source, 0,0,0,0,180,109,$imageWidth,$imageHeight);
代替
imagecopyresized( $resampledImage, $source, 180, 109, $imageWidth, $imageHeight );
现在它的工作就像魅力;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM