试图透明图像php

时间:2016-02-12 03:33:30

标签: php

我又来了。我需要帮助,我需要把透明的img php 但我无法得到它。我是PHP的新手,我想要学习。我在PHP.net上阅读,我只删除了一些问题。

以下是代码:

 <?php
header('Content-type: image/jpeg');
$im = file_get_contents("http://fthw.cf/count/index.php?dt=2016-02-13/10:00:00");
$black = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $black);
imagepng($im, '$img');
imagedestroy($im);
?>

错误日志:

    [11-Feb-2016 22:00:17 America/New_York] PHP Warning:  imagecolorallocate() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 4
[11-Feb-2016 22:00:17 America/New_York] PHP Warning:  imagecolortransparent() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 5
[11-Feb-2016 22:00:17 America/New_York] PHP Warning:  imagepng() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 6
[11-Feb-2016 22:00:17 America/New_York] PHP Warning:  imagedestroy() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 7

问候! PS:帮帮我吧

1 个答案:

答案 0 :(得分:0)

<?php
function copyTransparent($src, $output)
{
    $dimensions = getimagesize($src);
    $x = $dimensions[0];
    $y = $dimensions[1];
    $im = imagecreatetruecolor($x,$y); 
    $src_ = imagecreatefrompng($src); 
    // Prepare alpha channel for transparent background
    $alpha_channel = imagecolorallocatealpha($im, 0, 0, 0, 127); 
    imagecolortransparent($im, $alpha_channel); 
    // Fill image
    imagefill($im, 0, 0, $alpha_channel); 
    // Copy from other
    imagecopy($im,$src_, 0, 0, 0, 0, $x, $y); 
    // Save transparency
    imagesavealpha($im,true); 
    // Save PNG
    imagepng($im,$output,9); 
    imagedestroy($im); 
}
$png = 'file.png';

copyTransparent($png,"png.png");
?>

这是一个可以用来使图像透明的功能