您现在的位置是:首页  > 后端  > PHP PHP

PHP输出图片

2020-08-26【PHP】2041人已围观

简介> PHP ```php

PHP

  1. <?php
  2. $imgArr[] = "http://image.apidata.vip/blog/type/2020-04-30/158825027497343212.jpg";
  3. $imgArr[] = "http://image.apidata.vip/blog/type/2020-04-30/158824995423596172.png";
  4. $imgArr[] = "http://image.apidata.vip/blog/type/2020-04-30/15882498713244952.png";
  5. $imgArr[] = "http://image.apidata.vip/blog/type/2020-04-30/158824975631129892.png";
  6. //创建并载入一幅图像
  7. $imgKey = mt_rand(0,count($imgArr)-1);
  8. $img = $imgArr[$imgKey];
  9. $dis = strtolower(substr($img, strrpos($img, ".")+1));
  10. // 获取mime类型
  11. $image = exif_imagetype($img);
  12. $mime_type = image_type_to_mime_type($image);
  13. header("Content-type: {$mime_type}");
  14. if ($dis == "jpg") {
  15. $im = @ImageCreateFromJPEG($imgArr[$imgKey]);
  16. } else if ($dis == "png") {
  17. $im = @ImageCreateFromPNG($imgArr[$imgKey]);
  18. } else if ($dis == "gif") {
  19. $im = @ImageCreateFromGIF($imgArr[$imgKey]);
  20. }
  21. //错误处理
  22. if(!$im){
  23. $im = imagecreatetruecolor(150, 30);
  24. $bg = imagecolorallocate($im, 255, 255, 255);
  25. $text_color = imagecolorallocate($im, 0, 0, 255);
  26. //填充背景色
  27. imagefilledrectangle($im, 0, 0, 150, 30, $bg);
  28. //以图像方式输出错误信息
  29. imagestring($im, 3, 5, 5, "Error loading image", $text_color);
  30. } else {
  31. //输出该图像
  32. imagejpeg($im);
  33. }

html 页面调用

  1. <img src="/jpg.php" alt="" width="200" height="200">


关注博客,更多精彩分享,敬请期待!
 

Tags:

很赞哦! (0)

我的名片

网名:随心

职业:PHP程序员

现居:湖北省-武汉市

Email:704061912@qq.com