添加图像层失败 (GD) PHP

发布于 2024-11-06 15:05:33 字数 1868 浏览 0 评论 0原文

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

我已经尝试了许多不同的 videoID,但图像没有不加载。有一条消息表明该图形无法打开,因为它包含错误。

打开文件

我正在使用 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); 

?>

I've installed the GD Library on my Apache just now, and it seems that my script below doesn't work.
I'm trying to add a layer "play.png" to a youtube video thumbnail (http://img.youtube.com/vi/VIDEOID/default.jpg)

I've tried it with many different videoID's but the image doesn't load. There is a message that the graphic couldn't be opened because it contains errors.

I'm opening the file with postimage.php?v=7yV_JtFnIwo

http://img.youtube.com/vi/7yV_JtFnIwo/default.jpg opens correctly too...

Does anyone know where the issue could be?

Thanks in advance!

<?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); 

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇色安年 2024-11-13 15:05:33

不要使用 ?> 关闭(避免空格或新闻行)脚本,并使用 NULL 代替 ""

imagepng($background, NULL); 

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

Do not close (avoid whitespaces or newslines) your script with ?> and use NULL instead "".

imagepng($background, NULL); 

Then, in imagepng the quality parameter is between 0 and 9, as in http://it.php.net/manual/en/function.imagepng.php.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文