IT技术博客网IT技术博客网IT技术博客网

当前位置: 首页 > php开发

使用TP框架将jpg格式图片转换生成ico图标的代码

<?php
namespace app\index\controller;
use \think\Controller;
use \think\Request;



class Index extends controller
{
    public function index()
    {

// 源图片路径  
$sourceImage = 'public/index/images/erweima.jpg';  
 
// 目标图片路径  
$targetImage = 'public/ico.ico';  
  
// 创建图像资源  
$source = imagecreatefromjpeg($sourceImage);  
  
// 设置ICO图标尺寸(宽高)  
$width = 64; // 宽  
$height = 64; // 高  
  
// 创建空白目标图像资源  
$target = imagecreatetruecolor($width, $height);  

// 将源图像缩放为目标图像  
imagecopyresampled($target, $source, 0, 0, 0, 0, $width, $height, imagesx($source), imagesy($source));  
  
// 输出图像到文件  
header('Content-Type: image/x-icon'); // 设置输出类型为ICO图标  
imagepng($target, $targetImage); // 将目标图像保存为文件  
  
// 释放图像资源  
imagedestroy($source);  
imagedestroy($target);  


    }

}



技术QQ交流群:157711366

技术微信:liehuweb

写评论