使用实时站点中的GD库时,我的图像出现问题。 linux中的GD库是否存在问题?我使用GD库实现了图像的调整大小和裁剪,但是某种程度上只有调整大小才有效。同样,png图像在调整大小后具有黑色背景。我的代码在本地运行正常,但在我的托管站点上运行不正常。我没有收到任何错误,所以我不确定问题出在哪里。
这是我的代码:
$info = getimagesize($src);
$source_image = '';
if ($info['mime'] == 'image/jpeg')
$source_image = imagecreatefromjpeg($src);
elseif ($info['mime'] == 'image/gif')
$source_image = imagecreatefromgif($src);
elseif ($info['mime'] == 'image/png')
$source_image = imagecreatefrompng($src);
$cropped = imagecropauto($source_image, IMG_CROP_DEFAULT);
if ($cropped !== false) {
imagedestroy($source_image);
$source_image = $cropped;
}
$width = imagesx($source_image);
$height = imagesy($source_image);
$maxHeight = floor($height * ($maxWidth / $width));
$dst = imagecreatetruecolor($maxWidth, $maxHeight);
$background = imagecolorallocate($dst, 0, 0, 0);
imagecolortransparent($dst, $background);
imagealphablending($dst, false);
imagesavealpha($dst,true);
imagecopyresampled($dst, $source_image, 0, 0, 0, 0, $maxWidth, $maxHeight, $width, $height);
if ($info['mime'] == 'image/jpeg')
imagejpeg($dst, $newFilename);
elseif ($info['mime'] == 'image/gif')
imagegif($dst, $newFilename);
elseif ($info['mime'] == 'image/png')
imagepng($dst, $newFilename);
请帮助。谢谢
答案 0 :(得分:0)
将IMG_CROP_DEFAULT替换为IMG_CROP_SIDES 还请阅读文档click here,您也可以使用
填充颜色
<?php
// Create a 300x300px transparant image with a 100px wide red circle in the middle
$i = imagecreatetruecolor(300, 300);
imagealphablending($i, FALSE);
imagesavealpha($i, TRUE);
$transparant = imagecolorallocatealpha($i, 0xDD, 0xDD, 0xDD, 0x7F);
imagecolortransparent($i, $transparant); // Set background transparent
imagefill($i, 0, 0, $transparant);
$red = imagecolorallocate($i, 0xFF, 0x0, 0x0);
imagefilledellipse($i, 150, 150, 100, 100, $red);
imagepng($i, "red_300.png");
// Crop away transparant parts and save
$i2 = imagecropauto($i, IMG_CROP_DEFAULT); //Attempts to use IMG_CROP_TRANSPARENT and if it fails it falls back to IMG_CROP_SIDES.
imagepng($i2, "red_crop_trans.png");
imagedestroy($i2);
// Crop away bg-color parts and save
$i2 = imagecropauto($i, IMG_CROP_SIDES);
imagepng($i2, "red_crop_sides.png");
imagedestroy($i2);
// clean up org image
imagedestroy($i);
?>