Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 具有透明背景的Imagecrop变为黑色_Php_Image_Ubuntu - Fatal编程技术网

Php 具有透明背景的Imagecrop变为黑色

Php 具有透明背景的Imagecrop变为黑色,php,image,ubuntu,Php,Image,Ubuntu,我在从Base64字符串中裁剪图像时遇到问题,它在本地主机上运行得非常好,但当我将其上载到远程服务器时,透明背景是黑色的 $base64String = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAlongbase64string...." //see: https://pastebin.com/zqVumGPi for the full base64string $contentType = explode(':', substr($base6

我在从Base64字符串中裁剪图像时遇到问题,它在本地主机上运行得非常好,但当我将其上载到远程服务器时,透明背景是黑色的

$base64String = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAlongbase64string...."
//see: https://pastebin.com/zqVumGPi for the full base64string

$contentType = explode(':', substr($base64String, 0, strpos($base64String, ';')))[1];

$dataIn = str_replace("data:{$contentType};base64,", '', $base64String);
$dataIn = str_replace(' ', '+', $dataIn);
$dataIn = base64_decode($dataIn);
$src = imagecreatefromstring($dataIn);

$width = 960;
$height = 640;

$dst = imagecreatetruecolor($width, $height);

// Create transparency
imagesavealpha($dst, true);
$color = imagecolorallocatealpha($dst, 0, 0, 0, 127);
imagefill($dst, 0, 0, $color);

imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $width, $height);

$to_crop_array = array('x' =>intval(187) , 'y' => intval(48), 'width' => 320, 'height'=> 320);

//This is where the transparency disappears on my server. 
//It works fine on localhost
$thumb_im = imagecrop($dst, $to_crop_array);

header('Content-Type: image/png');
imagepng($thumb_im);
根据phpInfo的说法,我在本地主机和远程服务器上使用的是phpversion7.2.1,我在使用phpversion7.2.7-0ubuntu0.18.04.2

localhost上的GD版本为2.1.0,而remove服务器上的GD版本为2.2.5

localhost上的LibPNG版本是1.6.27,而我的远程服务器上使用的是1.6.34。 我的服务器运行在Ubuntu18.04.1 LTS上,由DigitalOcean托管。
我在另一台服务器上用LibPng和Php的相同版本尝试了这段代码,效果很好。

我用Imagick解决了这个问题

如果将来有人偶然发现这个问题,这里是我的解决方案

$contentType = explode(':', substr($base64String, 0, strpos($base64String, ';')))[1];

// Decode base64
$dataIn = str_replace("data:{$contentType};base64,", '', $base64String);
$dataIn = str_replace(' ', '+', $dataIn);
$imageBlob = base64_decode($dataIn);

$imagick = new Imagick();
$imagick->readImageBlob($imageBlob);
$imagick->cropImage(400,400, 30,10);

header('Content-Type: image/'.$imagick->getImageFormat());
echo $imagick->getImageBlob();