Icon.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare (strict_types=1);
  3. namespace library\utils;
  4. // +----------------------------------------------------------------------
  5. // | [ WE CAN DO IT MORE SIMPLE ]
  6. // +----------------------------------------------------------------------
  7. // | Copyright (c) 2018-2020 rights reserved.
  8. // +----------------------------------------------------------------------
  9. // | Author: TABLE ME
  10. // +----------------------------------------------------------------------
  11. // | Date: 2020-11-15 20:30
  12. // +----------------------------------------------------------------------
  13. namespace library\utils;
  14. class Icon
  15. {
  16. public function mkIcon($img,$new_img) {
  17. $exp = strtoupper(substr(strrchr($img, '.'), 1));
  18. //下载图片
  19. $data = $this->httpcopy($img,app()->getRootPath()."public/public/tmp/"
  20. .md5($img .time()).'.'.$exp);
  21. $img = $data['fileName'];
  22. if(empty($img)) return null;
  23. $new_img = $new_img . md5($img) . '_favicon.ico';
  24. if($exp == 'PNG') {
  25. $im = @imagecreatefrompng($img);
  26. imagesavealpha($im,true);
  27. $img_func = 'imagepng';
  28. }
  29. if($exp == 'JPG' || $exp == 'JPEG') {
  30. $im = @imagecreatefromjpeg($img);
  31. $img_func = 'imagejpeg';
  32. }
  33. if($exp == 'GIF') {
  34. $im = @imagecreatefromgif($img);
  35. $img_func = 'imagegif';
  36. }
  37. $quality = 100;//图片质量
  38. if($img_func == 'imagepng')
  39. {
  40. $quality = 9;
  41. }
  42. $imginfo = @getimagesize($img);
  43. $size = 64;
  44. $resize_im = @imagecreatetruecolor(64,64);
  45. $color = imagecolorallocatealpha($resize_im,0, 0, 0, 127);
  46. imagefill($resize_im,0,0,$color);
  47. imagecopyresampled($resize_im,$im,0,0,0,0,$size,$size,$imginfo[0],$imginfo[1]);
  48. imagesavealpha($resize_im,true);
  49. call_user_func_array($img_func,array($resize_im,app()->getRootPath() . 'public/' . $new_img,$quality));
  50. imagedestroy($resize_im);//清除句柄
  51. imagedestroy($im);//清除句柄
  52. @unlink($img);
  53. return ['root_img'=>app()->getRootPath() . $new_img,'img'=>$new_img];
  54. }
  55. /**
  56. * 生成down
  57. * @param $url
  58. * @param $file
  59. * @return null
  60. */
  61. private function httpcopy($url,$file){
  62. $result = [];
  63. $ch=curl_init();
  64. curl_setopt($ch,CURLOPT_URL,$url);
  65. curl_setopt($ch,CURLOPT_TIMEOUT,60);
  66. curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
  67. $temp=curl_exec($ch);
  68. if(@file_put_contents($file,$temp) &&!curl_error($ch)){
  69. $result['fileName']=$file;
  70. $result['way']='curl';
  71. $result['size']=sprintf('%.3f',strlen($temp)/1024);
  72. }
  73. return $result;
  74. }
  75. }