添加图像层失败(GD)PHP

时间:2011-05-10 07:34:04

标签: php gd

我刚才在我的Apache上安装了GD库,似乎我的下面的脚本不起作用。 我正在尝试在youtube视频缩略图中添加“play.png”图层(http://img.youtube.com/vi/VIDEOID/default.jpg)

我尝试过很多不同的视频ID,但图片没有加载。有一条消息说无法打开图形,因为它包含错误。

我用postimage.php打开文件?v = 7yV_JtFnIwo

http://img.youtube.com/vi/7yV_JtFnIwo/default.jpg也正确打开...

有谁知道问题出在哪里?

提前致谢!

<?php

  // The header line informs the server of what to send the output
  // as. In this case, the server will see the output as a .png
  // image and send it as such

  header ("Content-type: image/png"); 


  // Defining the background image. Optionally, a .jpg image could 
  // could be used using imagecreatefromjpeg, but I personally 
  // prefer working with png

  $background = imagecreatefromjpeg("http://img.youtube.com/vi/".$_GET['v']."/default.jpg"); 


  // Defining the overlay image to be added or combined.

  $insert = imagecreatefrompng("play.png"); 


  // Select the first pixel of the overlay image (at 0,0) and use
  // it's color to define the transparent color

  imagecolortransparent($insert,imagecolorat($insert,0,0));


  // Get overlay image width and hight for later use

  $insert_x = imagesx($insert); 
  $insert_y = imagesy($insert); 


  // Combine the images into a single output image. Some people
  // prefer to use the imagecopy() function, but more often than 
  // not, it sometimes does not work. (could be a bug)

  imagecopymerge($background,$insert,0,0,0,0,$insert_x,$insert_y,100); 


  // Output the results as a png image, to be sent to viewer's
  // browser. The results can be displayed within an HTML document
  // as an image tag or background image for the document, tables,
  // or anywhere an image URL may be acceptable.

  imagepng($background,"",100); 

?>

1 个答案:

答案 0 :(得分:1)

请勿使用?>关闭(避免使用空格或新闻专线)并使用NULL代替""

imagepng($background, NULL); 

然后,在imagepng中,质量参数介于0和9之间,如http://it.php.net/manual/en/function.imagepng.php所示。